我的代码中有以下两行:
numSequences = tf.placeholder(tf.float32, shape=())
...
prediction = tf.reshape(predictionFlat, [numSequences, sequenceLength, vocabSize])
在定义prediction
张量的形状时,是否可以从numSequences
张量中提取标量值作为值使用?在
编辑
下面是我的更多代码:
^{pr2}$编辑2
我正在尝试做类似的事情,我需要我的sequenceLength
变量(张量形状的参数)是占位符而不是固定值。我的实现方法与对numSequences
相同,但是我得到了如下所示的错误。我不明白这与我最初询问的前一个numSequences
实现有何不同。在
代码:
numSequences = tf.placeholder(tf.int32, shape=())
seqLength = tf.placeholder(tf.int32, shape=())
x = tf.placeholder(tf.float32, [None, seqLength, vocabSize])
y = tf.placeholder(tf.float32, [None, seqLength, vocabSize])
xFlat = tf.contrib.layers.flatten(x) # [batchSize, sequenceLength*vocabSize]
W = tf.Variable(tf.random_normal([hiddenDimension, seqLength, vocabSize]))
b = tf.Variable(tf.random_normal([1, seqLength, vocabSize]))
WFlat = tf.contrib.layers.flatten(W) # [hiddenDimension, sequenceLength*vocabSize]
bFlat = tf.contrib.layers.flatten(b) # [1, sequenceLength*vocabSize]
cell = rnn.BasicLSTMCell(hiddenDimension, forget_bias=forgetRate)
outputs, states = tf.nn.static_rnn(cell, [xFlat], dtype=tf.float32) # outputs = [[batchSize, hiddenDimension]]
predictionFlat = tf.add(tf.matmul(outputs[0], WFlat), bFlat) # outputs[0] = [batchSize, hiddenDimension]
prediction = tf.reshape(predictionFlat, [numSequences, seqLength, vocabSize])
错误:
x = tf.placeholder(tf.float32, [None, seqLength, vocabSize])
File "/usr/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
name=name)
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 705, in apply_op
attr_value.shape.CopyFrom(_MakeShape(value, key))
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 198, in _MakeShape
return tensor_shape.as_shape(v).as_proto()
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 798, in as_shape
return TensorShape(shape)
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 434, in __init__
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 434, in <listcomp>
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 376, in as_dimension
return Dimension(value)
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 32, in __init__
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'
是的,张量形状通常可以是张量本身,但是它们必须是整数类型。在
编辑
更接近你的例子
^{pr2}$当然,整形应该保留
y
中元素的总数,因此它的用处有限。在编辑2
不能用另一个placehoder参数化一个占位符的形状。因为同时提供了占位符,所以没有意义。提供一个形状未知的占位符,然后像第一个示例中那样提供参数化的重塑操作。在
相关问题 更多 >
编程相关推荐