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?
相关问题
- batch_dot with variable batch size in Keras
- Extract matrix elements using a vector of column i
- Reshape matrix by rows
- How to use Reshape keras layer with two None dimen
- CV2 Image Error: error: (-215:Assertion failed) !s
相关文章
- tensorflow 神经网络 训练集准确度远高于验证集和测试集准确度?
- Tensorflow: device CUDA:0 not supported by XLA ser
- Numpy matrix of coordinates
- Numpy array to TFrecord
- conditional graph in tensorflow and for loop that
- How to downgrade to cuda 10.0 in arch linux?
- Apply TensorFlow Transform to transform/scale feat
- How to force tensorflow tensors to be symmetric?
is not valid at tensorflow 1.2.1
in python shell:
You will get:
Update: if you add
validate_shape=False
, there will be no error.if
tf.py_func
matches your requirement:You can create variable that has any shape by passing your own init function.
Another way:
You can pass
tf.constant
or anyinit
function that returns numpy array. The shape provided will not be validated. The output shape is your real data shape.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.
Documentation shows methods for reshaping. They are:
as well as bunch of methods to get
shape
,size
,rank
of your tensor. Probably the most used isreshape
and here is a code example with a couple of edge cases (-1):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):
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.