使用双向 lstm 在张量流中进行序列标记任务

sequence tagging task in tensorflow using bidirectional lstm

我对 NER 的序列标记不感兴趣。我按照代码“https://github.com/monikkinom/ner-lstm/blob/master/model.py”使我的模型如下所示:

X = tf.placeholder(tf.float32, shape=[None, timesteps , num_input])
Y = tf.placeholder("float", [None, timesteps, num_classes])
y_true = tf.reshape(tf.stack(Y), [-1, num_classes])

输入是,
X: (batch_size,max_sent_length,word_embed_dim)

Y: (batch_size,max_sent_length,number_of_labels)

然后我将值传递给双向 LSTM 单元:

def BiRNN(x):
    x=tf.unstack(tf.transpose(x, perm=[1, 0, 2]))

    def rnn_cell():
        cell = tf.nn.rnn_cell.LSTMCell(rnn_size, forget_bias=1,state_is_tuple=True)
        return cell

    fw_cell=rnn_cell()
    bw_cell=rnn_cell()
    output,_, _ = tf.nn.static_bidirectional_rnn(fw_cell, bw_cell,x, dtype=tf.float32)
    weight, bias = weight_and_bias(2 * rnn_size, num_classes)
    output = tf.reshape(tf.transpose(tf.stack(output), perm=[1, 0, 2]), [-1, 2 * rnn_size])
return (tf.matmul(output, weight) + bias)

其中,rnn_size = 128

然后我在做下面的计算:

logits = BiRNN(X)
logits = tf.reshape(tf.stack(logits), [-1, timesteps,num_classes])
prediction = tf.reshape(logits, [-1, num_classes])
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y_true))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(cost)

我用了,batch_size = 64 和 30 个 epoch。
但是在我的模型中每次只检测到一个标签。我无法指出我的代码中的问题。请帮忙。

请检查张量的维度y_true、输出(两个地方)、logits和预测,并检查它是否符合您的预期。