dnn文本分类
簡介
文本分類任務根據給定一條文本的內容,判斷該文本所屬的類別,是自然語言處理領域的一項重要的基礎任務。具體的,本任務是對文本quey進行分類,任務流程如下:運行
訓練: sh +x train.sh 預測: python infer.py輸入/輸出
輸入樣本:label text(分詞后)
0 龍脈溫泉 住宿 1 龍馬 機場 飛機 2 龍里 旅游 其中,label 0,1和2分別代表:酒店,票務和住宿。預估樣本:
2 0.0002 0.0001 0.9997 港澳 7 日 自助游 label prob text 其中,label為概率最大的類別,即2旅游,中間三個數值為每個類別的概率。DNN 模型
DNN 模型結構入下圖所示:?
圖1. 本例中的 DNN 文本分類模型 在 PaddlePaddle 實現該 DNN 結構的代碼見 network_conf.py 中的 fc_net 函數,模型主要分為如下幾個部分:- 詞向量層:為了更好地表示不同詞之間語義上的關系,首先將詞語轉化為固定維度的向量。訓練完成后,詞與詞語義上的相似程度可以用它們的詞向量之間的距離來表示,語義上越相似,距離越近。關于詞向量的更多信息請參考PaddleBook中的詞向量一節。
- 最大池化層:最大池化在時間序列上進行,池化過程消除了不同語料樣本在單詞數量多少上的差異,并提煉出詞向量中每一下標位置上的最大值。經過池化后,詞向量層輸出的向量序列被轉化為一條固定維度的向量。例如,假設最大池化前向量的序列為[[2,3,5],[7,3,6],[1,4,0]],則最大池化的結果為:[7,4,6]。
- 全連接隱層:經過最大池化后的向量被送入兩個連續的隱層,隱層之間為全連接結構。
- 輸出層:輸出層的神經元數量和樣本的類別數一致,例如在二分類問題中,輸出層會有2個神經元。通過Softmax激活函數,輸出結果是一個歸一化的概率分布,和為1,因此第$i$個神經元的輸出就可以認為是樣本屬于第$i$類的預測概率。
源碼:
import sys import math import gzipfrom paddle.v2.layer import parse_network import paddle.v2 as paddle__all__ = ["fc_net", "convolution_net"]def fc_net(dict_dim,class_num,emb_dim=28,hidden_layer_sizes=[28, 8],is_infer=False):""" define the topology of the dnn network:param dict_dim: size of word dictionary:type input_dim: int:params class_num: number of instance class:type class_num: int:params emb_dim: embedding vector dimension:type emb_dim: int""" # define the input layersdata = paddle.layer.data("word",paddle.data_type.integer_value_sequence(dict_dim))if not is_infer:lbl = paddle.layer.data("label",paddle.data_type.integer_value(class_num))# define the embedding layeremb = paddle.layer.embedding(input=data, size=emb_dim)# max pooling to reduce the input sequence into a vector (non-sequence)seq_pool = paddle.layer.pooling(input=emb, pooling_type=paddle.pooling.Max())for idx, hidden_size in enumerate(hidden_layer_sizes):hidden_init_std = 1.0 / math.sqrt(hidden_size)hidden = paddle.layer.fc(input=hidden if idx else seq_pool,size=hidden_size,act=paddle.activation.Tanh(),param_attr=paddle.attr.Param(initial_std=hidden_init_std))prob = paddle.layer.fc(input=hidden,size=class_num,act=paddle.activation.Softmax(),param_attr=paddle.attr.Param(initial_std=1.0 / math.sqrt(class_num)))if is_infer:return probelse:return paddle.layer.classification_cost(input=prob, label=lbl), prob, lbldef convolution_net(dict_dim,class_dim=2,emb_dim=28,hid_dim=128,is_infer=False):""" cnn network definition:param dict_dim: size of word dictionary:type input_dim: int:params class_dim: number of instance class:type class_dim: int:params emb_dim: embedding vector dimension:type emb_dim: int:params hid_dim: number of same size convolution kernels:type hid_dim: int""" # input layersdata = paddle.layer.data("word",paddle.data_type.integer_value_sequence(dict_dim))lbl = paddle.layer.data("label", paddle.data_type.integer_value(class_dim))# embedding layeremb = paddle.layer.embedding(input=data, size=emb_dim)# convolution layers with max poolingconv_3 = paddle.networks.sequence_conv_pool(input=emb, context_len=3, hidden_size=hid_dim)conv_4 = paddle.networks.sequence_conv_pool(input=emb, context_len=4, hidden_size=hid_dim)# fc and output layerprob = paddle.layer.fc(input=[conv_3, conv_4], size=class_dim, act=paddle.activation.Softmax())if is_infer:return probelse:cost = paddle.layer.classification_cost(input=prob, label=lbl)return cost, prob, lbl?
訓練結果如下圖:
預估結果:
?
轉載于:https://www.cnblogs.com/rongyux/p/7295571.html
總結
- 上一篇: Spring中配置数据源的4种形式
- 下一篇: 安天365第二期线上交流