2
$\begingroup$

I have implemented and trained a sequential model using tf.keras. Say I am given an input array of size 8X8 and an output [0,1,0,...(rest all 0)].

How to calculate the gradient of the input w.r.t to the given output?

model = ...
output = np.asarray([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
input = np.random.randn(1,64)
pred = model.predict(input)

gradient = somefunction(model,input,output,pred) 

Is there any way to achieve that?

I am trying to implement a project similar to Google Deep Dreams, so that given a random picture, if I mention a digit, then with each iteration I will update the input with its gradient to make the picture more like the digit that was asked.

I tried to follow other StackOverflow answers and Keras documentation. But as they are not mentioning the input shape, when someone writes input[:,:,something] I am having a hard time translating it to my requirement.

Can you provide a simple generic answer so that given an array as input and an expected output array, one can calculate the gradient of loss w.r.t the input for that exact output.

$\endgroup$

1 Answer 1

2
$\begingroup$

You can get that from the weight updates, not sure if it is the best approach

-Save the model
-Save the weights of the first layer
-Load the model and Compile the model with SGD w/o momentum
-Set all the weights = that of the previous model
-Train with the input and output i.e. the Array for epoch=1 and batch_size=1
-Get the weights again
-Calculate the respective gradient using two weights of 1st layer
-If Gradient is very small, it might become zero due to matching digits of both the weights

w0 = model.get_weights().copy()
w0_0 = w0[0]

optimizer = tf.keras.optimizers.SGD( learning_rate=0.01, momentum=0.0, nesterov=False, name="SGD")
model.compile(optimizer=optimizer, loss='mse')
model.fit(input, output, epochs=1, verbose=1, batch_size=1)

w0_new = model.get_weights().copy()
w0_0_new = w0_new[0]

'''
w_new = w0 - learning_rate * g
g = (w0 - w_new)/learning_rate
'''
grad = (w0_0 - w0_0_new)/0.01
$\endgroup$
3
  • $\begingroup$ Actually the model is already fitted. What I want is, if I were to fit this new image into the model, then what the gradient would have become. I don't want to update the weights of the model, only want to change the input ndarray so that it becomes more aligned to the desired output. $\endgroup$ Commented Jul 18, 2020 at 14:04
  • 1
    $\begingroup$ That is what I have suggested. You can't get the Gradient w/o passing the data and Gradient depends on the current status of weights. You take a copy of your trained model, pass the image, measure the weight update, and calculate the Gradient. Your initial trained version is intact. It is done on a copy $\endgroup$
    – 10xAI
    Commented Jul 18, 2020 at 14:14
  • $\begingroup$ It is a good solution but I would like a more direct way if it possible. $\endgroup$ Commented Mar 9, 2021 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.