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.