2

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.

1 Answers1

2

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

10xAI
  • 5,929
  • 2
  • 9
  • 25