在Tensorflow中使用多层感知器模型预测文本标签

时间:2018-05-25 18:49:34

标签: python tensorflow neural-network

我遵循教程并可以浏览代码,该代码训练神经网络并评估其准确性。

但我不知道如何在新的单个输入(字符串)上使用训练过的模型来预测其标签。

你能告诉我们如何做到这一点吗?

教程:

https://medium.freecodecamp.org/big-picture-machine-learning-classifying-text-with-neural-networks-and-tensorflow-d94036ac2274

会话代码:

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Training cycle
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(len(newsgroups_train.data)/batch_size)
        # Loop over all batches
        for i in range(total_batch):
            batch_x,batch_y = get_batch(newsgroups_train,i,batch_size)
            # Run optimization op (backprop) and cost op (to get loss value)
            c,_ = sess.run([loss,optimizer], feed_dict={input_tensor: batch_x,output_tensor:batch_y})
            # Compute average loss
            avg_cost += c / total_batch
        # Display logs per epoch step
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1), "loss=", \
                "{:.9f}".format(avg_cost))
    print("Optimization Finished!")

    # Test model
    correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(output_tensor, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    total_test_data = len(newsgroups_test.target)
    batch_x_test,batch_y_test = get_batch(newsgroups_test,0,total_test_data)
    print("Accuracy:", accuracy.eval({input_tensor: batch_x_test, output_tensor: batch_y_test}))

我有一些Python的经验,但基本上没有Tensorflow的经验。

3 个答案:

答案 0 :(得分:3)

Tensorflow使用声明式编程风格。您需要声明您希望它执行的操作,然后才能调用它的runeval函数。

1)如果您想对模型进行一些交互式修补,则需要打开Session处理程序。将第一行替换为:

# Launch the graph
sess = tf.Session()
with sess.as_default():
    .......

原始代码会关闭会话,您无法继续使用已训练的模型。 当您不需要它来释放分配给TF的资源时,不要忘记调用sess.close()

2)现在您必须将要分类的文本转换为数字张量表示。在原始代码中,它使用get_batch()完成。遵循相同的模式。

3)声明结果。您的模型与变量prediction相关联。

4)调用TF。  所以最终的代码如下:

texts = ['''By '8 grey level images' you mean 8 items of 1bit images?
It does work(!), but it doesn't work if you have more than 1bit
in your screen and if the screen intensity is non-linear.''',

'''Wanted: Shareware graphics display program for DOS.
Distribution: usa\nOrganization: University of Notre Dame, Notre Dame
Lines: 16 I need a graphics display program that can take as a parameter the name of
the file to be displayed, then just display that image and then quit.
All of the other graphics display programs come up with a menu first or some other silliness.
This program is going to be run from within another program.  '''       
        ]
# convert texts to tensors
batch = []
for text in texts:
    vector = np.zeros(total_words,dtype=float)
    for word in text.split(' '):
        if word in word2index:
            vector[word2index[word.lower()]] += 1
    batch.append(vector)

x_in = np.array(batch)

# declare new Graph node variable
category = tf.argmax(prediction,1) # choose by maximum score

# run TF
with sess.as_default():
    print("scores:", prediction.eval({input_tensor: x_in}))
    print('class:', category.eval({input_tensor: x_in}))


Out[]:
scores: [[-785.557    -781.1719    105.238686]
         [ 554.584    -532.36383   263.20908 ]]
class: [2 0] 

答案 1 :(得分:2)

需要将文本信息转换为某种数字形式以馈送到神经网络。所以上面的代码实现了以下功能:

创建vocabulary

首先使用训练+文本集形成具有唯一标记的所有单词的字典。在代码中,word2index为给定单词提供唯一标记。例如,word2index['the']会提供令牌输出10

注意: word2index的长度给出了字典总数,并将其用作上述网络中的要素尺寸。

将文本转换为要素

给定一个新的输入字符串,每个单词都使用word2index字典转换为标记,这些标记用于填充特征向量(字典大小),频率为该特定指数的出现。执行此操作的代码部分:

 input = 'Your string ....'

 #form the input feature should be size of the dictionary len(vocab)
 feature = np.zeros(len(vocab),dtype=float)

 #Split each word and then get the token. 
 #Use this token as the index to feature vector to update the frequency of occurrence. 
 for word in input.split(' '):
    feature[word2index[word.lower()]] += 1

上面的代码应该用于在给定文本字符串的情况下生成网络输入。

上面的代码有问题,一个特别的问题是:它没有处理词汇单词(OOV)。你可以找到解决该问题的详细信息here

答案 2 :(得分:2)

首先我们需要将文本转换为数组:

def text_to_vector(text):
    layer = np.zeros(total_words,dtype=float)
    for word in text.split(' '):
        layer[word2index[word.lower()]] += 1

    return layer

# Convert text to vector so we can send it to our model
vector_txt = text_to_vector(text)
# Wrap vector like we do in get_batches()
input_array = np.array([vector_txt])

我们可以保存并加载模型以供重用。我们首先创建一个Saver对象然后保存会话(在训练模型之后):

saver = tf.train.Saver()
... train the model ...
save_path = saver.save(sess, "/tmp/model.ckpt")

在示例模型中,最后一步"步骤"在模型体系结构中(即在multilayer_perceptron方法中完成的最后一件事)是:

'out': tf.Variable(tf.random_normal([n_classes]))

因此,要获得预测,我们得到此数组的最大值索引(预测类):

saver = tf.train.Saver()

with tf.Session() as sess:
    saver.restore(sess, "/tmp/model.ckpt")
    print("Model restored.")

    classification = sess.run(tf.argmax(prediction, 1), feed_dict={input_tensor: input_array})
    print("Predicted category:", classification)

您可以在此处查看整个代码:https://github.com/dmesquita/understanding_tensorflow_nn