I have a Keras model and I want to do some cool visiualizations with it. It's an object recognition network.

So I thought, It would be cool to input a blank image into the network and treat the image as the variable and not the weights, and then train the network to always output an icecream for example.

So I wrote the following code:

#loading the model
model = load_model('model.h5')

#create the input image as a variable
w = tf.Variable(tf.zeros([1,224,224,3]))

#create the flowgraph with the variable input
pred = model.call(inputs=w)

#create the desired output distribution
desired = np.zeros((1000))
desired[928] = 1.0

err = tf.reduce_mean(tf.subtract(pred,desired))
lr = tf.placeholder(dtype=tf.float32, shape=None)

#create an optimizer that can only affect the inital input variable I created
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(err, var_list=[w])

#train the network
for i in range(0,100):
    _,cost = sess.run([optimizer,err])

So I thought the code would work well, but the cost literally doesn't change. It stays in place as if it's entirely unaffected.


Try explicitly setting the variable as trainable by setting Trainable=True. Also after the network is created, check if the variable is in the list of trainables https://www.tensorflow.org/api_docs/python/tf/trainable_variables

  • 1
    $\begingroup$ Didn't work.. no change $\endgroup$ – ronsap123 Oct 6 '18 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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