使用Tensor的值作为另一个的形状?

时间:2017-08-02 10:47:15

标签: python tensorflow

我的代码中有以下两行:

numSequences = tf.placeholder(tf.float32, shape=())
...
prediction = tf.reshape(predictionFlat, [numSequences, sequenceLength, vocabSize])

是否可以从numSequences张量中提取标量值,以便在定义prediction张量的形状时将其用作值?

修改

以下是我的更多代码:

x = tf.placeholder(tf.float32, [None, sequenceLength, vocabSize])
y = tf.placeholder(tf.float32, [None, sequenceLength, vocabSize])
numSequences = tf.placeholder(tf.float32, shape=())
xFlat = tf.contrib.layers.flatten(x)    # [batchSize, sequenceLength*vocabSize]

W = tf.Variable(tf.random_normal([hiddenDimension, sequenceLength, vocabSize]))
b = tf.Variable(tf.random_normal([1, sequenceLength, vocabSize]))
WFlat = tf.contrib.layers.flatten(W)
bFlat = tf.contrib.layers.flatten(b)

cell = rnn.BasicLSTMCell(hiddenDimension, forget_bias=forgetRate)
outputs, states = tf.nn.static_rnn(cell, [xFlat], dtype=tf.float32) 
predictionFlat = tf.add(tf.matmul(outputs[0], WFlat), bFlat) # outputs = [np.array([batchSize, hiddenDimension])] -> outputs[0] = [batchSize, hiddenDimension]
prediction = tf.reshape(predictionFlat, [numSequences, sequenceLength, vocabSize])

编辑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'

1 个答案:

答案 0 :(得分:2)

是的,张量形状本身通常可以是张量,但它们必须是整数类型。

import tensorflow as tf

x = tf.constant([2, 3], dtype=tf.int32)
y = tf.zeros((x[0], x[1], 5))

sess = tf.InteractiveSession()
print(y.eval().shape)
# (2, 3, 5)

修改

更接近你的例子,

import tensorflow as tf

x = tf.placeholder(tf.int32, shape=())
y = tf.zeros((500,))

sess = tf.InteractiveSession()
print(sess.run(tf.shape(tf.reshape(y, [x, x, 5])), {x: 10}))
# [10 10 5]

当然,重塑应该保留y中元素的总数,因此它的用处有限。

编辑2

您无法通过其他位置参数设置一个占位符的形状。由于占位符是同时提供的,因此没有意义。提供一个形状未知的占位符然后提供参数化重塑操作,就像在第一个示例中一样。