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

前端 未结 6 542
醉梦人生
醉梦人生 2020-12-03 07:21

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 TensorF

6条回答
  •  时光取名叫无心
    2020-12-03 07:33

    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.

提交回复
热议问题