TransWikia.com

Can't train input variable with Keras+Tensorflow

Data Science Asked by ronsap123 on May 6, 2021

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])
    print(cost)

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.

One Answer

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

Answered by Surajit Podder on May 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP