Is there a tensorflow equivalent to np.empty?

こ雲淡風輕ζ 提交于 2019-12-20 01:58:09

问题


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

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

I find it pretty useful when I want to create a tensor using tf.concat since:

The number of dimensions of the input tensors must match, and all dimensions except axis must be equal.

So it comes in handy to start with an empty tensor of an expected shape. Is there any way to achieve this in tensorflow?

[edit]

A simplified example of why I want this

    netInput = np.empty([0, 4])
    netTarget = np.empty([0, 4])
    inputWidth = 2

    for step in range(data.shape.as_list()[-2]-frames_width-1):
        netInput = tf.concat([netInput, data[0, step:step + frames_width, :]], -2)
        target = tf.concat([target, data[0, step + frames_width + 1:step + frames_width + 2, :]], -2)

In this example, if netInput or netTarget are initialized, I'll be concatenating an extra example with that initialization. And to initialize them with the first value, I need to hack the loop. Nothing mayor, I just wondered if there is a 'tensorflow' way to solve this.


回答1:


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.



来源:https://stackoverflow.com/questions/50463092/is-there-a-tensorflow-equivalent-to-np-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!