简体   繁体   中英

TensorFlow Variable Increase Shape

I'm not sure if this is even possible, but what I want here is that in the first print statement below, I'd get a 3-entry column vector and in the second print statement below, I'd get a 4-entry column vector.

import tensorflow as tf

# Mx = b
M = tf.Variable([[1,2,3],[4,5,6],[7,8,9]],validate_shape=False)
x = tf.Variable([[1],[2],[3]])
b = tf.matmul(M,x)


init = tf.global_variables_initializer()

with tf.Session() as sess:
        sess.run(init)
        print(sess.run(b))
        M.load([[1,2,3],[4,5,6],[7,8,9],[10,11,12]],sess)
        print(sess.run(b))

This code doesn't work since "load" yells about the shape being wrong:

ValueError: Cannot feed value of shape (4, 3) for Tensor 'Variable/initial_value:0', which has shape '(3, 3)'

Also reshape doesn't work because I want to actually increase the total number of entries in the matrix as well.

Is there any simple way to do this? (preferably without adding many more nodes to my computational graph)

Thanks, JacKeown

According to the documentation variable shape can not be changed.

The Variable() constructor requires an initial value for the variable, which can be a Tensor of any type and shape. The initial value defines the type and shape of the variable. After construction, the type and shape of the variable are fixed . The value can be changed using one of the assign methods.

(emphasis added)

The solution would be to use placeholders.

let's say you're trying to learn the values for matrix M.

Please note that the use of "None" in the placeholder's shape allows you the provide inputs with different sizes.

import tensorflow as tf

# Mx = b
M = tf.Variable([[1,2,3],[4,5,6],[7,8,9]],validate_shape=False)
x = tf.Placeholder(shape=(None, 3))

b = tf.matmul(M,x)


init = tf.global_variables_initializer()

with tf.Session() as sess:
        sess.run(init)
        print(sess.run(b))
        print(sess.run(b, feed_dict = {x:[[1],[2],[3]]}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM