我希望通过tf.nn.bidirectional_rnn()连接最终的前向状态和后向状态输出,并将其用作后续softmax层的输入。但我失败了。 显示错误的代码在这里:
PROFESSOR(NAME,OFFICE,DEPARTMENT,AGE)
COURSE(CNO,TITLE,DEPARTMENT)
ENROLL(CNO,SEMESTER,INSTRUCTOR_NAME,ENROLLMENT)
,输出如下:
import tensorflow as tf
MAX_SENTENCE_LENGTH = 50
class RNNModel(object):
'''RNNModel'''
def __init__(self, config):
self.batch_size = batch_size = config.batch_size
self.num_steps = num_steps = config.num_steps
size = config.hidden_size
label_size = config.label_size
embedding_size = config.embedding_size
self._input_data = tf.placeholder(tf.int32, [None, num_steps], name="input_data")
self._targets = tf.placeholder(tf.float32, [None, label_size], name="targets")
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(size, forget_bias=0.0, state_is_tuple=True)
cell_fw = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * config.num_layers, state_is_tuple=True)
cell_bw = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * config.num_layers, state_is_tuple=True)
self._initial_state_fw = cell_fw.zero_state(batch_size, tf.float16)
self._initial_state_bw = cell_bw.zero_state(batch_size, tf.float16)
with tf.device('/cpu:0'):
self._embedding = embedding = tf.get_variable("embedding", [config.vocab_size, embedding_size],
dtype=tf.float16, trainable=False)
inputs = tf.nn.embedding_lookup(embedding, self._input_data)
# embedding size to hidden size
inputs2 = [tf.squeeze(s, squeeze_dims=[1]) for s in tf.split(1, num_steps, inputs)]
# outputs, state = tf.nn.rnn(cell, inputs2, initial_state=self._initial_state)
outputs, state_fw, state_bw = tf.nn.bidirectional_rnn(
cell_fw, cell_bw, inputs2, initial_state_fw=self._initial_state_fw, initial_state_bw=self._initial_state_bw)
# # use the mean of outputs in all time steps as output
# output = tf.reduce_mean(outputs, 0)
# use the final state as output
c_fw, _ = state_fw[-1]
c_bw, _ = state_bw[-1]
print(c_fw)
output = tf.concat(1, [c_fw, c_bw])
softmax_w = tf.get_variable("softmax_w", [size * 2, label_size], dtype=tf.float16)
softmax_b = tf.get_variable("softmax_b", [label_size], dtype=tf.float16)
self.logits = logits = tf.add(tf.matmul(output, softmax_w), softmax_b, name="predict")
loss = tf.nn.softmax_cross_entropy_with_logits(logits, self._targets)
self._cost = cost = tf.reduce_mean(loss) + 0.5 * config.l2 * tf.nn.l2_loss(softmax_w)
self._final_state_fw = state_fw
self._final_state_bw = state_bw
self._lr = tf.Variable(0.0, dtype=tf.float16, trainable=False)
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars), config.max_grad_norm)
optimizer = tf.train.GradientDescentOptimizer(self.lr)
self._train_op = optimizer.apply_gradients(zip(grads, tvars))
class Config(object):
'''Small Config'''
init_scale = 0.1
learning_rate = 1.0
max_grad_norm = 5
num_layers = 3
num_steps = MAX_SENTENCE_LENGTH
hidden_size = 200
lr_decay = 0.5
batch_size = 20
vocab_size = 1570974
label_size = 22
embedding_size = 300
l2 = 0.001
if __name__ == "__main__":
config = Config()
model = RNNModel(config)
我想知道导致此错误的原因,以及如何实现我的想法。 谢谢你的帮助!