Numpy has this helper function, np.empty, which will:
Return a new array of given shape and type, without initializing entries.
The closest thing you can do is create a variable that you do not initialize. If you use tf.global_variables_initializer()
to initialize your variables, disable putting your variable in the list of global variables during initialization by setting collections=[]
.
For example,
import numpy as np
import tensorflow as tf
x = tf.Variable(np.empty((2, 3), dtype=np.float32), collections=[])
y = tf.Variable(np.empty((2, 3), dtype=np.float32))
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# y has been initialized with the content of "np.empty"
y.eval()
# x is not initialized, you have to do it yourself later
x.eval()
Here np.empty
is provided to x
only to specify its shape and type, not for initialization.
Now for operations such as tf.concat
, you actually don't have (indeed cannot) manage the memory yourself -- you cannot preallocate the output as some numpy
functions allow you to. Tensorflow already manages memory and does smart tricks such as reusing memory block for the output if it detects it can do so.
If you're creating an empty tensor, tf.zeros
will do
>>> a = tf.zeros([0, 4])
>>> tf.concat([a, [[1, 2, 3, 4], [5, 6, 7, 8]]], axis=0)
<tf.Tensor: shape=(2, 4), dtype=float32, numpy=
array([[1., 2., 3., 4.],
[5., 6., 7., 8.]], dtype=float32)>
In TF 2,
tensor = tf.reshape(tf.convert_to_tensor(()), (0, n))
worked for me.