Python 如何在Tensorflow中使用采样的最大损耗

Python 如何在Tensorflow中使用采样的最大损耗,python,numpy,tensorflow,deep-learning,lstm,Python,Numpy,Tensorflow,Deep Learning,Lstm,我是tensorflow的初学者。我已经建立了简单的模型,但还没有尝试过类似多层LSTM的东西,因此非常感谢任何反馈:) 我目前正试图从头开始重新编写由构建的字符级模型,原因很简单,因为我想知道如何使用tensorflow(我之前已经按照cs231n的分配构建了自己的非常小的DL库)。现在,我正在努力构建一个简单的2层LSTM模型,但不确定到底出了什么问题。以下是我迄今为止编写的代码: class Model(): def __init__(self, batch_size, seq_l

我是tensorflow的初学者。我已经建立了简单的模型,但还没有尝试过类似多层LSTM的东西,因此非常感谢任何反馈:)

我目前正试图从头开始重新编写由构建的字符级模型,原因很简单,因为我想知道如何使用tensorflow(我之前已经按照cs231n的分配构建了自己的非常小的DL库)。现在,我正在努力构建一个简单的2层LSTM模型,但不确定到底出了什么问题。以下是我迄今为止编写的代码:

class Model():
    def __init__(self, batch_size, seq_length, lstm_size, num_layers, grad_clip, vocab_size):
        self.lr = tf.Variable(0.0, trainable=False)        

        #Define input and output
        self.input_data = tf.placeholder(tf.float32, [batch_size, seq_length])
        self.output_data = tf.placeholder(tf.float32, [batch_size, seq_length]) #although int would be better for character level..

        #Define the model
        cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=lstm_size) #can choose if basic or otherwise later on...
        self.cell = cell = rnn_cell.MultiRNNCell([cell] * num_layers)
        self.initial_state = cell.zero_state(batch_size, tf.float32)


        with tf.variable_scope("lstm"):
            softmax_w = tf.get_variable("softmax_w", [lstm_size, vocab_size])
            softmax_b = tf.get_variable("softmax_b", [vocab_size])

        #_, enc_state = rnn.rnn(cell, encoder_inputs, dtype=dtype)
        #outputs, states = rnn_decoder(decoder_inputs, enc_state, cell)


        outputs, states = seq2seq.basic_rnn_seq2seq(
                            [self.input_data],
                            [self.output_data], 
                            cell,
                            scope='lstm'
                        )


        #see how attention helps improving this model state...

        #was told that we should actually use samples softmax loss
        self.loss = tf.nn.sampled_softmax_loss(
                                    softmax_w, 
                                    softmax_b,
                                    outputs, 
                                    self.output_data,
                                    batch_size,
                                    vocab_size
                )
我现在遇到了tf.nn.sampled_softmax_损失的问题。我在调试方面走了很长的路,不理解Tensorflow的输入约定。我每次都要输入张量列表吗

我得到以下错误:

Traceback (most recent call last):
  File "Model.py", line 76, in <module>
vocab_size=82
  File "Model.py", line 52, in __init__
vocab_size
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/nn.py", line 1104, in sampled_softmax_loss
name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/nn.py", line 913, in _compute_sampled_logits
array_ops.expand_dims(inputs, 1),
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 506, in expand_dims
return _op_def_lib.apply_op("ExpandDims", input=input, dim=dim, name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 411, in apply_op
as_ref=input_arg.is_ref)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 566, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.py", line 179, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.py", line 162, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 332, in make_tensor_proto
_AssertCompatible(values, dtype)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 269, in _AssertCompatible
    raise TypeError("List of Tensors when single Tensor expected")
TypeError: List of Tensors when single Tensor expected

此外,如果我有任何其他错误等,请让我在评论中知道!这是我在tensorflow中使用seq2seq模型的第一个模型,因此非常感谢您的建议

这个特殊的错误是关于传递
输出
,这是一个列表,需要一个张量

该函数返回大小为
[batch\u size x output\u size]
的张量列表作为第一个输出。假设每个输出都是一维的,您希望使用(创建大小为
[seq\u len x batch\u size x 1]
的张量)、最后一个维度(结果为
[seq\u len x batch\u size]
)连接输出列表,并使
输出具有大小
[batch\u size x seq\u len]
,与
self.output\u data
相同

要调试此问题,请使用
print(output.get_shape())
打印张量大小

Model = Model(batch_size=32, 
              seq_length=128, 
              lstm_size=512, 
              num_layers=2, 
              grad_clip=5,
              vocab_size=82
             )