1
$\begingroup$

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.

$\endgroup$
2
0
$\begingroup$

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

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

The error is simple when you input a blank image. Let us say black image just like you have done with tf.zeros the resulting images will be full of 0s so coming to the point when you will multiply your weights by 0 the resulting dot product will be 0

because the formula is sum(weights * conv) and as conv is filled with 0 the dot product will be 0.

Then we will add the bias every value in the resulting image will be the bias activation next still the same value everywhere. No matter what input you will give and what weights you will have in the first layer its gradient will be 0 and thus will not change. No matter what input image you give the resulting image will be 0 filled with the bias values and the final output will be the same. Thus it will not learn anything.

$\endgroup$

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.