Bugs of getting gradient of loss function with respect to input using Keras

Viewed 827
model = keras.Sequential([         #my model, trained on Fashion_MNIST
    keras.layers.Conv2D(32, (3,3), activation=tf.nn.relu, input_shape=(28,28,1)),
    keras.layers.MaxPooling2D(),
    keras.layers.Conv2D(64, (3,3), activation=tf.nn.relu),
    keras.layers.MaxPooling2D(),
    keras.layers.Flatten(),
    keras.layers.Dense(1000, activation='relu'),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.load_weights('./weights/235model')            #load weights
model.compile(optimizer=tf.train.AdamOptimizer(),   
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

tmp = np.full((10,1),0.0)                           #get correct label
tmp[test_labels[0]] = 1.0
y_tensor=tf.convert_to_tensor(tmp)

loss = keras.losses.sparse_categorical_crossentropy(model.output,    y_tensor)
gradients = keras.backend.gradients(loss, model.input)
print(gradients)

I am doing an iterative FGSM attack, so I need to get the gradient of the loss with respect to the input, but when I print gradients, it shows [None] instead of a valid tensor.

Later I run it,

with keras.backend.get_session() as sess:

    evaluated_gradients = sess.run(gradients, feed_dict={model.input:np.reshape(test_images[0],(1,28,28,1))})

It gives me error /.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 258, in for_fetch type(fetch))) TypeError: Fetch argument None has invalid type

If I change loss to model.output in setting the gradients, it works ok, but if I use the crossentropy loss there, it doesn't work, could someone helps me? I am a beginner to keras and tensorflow.

1 Answers
Related