我想用TensorFlow实现NER任务,所以我将conll2003语料库分开了 到train_set和valid_set,train_set和valid_set具有相同的格式。我在train_set上运行下面的代码(在代码中称为train_reader)
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
from nlpfromscratch.vocab import MultiVocab
from nlpfromscratch.csvreader import CSVReader
from nlpfromscratch.embeds import Embeddings
from nlpfromscratch.mlp import linear
from nlpfromscratch.loss import reg_softmax_loss
from nlpfromscratch.args import parser
from nlpfromscratch.convnet import conv_max
from nlpfromscratch.evaluation import metrics, prf_eval
import os
train_path = "E:\\github\\nlpfromscratch-tf\\data\\conll2003\\train_ner_w3.csv"
valid_path = "E:\\github\\nlpfromscratch-tf\\data\\conll2003\\valid_ner_w3.csv"
log_dir = "E:\\github\\nlpfromscratch-tf\\data\\conll2003\\"
batch_size = 32
vocab_path = "E:\\github\\nlpfromscratch-tf\\data\\conll2003\\vocab.json"
word_dim = 50
feat_dim = 8
n_hidden = 300
lambda_ = 0.001
init = 0.01
max_epochs = 20
train_reader = CSVReader(train_path, batch_size) #read the data
valid_reader = CSVReader(valid_path, batch_size)
seq_len = train_reader.seq_len
num_feats = train_reader.num_feats
chkpt = os.path.join(log_dir, 'nlpfromscratch')
with tf.Graph().as_default():
sess = tf.Session()
global_step = tf.contrib.framework.get_or_create_global_step()
tokens_pl = tf.placeholder(tf.string, (batch_size, seq_len))
features_pl = tf.placeholder(tf.string, (batch_size, seq_len, num_feats))
labels_pl = tf.placeholder(tf.string, (batch_size, ))
multi_vocab = MultiVocab(vocab_path)
label_lookup = multi_vocab.labels.lookup(labels_pl)
metrics(multi_vocab._labels)
embeddings = Embeddings(multi_vocab, word_dim, feat_dim, num_feats)
encoded_input = embeddings.encode(tokens_pl, features_pl)
wx_plus_b = linear(encoded_input, n_hidden, 'hidden', lambda_)
hidden = tf.nn.relu(wx_plus_b)
logits = linear(hidden, multi_vocab.num_classes, 'classify', lambda_)
with tf.name_scope('predict'):
predict = multi_vocab.labels_inv.lookup(
tf.argmax(tf.nn.softmax(logits), axis=1))
loss = reg_softmax_loss(logits, label_lookup)
grad = tf.train.GradientDescentOptimizer(init)
train_op = grad.minimize(loss,global_step = global_step)
summary = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
embeddings.embed_visualization(summary_writer, log_dir, vocab_path)
saver = tf.train.Saver( )
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())
for tokens, features, labels in train_reader.batcher(max_epochs):
feed_dict = {tokens_pl:tokens, features_pl:features, labels_pl:labels}
summ, step_loss, gs, _ =sess.run([summary, loss, global_step, train_op],
feed_dict=feed_dict)
if gs % 10 == 0:
print(gs, train_reader.epoch, max_epochs, step_loss)
它完全没有问题。但是当我将train_set更改为valid_set时。
predict = multi_vocab.labels_inv.lookup(tf.argmax(tf.nn.softmax(logits), axis=1))
for tokens, features, truths, in valid_reader.batcher(1): # only one epoch for validation
feed_dict = {tokens_pl:tokens, features_pl:features}
predictions = sess.run(predict, feed_dict=feed_dict)
它出错并返回:
Traceback (most recent call last):
File "<ipython-input-21-a79489f6e284>", line 1, in <module>
runfile('E:/github/nlpfromscratch-tf/train.py',wdir='E:/github/nlpfromscratch-tf')
File "F:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "F:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/github/nlpfromscratch-tf/train.py", line 97, in <module>
features_pl)
File "E:\github\nlpfromscratch-tf\nlpfromscratch\evaluation.py", line 35, in prf_eval
predictions = sess.run(predict, feed_dict=feed_dict)
File "F:\Anaconda\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
run_metadata_ptr)
File "F:\Anaconda\lib\site-packages\tensorflow\python\client\session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "F:\Anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "F:\Anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1022, in _do_call
return fn(*args)
File "F:\Anaconda\lib\site-packages\tensorflow\python\client\session.py", line 1004, in _run_fn
status, run_metadata)
SystemError: <built-in function TF_Run> returned a result with an error set
首先我认为我的验证代码可能有问题。所以我使用了valid_set进行这样的训练:
for tokens, features, labels in valid_reader.batcher(max_epochs):
feed_dict = {tokens_pl:tokens, features_pl:features, labels_pl:labels}
summ, step_loss, gs, _ =sess.run([summary, loss, global_step, train_op],
feed_dict=feed_dict)
它返回了同样的错误! 所以我很困惑为什么train_set没有问题但是当valid_set的格式相同时会出错。