How can I change the shape of a variable in TensorFlow?

醉酒当歌 提交于 2019-12-17 10:58:13

问题


TensorFlow tutorial says that at creation time we need to specify the shape of tensors. That shape automatically becomes the shape of the tensor. It also says that TensorFlow provides advanced mechanisms to reshape variables. How can I do that? Any code example?


回答1:


Take a look at shapes-and-shaping from TensorFlow documentation. It describes different shape transformations available.

The most common function is probably tf.reshape, which is similar to its numpy equivalent. It allows you to specify any shape that you want as long as the number of elements stays the same. There are some examples available in the documentation.




回答2:


The tf.Variable class is the recommended way to create variables, but it restricts your ability to change the shape of the variable once it has been created.

If you need to change the shape of a variable, you can do the following (e.g. for a 32-bit floating point tensor):

var = tf.Variable(tf.placeholder(tf.float32))
# ...
new_value = ...  # Tensor or numpy array.
change_shape_op = tf.assign(var, new_value, validate_shape=False)
# ...
sess.run(change_shape_op)  # Changes the shape of `var` to new_value's shape.

Note that this feature is not in the documented public API, so it is subject to change. If you do find yourself needing to use this feature, let us know, and we can investigate a way to support it moving forward.




回答3:


Documentation shows methods for reshaping. They are:

  • reshape
  • squeeze (removes dimensions of size 1 from the shape of a tensor)
  • expand_dims (adds dimensions of size 1)

as well as bunch of methods to get shape, size, rank of your tensor. Probably the most used is reshape and here is a code example with a couple of edge cases (-1):

import tensorflow as tf

v1 = tf.Variable([
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12]
])
v2 = tf.reshape(v1, [2, 6])
v3 = tf.reshape(v1, [2, 2, -1])
v4 = tf.reshape(v1, [-1])
# v5 = tf.reshape(v1, [2, 4, -1]) will fail, because you can not find such an integer for -1
v6 = tf.reshape(v1, [1, 4, 1, 3, 1])
v6_shape = tf.shape(v6)
v6_squeezed = tf.squeeze(v6)
v6_squeezed_shape = tf.shape(v6_squeezed)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)
a, b, c, d, e, f, g = sess.run([v2, v3, v4, v6, v6_shape, v6_squeezed, v6_squeezed_shape])
# print all variables to see what is there
print e # shape of v6
print g # shape of v6_squeezed



回答4:


tf.Variable(tf.placeholder(tf.float32))

is not valid at tensorflow 1.2.1

in python shell:

import tensorflow as tf
tf.Variable(tf.placeholder(tf.float32))

You will get:

ValueError: initial_value must have a shape specified: Tensor("Placeholder:0", dtype=float32)

Update: if you add validate_shape=False, there will be no error.

tf.Variable(tf.placeholder(tf.float32), validate_shape=False)

if tf.py_func matches your requirement:

def init():
    return numpy.random.rand(2,3)
a = tf.pyfun(init, [], tf.float32)

You can create variable that has any shape by passing your own init function.

Another way:

var = tf.get_varible('my-name', initializer=init, shape=(1,1))

You can pass tf.constant or any init function that returns numpy array. The shape provided will not be validated. The output shape is your real data shape.




回答5:


tf.Variable: Use the shape argument with None

A feature was added in 1.14 that allows to specify unknown shapes.

If shape is None, the initial shape value is used.

If shape is specified, this is used as the shape and allows to have None.

Example:

var = tf.Variable(array, shape=(None, 10))

This allows to later on assign values with shapes matching the shape above (e.g. arbitrary shapes in axis 0)

var.assign(new_value)



来源:https://stackoverflow.com/questions/33654754/how-can-i-change-the-shape-of-a-variable-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!