Is there a tensorflow equivalent to np.empty?

后端 未结 3 1100
误落风尘
误落风尘 2021-01-18 10:39

Numpy has this helper function, np.empty, which will:

Return a new array of given shape and type, without initializing entries.

相关标签:
3条回答
  • 2021-01-18 10:58

    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.

    0 讨论(0)
  • 2021-01-18 11:15

    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)>
    
    0 讨论(0)
  • 2021-01-18 11:19

    In TF 2,

    tensor = tf.reshape(tf.convert_to_tensor(()), (0, n))
    

    worked for me.

    0 讨论(0)
提交回复
热议问题