作家
登录

图解机器学习:神经网络和TensorFlow的文本分类

作者: 来源: 2017-04-24 09:29:37 阅读 我要评论

  • newsgroups_test = fetch_20newsgroups(subset='test', categories=categories) 
  • 练习模型

    在 神经收集的术语里,一次 epoch = 一个向前传递(获得输出的值)和一个所有练习示例的向后传递(更新权重)。

    还记得 tf.Session.run() 办法吗?让我们细心看看它:

    在这篇文┞仿开端的数据流图里,你用到了和操作,然则我们也可以传递一个工作的列表用于运行。在这个神经统??行中将传递两个工作:损耗计算和优化步调。

    1. 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

    是以将要像如许定义占位符:

    1. n_input = total_words # Words in vocab 
    2. n_classes = 3         # Categories: graphics, sci.space and baseball 
    3. input_tensor = tf.placeholder(tf.float32,[None, n_input],name="input"
    4. output_tensor = tf.placeholder(tf.float32,[None, n_classes],name="output"
    1. # Test model 
    2.  index_prediction = tf.argmax(prediction, 1) 
    3.  index_correct = tf.argmax(output_tensor, 1) 
    4.  correct_prediction = tf.equal(index_prediction, index_correct) 
    5.  # Calculate accuracy 
    6.  accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
    7.  total_test_data = len(newsgroups_test.target) 
    8.  batch_x_test,batch_y_test = get_batch(newsgroups_test,0,total_test_data) 
    9.  print("Accuracy:", accuracy.eval({input_tensor: batch_x_test, output_tensor: batch_y_test})) 
    10.  Epoch: 0001 loss= 1133.908114347 
    11.  Epoch: 0002 loss= 329.093700409 
    12.  Epoch: 0003 loss= 111.876660109 
    13.  Epoch: 0004 loss= 72.552971845 
    14.  Epoch: 0005 loss= 16.673050320 
    15.  Epoch: 0006 loss= 16.481995190 
    16.  Epoch: 0007 loss= 4.848220565 
    17.  Epoch: 0008 loss= 0.759822878 
    18.  Epoch: 0009 loss= 0.000000000 
    19.  Epoch: 0010 loss= 0.079848485 
    20.  Optimization Finished! 
    21.  Accuracy: 0.75 

    还将要批量分别你的练习数据:

    • “如不雅为了可以或许 输入而应用占位符,可经由过程应用 tf.placeholder(…, shape=[None, …]) 创建占位符来指定变量批量维度。shape 的 None 元素对应于大年夜小可变的维度。” —  Source

    在测试模型时,我们将用更大年夜的批处理来供给字典,这就是为什么须要定义一个可变的批处理维度。

    get_batches() 函数为我们供给了批处理大年夜小的文本数。如今我们可以运行模型:

    1. training_epochs = 10# Launch the graph 
    2. with tf.Session() as sess: 
    3.     sess.run(init) #inits the variables (normal distribution, remember?) 
    4.     # Training cycle    

        推荐阅读

        iOS AFNetworking框架HTTPS请求配置

      【引自IamOkay的博客】 iOS在Apple公司的强迫请求下,数据传输必须按照ATS(App Transefer Security)条目。关于AFNetworking框架传输HTTPS数据。一.AllowsArbitraryLoads 白名单机制NSAll>>>详细阅读


      本文标题:图解机器学习:神经网络和TensorFlow的文本分类

      地址:http://www.17bianji.com/lsqh/34909.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)