从张量流模型得到结果

时间:2017-11-28 14:28:17

标签: tensorflow neural-network

我是神经网络的新手

我根据本教程创建了一个简单的网络。它经过培训,可以在三个类别中澄清文本:  运动,图形和空间 https://medium.freecodecamp.org/big-picture-machine-learning-classifying-text-with-neural-networks-and-tensorflow-d94036ac2274

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)
    print("total_batch",total_batch)
    # 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,cc = sess.run([loss,optimizer], feed_dict={input_tensor: batch_x,output_tensor:batch_y})
        print("C = ", c)
        print("Cc = ", cc)
        # Compute average loss
        avg_cost += c / total_batch
    # Display logs per epoch step
    if epoch % display_step == 0:
        print("inpt ten =", batch_y)
        print("Epoch:", '%04d' % (epoch+1), "loss=", \
            "{:.9f}".format(avg_cost))

我想知道在训练之后我可以用我自己的文本提供这个模型并得到结果

由于

1 个答案:

答案 0 :(得分:1)

像janu777说的那样,我们可以保存并加载模型以供重用。我们首先创建一个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