我知道这个问题已经问了很多。但是,浏览完不同的帖子后,我似乎仍然无法解决我的错误。
我目前正在建立一个递归神经网络,以便进行情感分析。在这个特定的文件中,我定义了两种不同类型的体系结构(一种具有0个完全连接的层,另一种具有1个完全连接的层),它们都具有不同的功能,因此可以调用诸如train(1,... ),其中1对应于其中一个选项。
我知道总体上来说架构是好的,因为我已经在没有功能的情况下测试了代码,并且一切正常。
但是,当我尝试将代码组织成针对不同体系结构和培训的功能时,出现此错误:
ValueError: Tensor("RNN1/rnn/Const:0", shape=(1,), dtype=int32) must be from the same graph as Tensor("ExpandDims:0", shape=(1,), dtype=int32).
指向行
outputs, final_state = tf.nn.dynamic_rnn(cell, embed, initial_state=initial_state)
如果有人可以帮助我,我将非常感激! 非常感谢您的宝贵时间!
完整代码:
(...)
inputs_ = tf.placeholder(tf.int32, [None, None], name="inputs")
labels_ = tf.placeholder(tf.int32, [None, None], name="labels")
keep_prob = tf.placeholder(tf.float32, name="keep_prob")
n_words=4000
embed_size=300
embedding = tf.Variable(tf.random_uniform((n_words, embed_size), -1, 1))
embed = tf.nn.embedding_lookup(embedding, inputs_)
def rnn_0_fc_layers(inputs_, lstm_size=226, lstm_layers=2, batch_size=600):
with tf.name_scope("RNN1"):
def lstm_cell():
lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
return tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
cell = tf.contrib.rnn.MultiRNNCell([lstm_cell() for i in range(lstm_layers)])
initial_state = cell.zero_state(batch_size, tf.float32)
outputs, final_state = tf.nn.dynamic_rnn(cell, embed, initial_state=initial_state)
pred = tf.contrib.layers.fully_connected(outputs[:,-1], 1, activation_func=tf.nn.relu)
return pred, cell, initial_state, final_state
def rnn_1_fc_layers(inputs_, lstm_size=226, lstm_layers=2, batch_size=600):
with tf.name_scope("RNN1"):
def lstm_cell():
lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
return tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
cell = tf.contrib.rnn.MultiRNNCell([lstm_cell() for i in range(lstm_layers)])
initial_state = cell.zero_state(batch_size, tf.float32)
outputs, final_state = tf.nn.dynamic_rnn(cell, embed, initial_state=initial_state)
dense1= tf.contrib.layers.fully_connected(outputs[:, -1], 256, activation_func=tf.nn.relu)
dense1 = tf.contrib.layers.dropout(dense1, keep_prob)
pred = tf.contrib.layers.fully_connected(dense1, 1, activation_func=tf.nn.relu)
return pred, cell, initial_state, final_state
def get_batch(x, y, batch_size):
'''Create the batches for the training and validation data'''
n_batches = len(x)//batch_size
x, y = x[:n_batches*batch_size], y[:n_batches*batch_size]
for ii in range(0, len(x), batch_size):
yield x[ii:ii+batch_size], y[ii:ii+batch_size]
tf.reset_default_graph()
def train_rnn(model, learning_rate, epochs, batch_size):
tf.reset_default_graph()
learning_rate= learning_rate
epochs=epochs
batch_size=batch_size
inputs_ = tf.placeholder(tf.int32, [None, None], name="inputs")
labels_ = tf.placeholder(tf.int32, [None, None], name="labels")
if model ==0:
pred,_,_,_= rnn_0_fc_layers(inputs_)
else:
model ==1:
pred,_,_,_=rnn_1_fc_layers(inputs_)
with tf.name_scope('cost'):
cost = tf.losses.mean_squared_error(labels_, pred)
tf.summary.scalar('cost', cost)
with tf.name_scope('train'):
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
with tf.name_scope('eval'):
correct_pred = tf.equal(tf.cast(tf.round(pred), tf.int32), labels_)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
merged = tf.summary.merge_all()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
iteration = 0
for e in range(epochs):
state = sess.run(model.initial_state)
for i1, (x,y) in enumerate(get_batch(train_x, train_y, batch_size), 1):
feed = {
inputs_ : x,
labels_ : y[:, None],
keep_prob : 0.5,
model.initial_state : state}
summary, acc, loss, state, _ = sess.run([merged, accuracy, cost, model.final_state, optimizer], feed_dict=feed)
if iteration%5 == 0:
train_acc= []
train_acc.append(acc)
print("Epoch: {}/{}".format(e, epochs),
"Iteration: {}".format(iteration),
"Accuracy: {}".format(train_acc),
"Train Loss: {:.3f}".format(loss))
if iteration%5 == 0:
val_acc = []
val_loss= []
val_state = sess.run(model.cell.zero_state(batch_size, tf.float32))
for x, y in get_batch(val_x, val_y, batch_size):
feed = {inputs_ : x,
labels_ : y[:, None],
keep_prob : 1,
model.initial_state : val_state}
summary, batch_acc, batch_loss, val_state = sess.run([merged, accuracy, cost, model.final_state], feed_dict=feed)
val_acc.append(batch_acc)
val_loss.append(batch_loss)
print("Val acc: {:.3f}".format(np.mean(val_acc)))
print("Val loss: {:.3f}".format(np.mean(val_loss)))
iteration += 1