【问题标题】:Predict label of text with multi-layered perceptron model in Tensorflow在 Tensorflow 中使用多层感知器模型预测文本标签
【发布时间】:2018-11-05 05:02:48
【问题描述】:

我正在学习一个教程,并且可以通过代码来训练神经网络并评估其准确性。

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

您能建议如何做到这一点吗?

教程:

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 经验。

【问题讨论】:

    标签: python tensorflow neural-network


    【解决方案1】:

    Tensorflow 使用声明式编程。您需要声明您希望它做什么,然后才调用它的 runeval 函数。

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

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

    原代码关闭会话,你不能继续使用训练好的模型了。 不需要时不要忘记调用sess.close()释放分配给TF的资源。

    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] 
    

    【讨论】:

      【解决方案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

      【讨论】:

        【解决方案3】:

        需要将文本信息转换为某种数字形式才能馈送到神经网络。因此,上面的代码执行以下操作来实现:

        创建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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-10-05
          • 2017-04-12
          • 2017-01-06
          • 2023-03-03
          • 2017-06-28
          • 1970-01-01
          • 2019-09-28
          • 1970-01-01
          相关资源
          最近更新 更多