【问题标题】:while calling the seq2seq_model to get train and test prediction im getting this error?在调用 seq2seq_model 来获得训练和测试预测时,我得到了这个错误?
【发布时间】:2020-04-24 06:33:20
【问题描述】:
'''
training_predictions, test_predictions = seq2seq_model(tf.reverse(inputs, [-1]),
                                                       targets,
                                                       keep_prob,
                                                       batch_size,
                                                       sequence_length,
                                                       len(answerword2int),
                                                       len(questionword2int),
                                                       encoding_embedding_size,
                                                       decoding_embedding_size,
                                                       rnn_size,
                                                       num_layers,
                                                       questionword2int)
Traceback (most recent call last):

  File "<ipython-input-28-b2be08c330e7>", line 12, in <module>
    questionword2int)

  File "<ipython-input-22-c4f5411a2dc7>", line 26, in seq2seq_model
    batch_size)

  File "<ipython-input-21-472a41dad669>", line 34, in decoder_rnn
    batch_size)

TypeError: decode_test_set() missing 1 required positional argument: 'batch_size'
'''

'''
Its the following code
#decoding the test/validation set
def decode_test_set(encoder_state, decoder_cell, decoder_embeddings_matrix, sos_id, eos_id, maximum_length, num_words,

sequence_length、decode_scope、output_function、keep_prob、 批量大小): attention_states = tf.zeros([batch_size, 1, decoder_cell.output_size]) attention_keys, attention_values, attention_score_function, attention_construct_function = tf.contrib.seq2seq.prepare_attention(attention_states, 注意选项='bahdanau',num_units = decoder_cell.output_size) test_decoder_function = tf.contrib.seq2seq.attention_decoder_fn_inference(output_function, 编码器状态[0], 注意键, 注意值, attention_score_function, attention_construct_function, 解码器嵌入矩阵, sos_id, eos_id, 最大长度, num_words, 名称=“attn_dec_inf”) test_prediction, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder(decoder_cell, test_decoder_function, 范围=解码范围)

    return test_prediction


#creating the decoder rnn
def decoder_rnn(decoder_embedded_input, decoder_embeddings_matrix, encoder_state, num_words, sequence_length, rnn_size, num_layers,

word2int、keep_prob、batch_size): 使用 tf.variable_scope("decoding") 作为解码范围: lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size) lstm_dropout = tf.contrib.rnn.DropoutWrapper(lstm, input_keep_prob = keep_prob) 解码器单元 = tf.contrib.rnn.MultiRNNCell([lstm_dropout] * num_layers) 权重 = tf.truncated_normal_initializer(stddev = 0.1) 偏差 = tf.zeros_initializer() output_function = lambda x: tf.contrib.layers.fully_connected(x, num_words, 没有任何, 范围=解码范围, weights_initializer = 权重, biases_initializer = 偏差)

        training_predictions = decode_training_set(encoder_state,
                                                   decoder_cell,
                                                   decoder_embedded_input,
                                                   sequence_length,
                                                   decoding_scope,
                                                   output_function,
                                                   keep_prob,
                                                   batch_size)
        decoding_scope.reuse_variables()
        test_prediction = decode_test_set(encoder_state,
                                          decoder_cell,
                                          decoder_embeddings_matrix,
                                          word2int['<SOS>'],
                                          word2int['<EOS>'],
                                          sequence_length - 1,
                                          num_words,
                                          decoding_scope,
                                          output_function,
                                          keep_prob,
                                          batch_size)
    return training_predictions, test_prediction

#building the seq2seq model

def seq2seq_model(inputs, targets, keep_prob, batch_size, sequence_length, answers_num_words, questions_num_words,

encoder_embedding_size、decoder_embedding_size、rnn_size、num_layers、 questionwords2int): encoder_embedded_input = tf.contrib.layers.embed_sequence(输入, answers_num_words + 1, 编码器嵌入尺寸, 初始化器 = tf.random_uniform_initializer(0,1)) 编码器状态=编码器RNN(编码器嵌入输入, rnn_size, 层数, 保持概率, 序列长度) preprocessed_targets = preprocess_targets(目标, questionwords2int, 批量大小) decoder_embeddings_matrix = tf.Variable(tf.random_uniform([questions_num_words + 1, 解码器嵌入尺寸],0 ,1)) decoder_embedded_input = tf.nn.embedding_lookup(decoder_embeddings_matrix, 预处理目标) training_predictions, test_predictions = decoder_rnn(decoder_embedded_input, 解码器嵌入矩阵, 编码器状态, questions_num_words, 序列长度, rnn_size, 层数, questionword2int, 保持概率, 批量大小)

    return training_predictions, test_predictions


#training the seq2seq modal
#setting up the hyperparameter

epochs = 100
batch_size = 64
rnn_size = 512
num_layers = 3
encoding_embedding_size = 512
decoding_embedding_size = 512
learning_rate = 0.01
learning_rate_decay = 0.9
min_learning_rate = 0.0001
keep_probability = 0.5

#defining a session

tf.reset_default_graph()
session = tf.InteractiveSession()

#loading the modal input

inputs, targets, lr, keep_prob = modal_input()

#setting the sequence length

sequence_length = tf.placeholder_with_default(25, None, name = 'sequence_length')

#getting the shape of input tensor

input_shape = tf.shape(inputs)

#getting the training and test predivtions
training_predictions, test_predictions = seq2seq_model(tf.reverse(inputs, [-1]),
                                                       targets,
                                                       keep_prob,
                                                       batch_size,
                                                       sequence_length,
                                                       len(answerword2int),
                                                       len(questionword2int),
                                                       encoding_embedding_size,
                                                       decoding_embedding_size,
                                                       rnn_size,
                                                       num_layers,
                                                       questionword2int)
'''

【问题讨论】:

  • 请尝试更好地编辑和格式化代码。
  • 对不起,我是新人:)

标签: tensorflow machine-learning deep-learning chatbot seq2seq


【解决方案1】:

这次请慢慢重新阅读您的错误。 您会看到您的函数 decode_test_set() 定义定义了 12 个参数。但是,在预测期间调用它时,您只提供了 11 个值,而缺少最后一个值 batch_size

另外,为了以后的问题,请正确格式化您的问题,以便于阅读,社区可以更好地帮助您。

【讨论】:

  • 是的,现在它正在工作。谢谢 在教程中,他们只通过了 11。下次我会继续处理格式化,对不起
猜你喜欢
  • 2021-04-03
  • 2017-08-16
  • 2019-10-10
  • 1970-01-01
  • 2020-09-09
  • 2021-06-11
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
相关资源
最近更新 更多