Skip to main content
 首页 » 编程设计

python之使用张量的值作为另一个的形状

2024年10月01日9lyj

我的代码中有以下两行:

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' 

请您参考如下方法:

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

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] 

当然,reshaping 应该保留 y 中元素的总数,所以它的用处有限。

编辑 2

您不能将一个占位符的形状参数化为另一个占位符。这没有意义,因为同时提供了占位符。提供一个形状未知的占位符然后像您在第一个示例中所做的那样提供一个参数化的 reshape 操作。