資源簡介
采用深度學習,cnn,rnn 兩種方式對新聞類信息。進行分類預(yù)測。。。。僅供初學者練習使用
代碼片段和文件信息
#?coding:?utf-8
import?tensorflow?as?tf
class?TCNNConfig(object):
????“““CNN配置參數(shù)“““
????embedding_dim?=?64??#?字的特征是64
????seq_length?=?600??#?序列長度????句子長度600
????num_classes?=?10??#?類別數(shù)
????num_filters?=?256??#?卷積核數(shù)目
????kernel_size?=?5??#?卷積核尺寸
????vocab_size?=?5000??#?字數(shù)
????hidden_dim?=?128??#?全連接層神經(jīng)元
????dropout_keep_prob?=?0.5??#?dropout保留比例
????learning_rate?=?1e-3??#?學習率0.001
????batch_size?=?64??#?每批訓練大小
????num_epochs?=?10000??#?總迭代輪次
????print_per_batch?=?100??#?每多少輪輸出一次結(jié)果
????save_per_batch?=?10??#?每多少輪存入tensorboard
class?TextCNN(object):
????“““文本分類,CNN模型“““
????def?__init__(self?config):
????????self.config?=?config
????????#?三個待輸入的數(shù)據(jù)
????????self.input_x?=?tf.placeholder(tf.int32?[None?self.config.seq_length]?name=‘input_x‘)#句子長度(句子數(shù)600)
????????self.input_y?=?tf.placeholder(tf.float32?[None?self.config.num_classes]?name=‘input_y‘)#標簽類別(110)
????????self.keep_prob?=?tf.placeholder(tf.float32?name=‘keep_prob‘)#設(shè)置的dropout
????????self.cnn()
????def?cnn(self):
????????“““CNN模型“““
????????#?字向量映射
????????with?tf.device(‘/cpu:0‘):#5000行64列代表一個字
????????????embedding?=?tf.get_variable(‘embedding‘?[self.config.vocab_size?self.config.embedding_dim])#(500064)5000個字
????????????embedding_inputs?=?tf.nn.embedding_lookup(embedding?self.input_x)?#選取一個張量里面索引對應(yīng)的元素?shape=(句子數(shù)?600?64)
????????with?tf.name_scope(“cnn“):
????????????#?CNN?layer??embedding_inputs?是三維(hw)
????????????conv?=?tf.layers.conv1d(embedding_inputs?self.config.num_filters?self.config.kernel_size?name=‘conv‘)
????????????#1*5的個卷積核,256個核?一維計算?輸入的(??600?64)??????輸出?shape=(??596?256)??(600-5)/1+1=596??
????????????#?global?max?pooling?layer?reduce_max計算張量的各個維度上的元素的最大值??64個句子,每個句子是600?每個字是256維(每個維度是1*5卷積)
????????????gmp?=?tf.reduce_max(conv?reduction_indices=[1]?name=‘gmp‘)#shape=(??256)?按1維去取最大[[12][34]]指定按行列,不指定按均值
????????with?tf.name_scope(“score“):
????????????#?全連接層,后面接dropout以及relu激活gmp輸入的數(shù)據(jù),hidden_dim輸出的維度大小
????????????fc?=?tf.layers.dense(gmp?self.config.hidden_dim?name=‘fc1‘)#shape=(64?128)
????????????fc?=?tf.contrib.layers.dropout(fc?self.keep_prob)#shape=(64?128)
????????????fc?=?tf.nn.relu(fc)#shape=(64?128)
????????????#?分類器
????????????self.logits?=?tf.layers.dense(fc?self.config.num_classes?name=‘fc2‘)#shape=(??10)
????????????self.y_pred_cls?=?tf.argmax(tf.nn.softmax(self.logits)?1)??#?預(yù)測類別?shape=(?)按列取
????????with?tf.name_scope(“optimize“):
????????????#?損失函數(shù),交叉熵
????????????cross_entropy?=?tf.nn.softmax_cross_entropy_with_logits(logits=self.logits?labels=self.input_y)#?shape=(?)
????????????self.loss?=?tf.reduce_mean(cross_entropy)#shape=()?
????????????#?優(yōu)化器
????????????self.optim?=?tf.train.AdamOptimizer(learning_rate=self.config.learning_rate).minimize(self.loss)
????????with?tf.name_scope(“accuracy“):
????????????#?準確率
????????????correct_pred?=?tf.equal(tf.argmax(self.input_y?1)?self.y_pred_cls)#shape=(?)
????????????self.acc?=?tf.reduce_mean(tf.cast(correct_pred?tf.float32))#shape=()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????96??2018-04-25?15:32??text-classification-cnn-rnn\.gitignore
?????文件????6127232??2018-08-30?10:55??text-classification-cnn-rnn\checkpoints\textcnn\best_validation.data-00000-of-00001
?????文件???????1554??2018-08-30?10:55??text-classification-cnn-rnn\checkpoints\textcnn\best_validation.index
?????文件?????474291??2018-08-30?10:55??text-classification-cnn-rnn\checkpoints\textcnn\best_validation.me
?????文件?????????87??2018-08-30?10:55??text-classification-cnn-rnn\checkpoints\textcnn\checkpoint
?????文件???????3518??2018-08-29?18:38??text-classification-cnn-rnn\cnn_model.py
?????文件???27508648??2018-04-30?23:12??text-classification-cnn-rnn\data\cnews\cnews.test.txt
?????文件??130089129??2018-04-30?23:13??text-classification-cnn-rnn\data\cnews\cnews.train.txt
?????文件???11788178??2018-04-30?23:15??text-classification-cnn-rnn\data\cnews\cnews.val.txt
?????文件??????24784??2018-08-29?13:59??text-classification-cnn-rnn\data\cnews\cnews.vocab.txt
?????文件??????19782??2018-04-30?23:12??text-classification-cnn-rnn\data\cnews\cnews.vocab1.txt
?????文件???????5437??2018-08-29?17:02??text-classification-cnn-rnn\data\cnews_loader.py
?????文件??????????0??2018-04-25?15:32??text-classification-cnn-rnn\data\__init__.py
?????文件???????4167??2018-08-29?17:05??text-classification-cnn-rnn\data\__pycache__\cnews_loader.cpython-36.pyc
?????文件????????150??2018-05-07?23:57??text-classification-cnn-rnn\data\__pycache__\__init__.cpython-36.pyc
?????文件???????1745??2018-04-25?15:32??text-classification-cnn-rnn\helper\cnews_group.py
?????文件????????440??2018-04-25?15:32??text-classification-cnn-rnn\helper\copy_data.sh
?????文件??????????0??2018-04-25?15:32??text-classification-cnn-rnn\helper\__init__.py
?????文件?????185646??2018-04-25?15:32??text-classification-cnn-rnn\images\acc_loss.png
?????文件?????137770??2018-04-25?15:32??text-classification-cnn-rnn\images\acc_loss_rnn.png
?????文件??????60095??2018-04-25?15:32??text-classification-cnn-rnn\images\cnn_architecture.png
?????文件??????57085??2018-04-25?15:32??text-classification-cnn-rnn\images\rnn_architecture.png
?????文件????????586??2018-05-12?23:29??text-classification-cnn-rnn\New?Project?#2.wpr
?????文件??????38443??2018-07-14?01:10??text-classification-cnn-rnn\New?Project?#2.wpu
?????文件???????1890??2018-04-25?15:32??text-classification-cnn-rnn\predict.py
?????文件?????????24??2018-04-25?15:32??text-classification-cnn-rnn\requirements.txt
?????文件???????4344??2018-08-31?18:05??text-classification-cnn-rnn\rnn_model.py
?????文件???????7396??2018-08-30?09:48??text-classification-cnn-rnn\run_cnn.py
?????文件??????81109??2018-08-29?16:51??text-classification-cnn-rnn\tensorboard\textcnn\events.out.tfevents.1535532668.LAPTOP-1V3PUQTB
?????文件?????100376??2018-08-29?17:29??text-classification-cnn-rnn\tensorboard\textcnn\events.out.tfevents.1535533727.LAPTOP-1V3PUQTB
............此處省略17個文件信息
評論
共有 條評論