# 手把手教你如何用 TensorFlow 实现基于 DNN 的文本分类

http://t.cn/RXiP3Om

## 关于 TensorFlow

TensorFlow 是谷歌旗下一个开源的机器学习框架。从它的名字就能看出这个框架基本的工作原理：由多维数组构成的张量（tensor）在图（graph）结点之间定向流动（flow），从输入走到输出。

● 一组 tf.Operation 对象，代表运算本身； ● 一组 tf.Tensor 对象，代表被运算的数据。

import tensorflow as tf x = tf.constant([1,3,6]) y = tf.constant([1,1,1]) op = tf.add(x,y)

import tensorflow as tf my_graph = tf.Graph() with my_graph.as_default(): x = tf.constant([1,3,6]) y = tf.constant([1,1,1]) op = tf.add(x,y)

import tensorflow as tf my_graph = tf.Graph() with tf.Session(graph=my_graph) as sess: x = tf.constant([1,3,6]) y = tf.constant([1,1,1]) op = tf.add(x,y)

import tensorflow as tf my_graph = tf.Graph() with tf.Session(graph=my_graph) as sess: x = tf.constant([1,3,6]) y = tf.constant([1,1,1]) op = tf.add(x,y) result = sess.run(fetches=op) print(result) >>> [2 4 7]

## 关于神经网络

http://t.cn/R5MphRp

# Network Parameters n_hidden_1 = 10 # 1st layer number of features n_hidden_2 = 5 # 2nd layer number of features n_input = total_words # Words in vocab n_classes = 3 # Categories: graphics, space and baseball def multilayer_perceptron(input_tensor, weights, biases): layer_1_multiplication = tf.matmul(input_tensor, weights['h1']) layer_1_addition = tf.add(layer_1_multiplication, biases['b1']) layer_1_activation = tf.nn.relu(layer_1_addition) # Hidden layer with RELU activation layer_2_multiplication = tf.matmul(layer_1_activation, weights['h2']) layer_2_addition = tf.add(layer_2_multiplication, biases['b2']) layer_2_activation = tf.nn.relu(layer_2_addition) # Output layer with linear activation out_layer_multiplication = tf.matmul(layer_2_activation, weights['out']) out_layer_addition = out_layer_multiplication + biases['out'] return out_layer_addition

## 神经网络的训练

weights = { 'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), 'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes])) } biases = { 'b1': tf.Variable(tf.random_normal([n_hidden_1])), 'b2': tf.Variable(tf.random_normal([n_hidden_2])), 'out': tf.Variable(tf.random_normal([n_classes])) }

# Construct model prediction = multilayer_perceptron(input_tensor, weights, biases) # Define loss entropy_loss = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=output_tensor) loss = tf.reduce_mean(entropy_loss)

learning_rate = 0.001 # Construct model prediction = multilayer_perceptron(input_tensor, weights, biases) # Define loss entropy_loss = tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=output_tensor) loss = tf.reduce_mean(entropy_loss) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)

## 数据处理

● 为每个单词编码； ● 为每个文本片段创建对应的张量表示，其中以数字 1 代表出现了某个单词，0 表示没有该单词。

import numpy as np #numpy is a package for scientific computing from collections import Counter vocab = Counter() text = "Hi from Brazil" #Get all words for word in text.split(' '): vocab[word]+=1 #Convert words to indexes def get_word_2_index(vocab): word2index = {} for i,word in enumerate(vocab): word2index[word] = i return word2index #Now we have an index word2index = get_word_2_index(vocab) total_words = len(vocab) #This is how we create a numpy array (our matrix) matrix = np.zeros((total_words),dtype=float) #Now we fill the values for word in text.split(): matrix[word2index[word]] += 1 print(matrix) >>> [ 1. 1. 1.]

matrix = np.zeros((total_words),dtype=float) text = "Hi" for word in text.split(): matrix[word2index[word.lower()]] += 1 print(matrix) >>> [ 1. 0. 0.]

y = np.zeros((3),dtype=float) if category == 0: y[0] = 1. # [ 1. 0. 0.] elif category == 1: y[1] = 1. # [ 0. 1. 0.] else: y[2] = 1. # [ 0. 0. 1.]

## 运行模型并预测

http://t.cn/zY6ssrE

from sklearn.datasets import fetch_20newsgroups categories = ["comp.graphics","sci.space","rec.sport.baseball"] newsgroups_train = fetch_20newsgroups(subset='train', categories=categories) newsgroups_test = fetch_20newsgroups(subset='test', categories=categories)

tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=None)

run() 函数中另一个重要的参数是 feed_dict，我们就是通过这个参数传入模型每次处理的输入数据。而为了输入数据，我们又必须先定义 tf.placeholders。

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")

training_epochs = 10 # Launch the graph with tf.Session() as sess: sess.run(init) #inits the variables (normal distribution, remember?) # 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})

# 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

0 条评论

• ### 手把手教你用 TensorFlow 实现文本分类（下）

本篇文章主要记录对之前用神经网络做文本识别的初步优化，进一步将准确率由原来的65%提高到80%，这里优化的几个方面包括： ● 随机打乱训练数据 ● 增加隐层，和...

• ### 如何利用微信监管你的TF训练？

之前回答问题【在机器学习模型的训练期间，大概几十分钟到几小时不等，大家都会在等实验的时候做什么？（http://t.cn/Rl8119m）】的时候，说到可以用微...

• ### Github 项目推荐 | 在 Spark 上实现 TensorFlow 的库 —— Sparkflow

该库是 TensorFlow 在 Spark 上的实现，旨在 Spark 上使用 TensorFlow 提供一个简单的、易于理解的接口。借助 SparkFlow...

• ### 详细介绍tensorflow 神经网络分类模型构建全过程：以文本分类为例

许多开发者向新手建议：如果你想要入门机器学习，就必须先了解一些关键算法的工作原理，然后再开始动手实践。但我不这么认为。 我觉得实践高于理论，新手首先要做的是了解...

• ### tensorflow CNN mnist 示例

版权声明：本文为博主原创文章，未经博主允许不得转载。 https://blog.csdn.net/haluoluo211/article/d...

• ### CTR学习笔记&代码实现2-深度ctr模型 MLP->Wide&Deep

这一篇我们从基础的深度ctr模型谈起。我很喜欢Wide&Deep的框架感觉之后很多改进都可以纳入这个框架中。Wide负责样本中出现的频繁项挖掘，Deep负责样本...

• ### 深度学习_1_Tensorflow_2_数据_文件读取

子线程读取数据 向队列放数据(如每次100个),主线程学习,不用全部数据读取后,开始学习

• ### 深度学习|tensorflow识别手写字体

我们依旧以MNIST手写字体数据集，来看看我们如何使用tensorflow来实现MLP。

• ### win 10 tensorflow笔记1 安装

1.anaconda创建python3.6环境 2.打开py3.6环境的cmd cd进想安装的文件夹 创建文件夹内的虚拟环境：python -m venv ...

• ### 深度学习_1_神经网络_3_验证码识别

​ N ------>[0.01，0.02,0.03.......] 概率 N------->[0，0,0,0,1.......] one-hot编码