【发布时间】: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