Find Input that maximizes class output score in tensorflow JS

Viewed 128

I have a trained CNN network for the MNIST dataset in tensorflow JS. I am trying to tune the input so that one of the output scores is maximized. I know how to do this in python using keras like so:

from keras import backend as K
import numpy as np


class_num = 0 
output = model.layers[-1].output
loss = K.mean(output[:,class_num]) 

grads = K.gradients(loss, model.input)[0] 
grads = K.l2_normalize(grads)

func = K.function([model.input], [loss, grads])


input = np.random.random((1, 28, 28)) 

for i in range(10):
    loss_val, grads_val = func([input])
    input_img += grads_val 

The problem is I don't know how to do the same in tensorflow JS. Can someone help in a similar implementation for tensorflow JS? I am unable to get working loss and gradient functions. Many Thanks!

0 Answers
Related