张量流中的Bi-LSTM CRF

时间:2018-08-01 16:02:00

标签: tensorflow lstm crf

我正在尝试在张量流中实现Bi-LSTM CRF,我的代码如下:

def BiRNN(x):
    x=tf.unstack(tf.transpose(x, perm=[1, 0, 2]))
    def rnn_cell():
        cell = tf.nn.rnn_cell.LSTMCell(num_hidden, forget_bias=1,state_is_tuple=True)
        cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=1)
        cell = tf.contrib.rnn.AttentionCellWrapper(cell,20,state_is_tuple=True)
        return cell

    fw_cell = tf.nn.rnn_cell.MultiRNNCell([rnn_cell() for _ in range(num_layers)] , state_is_tuple=True)
    bw_cell = tf.nn.rnn_cell.MultiRNNCell([rnn_cell() for _ in range(num_layers)] , state_is_tuple=True)
    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)

然后我对此使用CRF

logits = BiRNN(layer_flat)
logits = tf.reshape(tf.stack(logits), [-1, timesteps,num_classes])
log_likelihood, transition_params = tf.contrib.crf.crf_log_likelihood(logits, y_true, sqln) #sqln is the sequence length
cost = tf.reduce_mean(-log_likelihood)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(cost)

但是我收到如下错误:

File "train1.py", line 398, in <module>
log_likelihood, transition_params = tf.contrib.crf.crf_log_likelihood(logits, y_true, sqln)
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/contrib/crf/python/ops/crf.py", line 201, in crf_log_likelihood
transition_params)
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/contrib/crf/python/ops/crf.py", line 128, in crf_sequence_score
fn2=_multi_seq_fn)
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/layers/utils.py", line 209, in smart_cond
return fn2()
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/contrib/crf/python/ops/crf.py", line 118, in _multi_seq_fn
unary_scores = crf_unary_score(tag_indices, sequence_lengths, inputs)
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/contrib/crf/python/ops/crf.py", line 231, in crf_unary_score
flattened_tag_indices = array_ops.reshape(offsets + tag_indices, [-1])
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 898, in binary_op_wrapper
y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 932, in convert_to_tensor
as_ref=False)
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1022, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/home/abir/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 866, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype   float32: 'Tensor("Reshape_3:0", shape=(?, 4), dtype=float32)'

我不知道该怎么办。请帮忙。

0 个答案:

没有答案