所以假设我有一个张量
X = tf.placeholder("float", [None, 5])
Run Code Online (Sandbox Code Playgroud)
这样我就知道列数而不是行数.我需要初始化维度的向量nrows x 1
现在,以下代码块不起作用,
o = tf.ones(shape=(tf.shape(X)[0], 1))
==> TypeError: List of Tensors when single Tensor expected
Run Code Online (Sandbox Code Playgroud)
也没有,
o = tf.ones(shape=(X.get_shape()[0].value, 1))
==> TypeError: Input 'dims' of 'Fill' Op has type
string that does not match expected type of int32.
Run Code Online (Sandbox Code Playgroud)
现在,我发现解决这个问题的一种方法是实际上使我的矢量为占位符,
o = tf.placeholder(dtype=tf.float32, shape=[None, 1])
Run Code Online (Sandbox Code Playgroud)
并传入一个适当大小的numpy数组feed_dict.但是这个解决方案让我感到不优雅,而不是占位符的预期用途.我可能在这里错了,但肯定有更好的方法.