我定义了一个像这样的张量:
x = tf.get_variable("x", [100])
但是当我尝试打印张量的形状时:
print( tf.shape(x) )
我得到Tensor("Shape:0",shape =(1,),dtype = int32),为什么输出的结果不应该是shape =(100)
我尝试运行此代码:
outputs, states = rnn.rnn(lstm_cell, x, initial_state=initial_state, sequence_length=real_length)
tensor_shape = outputs.get_shape()
for step_index in range(tensor_shape[0]):
word_index = self.x[:, step_index]
word_index = tf.reshape(word_index, [-1,1])
index_weight = tf.gather(word_weight, word_index)
outputs[step_index, :, :]=tf.mul(outputs[step_index, :, :] , index_weight)
Run Code Online (Sandbox Code Playgroud)
但我在最后一行得到错误:
TypeError: 'Tensor' object does not support item assignment
似乎我无法分配张量,我该如何解决?
我在tensorflow中找到了两种RNN实现.
第一个实现是这个(从第124行到第129行).它使用循环来定义RNN中的每个输入步骤.
with tf.variable_scope("RNN"):
for time_step in range(num_steps):
if time_step > 0: tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(inputs[:, time_step, :], state)
outputs.append(cell_output)
states.append(state)
Run Code Online (Sandbox Code Playgroud)
第二个实现是这个(从第51行到第70行).它不使用任何循环来定义RNN中的每个输入步骤.
def RNN(_X, _istate, _weights, _biases):
# input shape: (batch_size, n_steps, n_input)
_X = tf.transpose(_X, [1, 0, 2]) # permute n_steps and batch_size
# Reshape to prepare input to hidden activation
_X = tf.reshape(_X, [-1, n_input]) # (n_steps*batch_size, n_input)
# Linear activation
_X = tf.matmul(_X, _weights['hidden']) + _biases['hidden']
# Define a lstm cell with …Run Code Online (Sandbox Code Playgroud) python deep-learning lstm tensorflow recurrent-neural-network