我定义了两个lstm网络,想要实现第一个网络的输出reshape一下后,作为第二个网络的输入,但程序一直报错,这种问题该怎么解决呢

泰伦•洛克  

2017-12-04

我定义了两个lstm网络,想要实现第一个网络的输出reshape一下后,作为第二个网络的输入,但程序一直报错,“ValueError: Variable rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:”这种问题该怎么解决呢

 

关键代码如下:

 

定义网络一:

 

def LSTMnet1(data_holder):
 
    cell_1 = tf.contrib.rnn.BasicLSTMCell(10, state_is_tuple=True,activation=tf.tanh)
    cell_f = [cell_1]
    cells= tf.contrib.rnn.MultiRNNCell(cell_f, state_is_tuple=True)
    output_f, _ = tf.nn.dynamic_rnn(cells, data_holder, dtype=tf.float32)
    output_f=tf.transpose(output_f, perm=[0, 2, 1]) 
    print(output_f.shape)
    return output_f

 

定义网络二:
 

def LSTMnet2(output_f):

    cell_2 = tf.contrib.rnn.BasicLSTMCell(50, state_is_tuple=True,activation=tf.tanh)
    cell_t = [cell_2]
    cells2= tf.contrib.rnn.MultiRNNCell(cell_t, state_is_tuple=True,reuse=True)
    outputs, _ = tf.nn.dynamic_rnn(cells2, output_f, dtype=tf.float32)      #  X input
    
    output_1= tf.contrib.layers.fully_connected(outputs[:, -1], 40, activation_fn=tf.nn.relu)
    y_pred= tf.contrib.layers.fully_connected(output_1, 3, activation_fn=tf.nn.softmax) 
    return y_pred,output_1,outputs,cells2

两个网络联系到一起的代码

    total_num=len(traindata)
    data_holder = tf.placeholder(tf.float32, [None,288,21])
    label_holder = tf.placeholder(tf.int32, [None,3])

    output_f=LSTMnet1(data_holder)
    y_pred,output_1,outputs,cells2=LSTMnet2(output_f)

 

   

关注问题 3人已关注
 写回答 1人已回答