DDz
DDz

Reputation: 57

keras trainable attribute not compatible with tensorflow?

It seems that keras trainable attribute is ignored by tensorflow, which makes it very inconvenient to use keras as a syntactical shortcut in tensorflow.

For example:

import keras
import tensorflow as tf
import numpy as np
import keras.backend as K

Conv2 = keras.layers.Conv2D(filters=16, kernel_size=3, padding='same')
Conv2.trainable = False #This layers has been set to not trainable.

A=keras.layers.Input(batch_shape=(1,16,16,3))
B = Conv2(A)


x = np.random.randn(1, 16, 16,3)
y = np.random.randn(1,16, 16, 16)

True_y = tf.placeholder(shape=(1,16,16,16), dtype=tf.float32)

loss = tf.reduce_sum((B - True_y) ** 2)

opt_op = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)
print(tf.trainable_variables())
# [<tf.Variable 'conv2d_1/kernel:0' shape=(3, 3, 3, 16) dtype=float32_ref>, <tf.Variable 'conv2d_1/bias:0' shape=(16,) dtype=float32_ref>]
sess = K.get_session()

for _ in range(10):
    out = sess.run([opt_op, loss], feed_dict={A:x, True_y:y})
    print(out[1])

OutPut:

5173.94
4968.7754
4785.889
4624.289
4482.1
4357.5757
4249.1504
4155.329
4074.634
4005.6482

It simply means the loss is decreasing and the weights are trainable.

I read the blog ''Keras as a simplified interface to TensorFlow'', but it mentioned nothing about the trainable problem.

Any suggestion is appreciated.

Upvotes: 0

Views: 93

Answers (1)

Zvika
Zvika

Reputation: 1280

Your conclusion is basically correct. Keras is a wrapper around TensorFlow, but not all Keras functionality transfers directly into TensorFlow, so you need to be careful when you mix Keras and raw TF.

Specifically, in this case, if you want to call the minimize function yourself, you need to specify which variables you want to train on using the var_list argument of minimize.

Upvotes: 1

Related Questions