I'm trying to get the gradients wrt input for my model:
input_mat = np.random.rand(1,252,252,1)
with tf.Session() as sess:
input_tensor = tf.placeholder(shape=input_mat.shape,dtype=tf.float32)
outmat = tf.convert_to_tensor(np.dstack((np.identity(252)[:,:,np.newaxis],np.zeros((252,252,36))))[np.newaxis,:,:,:])
input_layer = tf.keras.layers.Input(shape=(252,252,1))
layer = tf.keras.layers.Conv2D(activation='relu',kernel_size=(3,3),filters=37,padding='same')(input_layer)
m = tf.keras.Model(input_layer,layer)
prob_dist = m(input_tensor)
loss_dist = tf.keras.losses.categorical_crossentropy(y_pred=prob_dist,y_true=outmat,from_logits=True)
grads = K.gradients(loss_dist,m.input)
sess.run(tf.global_variables_initializer())
y = sess.run(grads, feed_dict={input_tensor:input_mat})
However, I'm getting the following error:
TypeError: Fetch argument None has invalid type <class 'NoneType'>
Apparently, the gradient appears to be None. How can I fix it?
Compute your gradient against the tf.Placeholder
:
grads = K.gradients(loss_dist,input_tensor)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.