在聊天机器人的训练过程中出现张量流错误

时间:2019-04-29 20:22:48

标签: python tensorflow deep-learning spyder training-data

我正在定义测试和交易预测,但遇到一些我不理解的错误

我用张量流做一个python chatbot,我建立了seq2seq模型 并且我定义了训练和测试预测,并在运行代码时收到此错误,直到我开始定义训练和测试预测,错误都出现了

有人可以帮助我

#### Building SEQ2SEQ Model #####\
### placeholders
def model_inputs():
    inputs = tf.placeholder(tf.int32,[None,None],name = 'input')
    targets = tf.placeholder(tf.int32,[None,None],name = 'target')
    lr = tf.placeholder(tf.float32,name = 'learning_rate')
    keep_prob = tf.placeholder(tf.float32,name = 'keep_prob')    
    return inputs,targets,lr,keep_prob

#### preprocess the targets adding sos
def process_targets(targets,word2int,batch_size):
    left_side = tf.fill([batch_size,1],word2int['<SOS>'])
    right_side = tf.strided_slice(targets,[0,0],[batch_size,-1],[1,1])
    preprocessed_targets = tf.concat([left_side,right_side],1)
    return preprocessed_targets

### encoder RNN layer ###
def encoder_rnn(rnn_inputs,rnn_size,num_layers ,keep_prob, sequence_length):
    lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    lstm_dropout = tf.contrib.rnn.DropoutWrapper(lstm,input_keep_prob = keep_prob)
    encoder_cell = tf.contrib.rnn.MultiRNNCell([lstm_dropout] * num_layers)
    _, encoder_state = tf.nn.bidirectional_dynamic_rnn(cell_fw = encoder_cell,
                                                       cell_bw = encoder_cell,
                                                       sequence_length = sequence_length,
                                                       inputs = rnn_inputs,
                                                       dtype = tf.float32)
    return encoder_state


#### decoding thne training set ###
def decode_training_set(encoder_state, decoder_cell,decoder_embedded_input,sequence_length,decoding_scope,output_function,keep_prob,batch_size):
    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, attention_option = 'bahdanau', num_units = decoder_cell.output_size)
    training_decoder_funtion = tf.contrib.seq2seq.attention_decoder_fn_train(encoder_state[0],
                                                                             attention_keys,
                                                                             attention_values,
                                                                             attention_score_function,
                                                                             attention_construct_function,
                                                                             name = "att_dec_train")

    decoder_output,decoder_final_state, decoder_final_context_state = tf.contrib.seq2seq.dynamic_rnn_decoder(decoder_cell,
                                                                                     training_decoder_fuction,
                                                                                     decoder_embedded_input,
                                                                                     sequence_length,
                                                                                     scope = decoding_scope)
    decoder_output_dropout = tf.nn.dropout(decoder_output, keep_prob)
    return output_function(decoder_output_dropout)      




#### decoding test/validation set ###
def decode_training_set(encoder_state, decoder_cell,decoder_embedding_matrix,sos_id,eos_id,maximum_length,num_words,sequence_length,decoding_scope,output_function,keep_prob,batch_size):
    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, attention_option = 'bahdanau', num_units = decoder_cell.output_size)
    test_decoder_funtion = tf.contrib.seq2seq.attention_decoder_fn_inference(output_function,
                                                                             encoder_state[0],
                                                                             attention_keys,
                                                                             attention_values,
                                                                             attention_score_function,
                                                                             attention_construct_function,
                                                                             decoder_embedding_matrix,
                                                                             sos_id,
                                                                             eos_id,
                                                                             maximum_length,
                                                                             num_words,
                                                                             name = "att_dec_inf")

    test_predictions,decoder_final_state, decoder_final_context_state = tf.contrib.seq2seq.dynamic_rnn_decoder(decoder_cell,
                                                                                     training_decoder_fuction,
                                                                                     scope = decoding_scope)

    return test_predictions              


## decoder rnn
def decoder_rnn(decoder_embedded_inputs, decoder_embedding_matrix, encoder_state, num_words, sequence_length, rnn_size, num_layers, word2int, keep_prob, batch_size):
    with tf.variable_scope("decoding") as decoding_scope:
        lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size)
        lstm_dropout = tf.contrib.rnn.DropoutWrapper(lstm,input_keep_prob = keep_prob)
        decoder_cell = tf.contrib.rnn.MultiRNNCell([lstm_dropout] * num_layers)
        weights = tf.truncated_normal_initializer(stdev = 0.1)
        biases = tf.zeros_initializer()
        output_function = lambda x: tf.contrib.layers.fully_connected(x,
                                                                      num_words,
                                                                      None,
                                                                      scope = decoding_scope,
                                                                      weights_initializer = weights,
                                                                      biases_initializer=biases)
        training_predictions = decoder_training_set(encoder_state,
                                                    decoder_cell,
                                                    decoder_embedded_inputs,
                                                    sequence_length,
                                                    decoding_scope,
                                                    output_function,
                                                    keep_prob,
                                                    batch_size)
        decoding_scope.reuse_variables()
        test_predictions = decode_test_set(encoder_state,
                                           decoder_cell,
                                           decoder_embedding_matrix,
                                           word2int['<SOS>'],
                                           word2int['<EOS>'],
                                           sequence_length - 1,
                                           num_words,
                                           decoding_scope,
                                           output_function,
                                           keep_prob,
                                           batch_size)

        return training_predictions,test_predictions

## building 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, questionsword2int):
    encoder_embedded_input = tf.contrib.layers.embed_sequence(inputs, 
                                                              answers_num_words + 1,
                                                              encoder_embedding_size,
                                                              initializer = tf.random_uniform_initializer(0,1))
    encoder_state = encoder_rnn(encoder_embedded_input, rnn_size, num_layers, keep_prob, sequence_length)
    preprocessed_targets = process_targets(targets, questionsword2int, batch_size)
    decoder_embeddings_matrix = tf.variables(tf.random_uniform([questions_num_words + 1, decoder_embedding_size], 0,1))        
    decoder_embedded_inputs = tf.nn.embedding_lookup(decoder_embeddings_matrix, preprocessed_targets)

    training_predictions, test_predictions = decoder_rnn(decoder_embedded_inputs,
                                                         decoder_ebeddings_matrix,
                                                         encoder_state,
                                                         questions_num_words,
                                                         sequence_length,
                                                         rnn_size,
                                                         num_layers,
                                                         questionwords2int,
                                                         keep_prob,
                                                         batch_size)
    return training_predictions,test_predictions

## training ##

## metrics
epochs = 100
batch_size = 64       
rnn_size = 512
num_layers = 3
encoder_embedding_size = 512
decoder_embedding_size = 512
learning_rate = 0.01
learning_rate_decay = 0.9
min_learning_rate = 0.0001
keep_probability = 0.5

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

## load model inputs ##
inputs, targets, lr, keep_prob = model_inputs()

## setting seq length 
sequence_length = tf.placeholder_with_default(25 ,None, name = 'sequence_length')

## getting the shape of the inputs
input_shape = tf.shape(inputs)

##getting trainning and test predictions ##
training_predictions, test_predictions = seq2seq_model(tf.reverse(inputs, [-1]),
                                                                  targets,
                                                                  keep_prob,
                                                                  batch_size,
                                                                  sequence_length,
                                                                  len(answerwords2int),
                                                                  len(questionwords2int),
                                                                  encoder_embedding_size,
                                                                  decoder_embedding_size,
                                                                  rnn_size,
                                                                  num_layers,
                                                                  questionwords2int)


##### and here is the error

questionwords2int)
Traceback (most recent call last):

  File "<ipython-input-45-e10ed1ff7cf8>", line 12, in <module>
    questionwords2int)

  File "<ipython-input-38-1f6f97672855>", line 6, in seq2seq_model
    encoder_state = encoder_rnn(encoder_embedded_input, rnn_size, num_layers, keep_prob, sequence_length)

  File "<ipython-input-21-b9e4092e7b5d>", line 26, in encoder_rnn
    dtype = tf.float32)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\rnn.py", line 350, in bidirectional_dynamic_rnn
    time_major=time_major, scope=fw_scope)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\rnn.py", line 545, in dynamic_rnn
    dtype=dtype)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\rnn.py", line 712, in _dynamic_rnn_loop
    swap_memory=swap_memory)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2626, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2459, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2409, in _BuildLoop
    body_result = body(*packed_vars_for_body)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\rnn.py", line 695, in _time_step
    skip_conditionals=True)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\rnn.py", line 177, in _rnn_step
    new_output, new_state = call_cell()

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\rnn.py", line 683, in <lambda>
    call_cell = lambda: cell(input_t, state)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 655, in __call__
    cur_inp, new_state = cell(cur_inp, cur_state)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 524, in __call__
    output, new_state = self._cell(inputs, state, scope)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 179, in __call__
    concat = _linear([inputs, h], 4 * self._num_units, True, scope=scope)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 747, in _linear
    "weights", [total_arg_size, output_size], dtype=dtype)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 988, in get_variable
    custom_getter=custom_getter)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 890, in get_variable
    custom_getter=custom_getter)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 348, in get_variable
    validate_shape=validate_shape)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 333, in _true_getter
    caching_device=caching_device, validate_shape=validate_shape)

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 639, in _get_single_variable
    name, "".join(traceback.format_list(tb))))

ValueError: Variable bidirectional_rnn/fw/multi_rnn_cell/cell_0/basic_lstm_cell/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\framework\ops.py", line 1264, in __init__
    self._traceback = _extract_stack()
  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\framework\ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\Ahmad Al-Bargouthy\Anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
    op_def=op_def)

0 个答案:

没有答案