There are many methods in TensorFlow that requires specifying a shape, for example truncated_normal:
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
I have a placeholder for the input of shape [None, 784], where the first dimension is None because the batch size can vary. I could use a fixed batch size but it still would be different from the test/validation set size.
I cannot feed this placeholder to tf.truncated_normal because it requires a fully specified tensor shape. What is a simple way to having tf.truncated_normal accept different tensor shapes?
A TensorShape represents a possibly-partial shape specification for a Tensor . It may be one of the following: Fully-known shape: has a known number of dimensions and a known size for each dimension. e.g. TensorShape([16, 256])
Create a tensor of n-dimensionYou begin with the creation of a tensor with one dimension, namely a scalar. Each tensor is displayed by the tensor name. Each tensor object is defined with tensor attributes like a unique label (name), a dimension (shape) and TensorFlow data types (dtype).
Tensors are used in all kinds of operations (or "Ops"). Note: Typically, anywhere a TensorFlow function expects a Tensor as input, the function will also accept anything that can be converted to a Tensor using tf. convert_to_tensor . See below for an example.
To define a multidimensional array (tensor) of size n in python, we can use the array method of NumPy. Alternatively, the Pytorch tensor method. The arguments M1,M2,...,Mn are arrays of size n-1. Both methods create a tensor of n dimensions.
You just need to feed it in as a single example but in the batched shape. So that means adding an extra dimension to the shape e.g.
batch_size = 32 # set this to the actual size of your batch tf.truncated_normal((batch_size, 784), mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
This way it will "fit" into the placeholder.
If you expect batch_size to change you can also use:
tf.truncated_normal(tf.shape(input_tensor), mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
Where input_tensor could be a placeholder or just whatever tensor is going to have this noise added to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With