Skip to content

Inconsistent gradient #31136

@nairouz

Description

@nairouz

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Colab
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary):
  • TensorFlow version (use command below): 1.4.0
  • Python version: 3.6
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory: Colab GPU

Describe the current behavior
I have a very simple Keras model and I want to compute the gradient of the different layers using TensorFlow. I start by creating the computational graph in the first cell of a Jupyter notebook. Here is the code of the computational graph:

import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.backend as K
import numpy as np
from tensorflow.keras.layers import Dense, Input, Layer
from tensorflow.keras.models import Model
input_tensor = Input(shape=(20,), name="input")
print(input_tensor.name)
hidden = Dense(100, activation='relu')(input_tensor)
out1 = Dense(10, activation='relu', name="out1")(hidden)
model = Model(inputs=input_tensor, outputs=[out1])
grad = []
for i in range(4):
   grad.append(tf.gradients(out1, model.trainable_weights[i]))
model.compile(loss={"out1": "mse"}, 
optimizer=tf.train.AdamOptimizer(learning_rate=0.001))

np.random.seed(0)
X = np.random.random((3, 20)).astype(np.float32)
Y = np.random.random((3, 10)).astype(np.float32)
model.fit(x={'input' : X}, y={'out1' : Y}, batch_size=1, epochs=10)

Then each time I run the tf.gradients operator, I get a different gradient vector (the gradient changes). This result is not reasonable. Where is the problem in my code?

And here is the code for the created Session:

with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())
   out_grad = sess.run(grad, feed_dict={'input:0':X})
   print(out_grad)

Describe the expected behavior
The same gradient each time I run the out_grad = sess.run(grad, feed_dict={'input:0':X})

Metadata

Metadata

Assignees

Labels

comp:kerasKeras related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions