练习模型
在 神经收集的术语里,一次 epoch = 一个向前传递(获得输出的值)和一个所有练习示例的向后传递(更新权重)。
还记得 tf.Session.run() 办法吗?让我们细心看看它:
在这篇文┞仿开端的数据流图里,你用到了和操作,然则我们也可以传递一个工作的列表用于运行。在这个神经统??行中将传递两个工作:损耗计算和优化步调。
- tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=http://ai.51cto.com/art/201704/None)
feed_dict 参数是我们为每步运行所输入的数据。为了传递这个数据,我们须要定义tf.placeholders(供给给 feed_dict)
正如 TensorFlow 文档中说的:
- “占位符的存在只作为输入的目标,它不须要初始化,也不包含数据。” — Source
是以将要像如许定义占位符:
- n_input = total_words # Words in vocab
- n_classes = 3 # Categories: graphics, sci.space and baseball
- input_tensor = tf.placeholder(tf.float32,[None, n_input],name="input")
- output_tensor = tf.placeholder(tf.float32,[None, n_classes],name="output")
- # Test model
- index_prediction = tf.argmax(prediction, 1)
- index_correct = tf.argmax(output_tensor, 1)
- correct_prediction = tf.equal(index_prediction, index_correct)
- # 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}))
- Epoch: 0001 loss= 1133.908114347
- Epoch: 0002 loss= 329.093700409
- Epoch: 0003 loss= 111.876660109
- Epoch: 0004 loss= 72.552971845
- Epoch: 0005 loss= 16.673050320
- Epoch: 0006 loss= 16.481995190
- Epoch: 0007 loss= 4.848220565
- Epoch: 0008 loss= 0.759822878
- Epoch: 0009 loss= 0.000000000
- Epoch: 0010 loss= 0.079848485
- Optimization Finished!
- Accuracy: 0.75
还将要批量分别你的练习数据:
- “如不雅为了可以或许 输入而应用占位符,可经由过程应用 tf.placeholder(…, shape=[None, …]) 创建占位符来指定变量批量维度。shape 的 None 元素对应于大年夜小可变的维度。” — Source
在测试模型时,我们将用更大年夜的批处理来供给字典,这就是为什么须要定义一个可变的批处理维度。
get_batches() 函数为我们供给了批处理大年夜小的文本数。如今我们可以运行模型:
- training_epochs = 10# Launch the graph
- with tf.Session() as sess:
- sess.run(init) #inits the variables (normal distribution, remember?)
- # Training cycle
推荐阅读
【引自IamOkay的博客】 iOS在Apple公司的强迫请求下,数据传输必须按照ATS(App Transefer Security)条目。关于AFNetworking框架传输HTTPS数据。一.AllowsArbitraryLoads 白名单机制NSAll>>>详细阅读
本文标题:图解机器学习:神经网络和TensorFlow的文本分类
地址:http://www.17bianji.com/lsqh/34909.html
1/2 1