我试图使用带有GRU单元格的RNN来生成文本,但是当我在文本上调用RNN函数来生成文本时,我收到错误。
Traceback (most recent call last):
File "trump.py", line 93, in <module>
var = RNN(emb(new[-3:]), weights, biases)
File "trump.py", line 58, in RNN
outputs, states = tf.nn.static_rnn(stack, lists, dtype = tf.float32)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn.py", line 1212, in static_rnn
(output, state) = call_cell()
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn.py", line 1199, in <lambda>
call_cell = lambda: cell(input_, state)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\layers\base.py", line 441, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 916, in call
cur_inp, new_state = cell(cur_inp, cur_state)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 752, in __call__
output, new_state = self._cell(inputs, state, scope)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 180, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\layers\base.py", line 441, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 295, in call
self._kernel_initializer))
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1017, in _linear
initializer=kernel_initializer)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1065, in get_variabl
e
use_resource=use_resource, custom_getter=custom_getter)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 962, in get_variable
use_resource=use_resource, custom_getter=custom_getter)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 360, in get_variable
validate_shape=validate_shape, use_resource=use_resource)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1405, in wrapped_cus
tom_getter
*args, **kwargs)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in _rnn_get_vari
able
variable = getter(*args, **kwargs)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in _rnn_get_vari
able
variable = getter(*args, **kwargs)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 352, in _true_getter
use_resource=use_resource)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 664, in _get_single_
variable
name, "".join(traceback.format_list(tb))))
ValueError: Variable rnn/multi_rnn_cell/cell_0/gru_cell/gates/kernel already exists, disallowed. Did you mean to set reuse=True
in VarScope? Originally defined at:
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
self._traceback = _extract_stack()
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\Abhinav Kumar\Miniconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_
op
op_def=op_def)
从RNN功能开始的代码如下:
# -*- coding: UTF-8 -*-
import tensorflow as tf
import os
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
wordp = ""
with open("test.txt", encoding = "UTF-8") as file:
reader = file.readlines()
for row in reader:
wordp += row + " "
words = set(wordp.split())
dictionary = {i: j for i, j in enumerate(words)}
reverse = {j: i for i, j in enumerate(words)}
training_epochs = 1
learning_rate = .001
input_dim = len(words)
output_dim = 1
num_units = 300
num_units1 = 300
output_prob = .5
train = []
for line in reader:
line = line.split()
for i in range(len(line)):
x = line[i]
line[i] = np.zeros(len(dictionary))
line[i][reverse[x]] = 1
train.append(line)
def emb(param):
train = []
global dictionary
global reverse
for i in range(len(param)):
x = param[i]
param[i] = np.zeros(len(dictionary))
param[i][reverse[x]] = 1
return np.asarray(param, dtype = np.float32)
def RNN(x, weights, biases):
cells = []
LSTM = tf.contrib.rnn.GRUCell(num_units, activation=tf.nn.relu)
LSTM = tf.contrib.rnn.DropoutWrapper(LSTM, output_keep_prob=output_prob)
cells.append(LSTM)
LSTM1 = tf.contrib.rnn.GRUCell(num_units1, activation=tf.nn.relu)
LSTM1 = tf.contrib.rnn.DropoutWrapper(LSTM1, output_keep_prob=output_prob)
cells.append(LSTM1)
stack = tf.contrib.rnn.MultiRNNCell(cells, state_is_tuple=True)
lists = tf.split(x, num_or_size_splits = 3, axis = 0)
outputs, states = tf.nn.static_rnn(stack, lists, dtype = tf.float32)
return tf.matmul(outputs[-1], weights)+biases
weights = tf.Variable(tf.random_normal([num_units,input_dim]))
biases = tf.Variable(tf.random_normal([input_dim]))
x_vals = tf.placeholder(tf.float32, shape = [None, input_dim])
y_vals = tf.placeholder(tf.float32, shape = [input_dim])
pred = RNN(x_vals,weights,biases)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y_vals))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for j in range(training_epochs):
accu = 0
c = 0
count = 0
for line in train:
i = 0
while i+4 < len(line):
_, acc, cost = sess.run([optimizer, accuracy, cross_entropy], feed_dict={x_vals: line[i:i+3], y_vals: line[i+3]})
i+=1
accu += acc
count += 1
c+= cost
print("Accuracy")
print(accu/count)
print("Cost")
print(c/count)
new = ["badly", "broken", "big"]
while len(" ".join(new)) <= 140:
var = RNN(emb(new[-3:]), weights, biases)
sess.run(var)
new.append(dictionary[var.index(max(var))])
训练部分工作正常,但是当我尝试第二次调用RNN功能时出现问题。我尝试在不同的变量范围内将RNN函数内外的各种变量放在一起,但后来我得到一个错误,说GRU子变量没有被初始化。
感谢您的帮助!
答案 0 :(得分:0)
您的代码中存在以下几个问题:
accuracy
未定义(我假设您发布了脚本的中间版本)
RNN
是生成图表的功能(LSTM
,LSTM1
等是节点。
它通常用于训练,但是第二次调用测试时在图中创建新节点,因此出现此错误消息。
我已经内联RNN
这样的功能:
weights = tf.Variable(tf.random_normal([num_units,input_dim]))
biases = tf.Variable(tf.random_normal([input_dim]))
x_vals = tf.placeholder(tf.float32, shape = [None, input_dim])
y_vals = tf.placeholder(tf.float32, shape = [input_dim])
cells = []
LSTM = tf.contrib.rnn.GRUCell(num_units, activation=tf.nn.relu)
LSTM = tf.contrib.rnn.DropoutWrapper(LSTM, output_keep_prob=output_prob)
cells.append(LSTM)
LSTM1 = tf.contrib.rnn.GRUCell(num_units1, activation=tf.nn.relu)
LSTM1 = tf.contrib.rnn.DropoutWrapper(LSTM1, output_keep_prob=output_prob)
cells.append(LSTM1)
stack = tf.contrib.rnn.MultiRNNCell(cells, state_is_tuple=True)
lists = tf.split(x_vals, num_or_size_splits = 3, axis = 0)
outputs, states = tf.nn.static_rnn(stack, lists, dtype = tf.float32)
pred = tf.matmul(outputs[-1], weights)+biases
并将测试目标更改为tf.argmax(pred, dimension=1)
:
while len(" ".join(new)) <= 140:
test_input = emb(new[-3:])
max_word = tf.argmax(pred, dimension=1)[0]
word_index = sess.run(max_word, feed_dict={x_vals: test_input})
print dictionary[word_index]
它为我生成了一些单词。我没有大的培训档案,所以输出对我来说毫无意义,但希望在你的情况下会更好。