Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Created October 28, 2017 14:19
Show Gist options
  • Save bzamecnik/59c3fb0068a92e6930baa12c6f9ca8b5 to your computer and use it in GitHub Desktop.
Save bzamecnik/59c3fb0068a92e6930baa12c6f9ca8b5 to your computer and use it in GitHub Desktop.
TensorFlow tf.Variable with dynamic shape
# - prevent the variable to be used as a model parameter: trainable=False, collections=[]
# - allow dynamic variable shape (for the last batch): validate_shape=False
placeholder = tf.placeholder(dtype=tf.float32, shape=(None, 10))
variable = tf.Variable(placeholder, trainable=False, collections=[], validate_shape=False)
with tf.Session() as sess:
sess.run(variable.initializer, feed_dict={placeholder: np.arange(20).reshape(2,10)})
print('shape:', sess.run(tf.shape(variable)))
print(sess.run(variable))
sess.run(variable.initializer, feed_dict={placeholder: np.arange(30).reshape(3,10)})
print('shape:', sess.run(tf.shape(variable)))
print(sess.run(variable))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment