Commit b0f6445e by 20200801011

Jupyter Commit

parent daf4d15a
{
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"import csv\n",
"import time\n",
"import datetime\n",
"import random\n",
"import json\n",
"import re\n",
"import gensim\n",
"\n",
"import warnings\n",
"from collections import Counter\n",
"from math import sqrt\n",
"\n",
"import jieba\n",
"from jieba.analyse import extract_tags\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"_dataSource = \"待抽取关键词文本数据.txt\"\n",
"_stopWordSource = \"cn_stopwords.txt\"\n",
"_sequenceLength = 804\n",
"_embeddingSize = 300\n",
"_hiddenSize = 256\n",
"_dropoutKeepProb = 0.5\n",
"_l2RegLambda = 0.0\n",
"_rate = 0.8\n",
"_epoches = 4\n",
"_evaluateEvery = 100\n",
"_checkpointEvery = 100\n",
"_learningRate = 0.001\n",
"_batchSize = 128\n",
"_numClasses = 10"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def _readData(filePath):\n",
" rowline = [line for line in open(filePath, 'r', encoding='utf8').readlines()]\n",
" labels = [line[:2] for line in rowline]\n",
" news = [line[3:-1] for line in rowline]\n",
" news = [re.sub(\"[A-Za-z0-9\\s+\\.\\-\\!\\[\\]\\……\\<\\>\\/_,$%^*()+\\\"\\']+|[+——!,:~“”‘’;。《》〈?、~@#¥%......&*()]+\", \"\", line) for line in news]\n",
" allNews = [_cutWord(line) for line in news]\n",
" \n",
" return allNews, labels\n",
"\n",
"def _readStopWord(filePath):\n",
" stopWordList = [line[:-1] for line in open(filePath, 'r', encoding='utf8').readlines()]\n",
" stopWordDict = dict(zip(stopWordList, range(len(stopWordList))))\n",
" \n",
" return stopWordDict\n",
"\n",
"def _cutWord(line):\n",
" cut = jieba.cut(line, cut_all = False)\n",
" cut_list = [i for i in cut]\n",
" \n",
" return cut_list\n",
"\n",
"def _getWordEmbedding(words):\n",
" wordvec = gensim.models.KeyedVectors.load_word2vec_format(\"word2vec/sgns.zhihu.bigram\", binary = False)\n",
" vocab = []\n",
" wordEmbedding = []\n",
" \n",
" vocab.append(\"PAD\")\n",
" vocab.append(\"UNK\")\n",
" wordEmbedding.append(np.zeros(_embeddingSize))\n",
" wordEmbedding.append(np.random.randn(_embeddingSize))\n",
" \n",
" for word in words:\n",
" try:\n",
" vector = wordvec.wv[word]\n",
" vocab.append(word)\n",
" wordEmbedding.append(vector)\n",
" except:\n",
" pass\n",
" return vocab, np.array(wordEmbedding)\n",
"\n",
"\n",
"def _genVocabulary(allNews, labels, stopWordDict):\n",
" allWords = [word for news in allNews for word in news]\n",
" \n",
" subWords = [word for word in allWords if word not in stopWordDict]\n",
" wordCount = Counter(subWords)\n",
" sortWordCount = sorted(wordCount.items(), key = lambda x: x[1], reverse = True)\n",
" \n",
" words = [item[0] for item in sortWordCount if item[1] >= 5]\n",
" \n",
" vocab, wordEmbedding = _getWordEmbedding(words)\n",
" \n",
" word2idx = dict(zip(vocab, list(range(len(vocab)))))\n",
" \n",
" uniqueLabel = list(set(labels))\n",
" label2idx = dict(zip(uniqueLabel, list(range(len(uniqueLabel)))))\n",
" labelList = list(range(len(uniqueLabel)))\n",
" \n",
" with open(\"word2vec/wordJson/word2idx.json\", \"w\", encoding = \"utf-8\") as f:\n",
" json.dump(word2idx, f)\n",
" \n",
" with open(\"word2vec/wordJson/label2idx.json\", \"w\", encoding = \"utf-8\") as f:\n",
" json.dump(label2idx, f)\n",
" \n",
" return word2idx, label2idx, wordEmbedding, labelList\n",
"\n",
"\n",
"def _labelToIndex(labels, label2idx):\n",
" labelIds = [label2idx[label] for label in labels]\n",
" return labelIds\n",
"\n",
"def _wordToIndex(allNews, word2idx):\n",
" newsIds = [[word2idx.get(item, word2idx[\"UNK\"]) for item in line] for line in news]\n",
" return newsIds"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Building prefix dict from the default dictionary ...\n",
"Loading model from cache C:\\Users\\jerry\\AppData\\Local\\Temp\\jieba.cache\n",
"Loading model cost 0.637 seconds.\n",
"Prefix dict has been built successfully.\n"
]
}
],
"source": [
"news, labels = _readData(_dataSource)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"stopWordDict = _readStopWord(_stopWordSource)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\jerry\\anaconda3\\envs\\tf\\lib\\site-packages\\ipykernel_launcher.py:34: DeprecationWarning: Call to deprecated `wv` (Attribute will be removed in 4.0.0, use self instead).\n"
]
}
],
"source": [
"word2idx, label2idx, wordEmbedding, labelList = _genVocabulary(news, labels, stopWordDict)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(36090,\n",
" [('PAD', 0),\n",
" ('UNK', 1),\n",
" ('基金', 2),\n",
" ('中', 3),\n",
" ('一个', 4),\n",
" ('月', 5),\n",
" ('年', 6),\n",
" ('中国', 7),\n",
" ('会', 8),\n",
" ('市场', 9)])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(word2idx), list(word2idx.items())[:10]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"(10,\n",
" [('房产', 0),\n",
" ('时尚', 1),\n",
" ('体育', 2),\n",
" ('娱乐', 3),\n",
" ('游戏', 4),\n",
" ('财经', 5),\n",
" ('教育', 6),\n",
" ('时政', 7),\n",
" ('家居', 8),\n",
" ('科技', 9)])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(label2idx), list(label2idx.items())[:10]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"labelIds = _labelToIndex(labels, label2idx)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"newsIds = _wordToIndex(news, word2idx)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def _genTrainEvalData(x, y, word2idx, rate):\n",
" allNews = []\n",
" for news in x:\n",
" if len(news) >= _sequenceLength:\n",
" allNews.append(news[:_sequenceLength])\n",
" else:\n",
" allNews.append(news + [word2idx[\"PAD\"]] * (_sequenceLength - len(news)))\n",
" \n",
" allNews = np.array(allNews)\n",
" y = np.array(y)\n",
" idx = np.arange(len(x))\n",
" np.random.shuffle(idx)\n",
" allNews = allNews[idx,:]\n",
" y = y[idx]\n",
" \n",
" trainIndex = int(len(x) * rate)\n",
" \n",
" trainNews = np.array(allNews[:trainIndex], dtype=\"int64\")\n",
" trainLabels = np.array(y[:trainIndex], dtype=\"float32\")\n",
" \n",
" evalNews = np.array(allNews[trainIndex:], dtype=\"int64\")\n",
" evalLabels = np.array(y[trainIndex:], dtype=\"float32\")\n",
" \n",
" return trainNews, trainLabels, evalNews, evalLabels, idx"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"trainNews, trainLabels, evalNews, evalLabels, idx = _genTrainEvalData(newsIds, labelIds, word2idx, _rate)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def nextBatch(x, y, batchSize):\n",
"\n",
" perm = np.arange(len(x))\n",
" np.random.shuffle(perm)\n",
" x = x[perm]\n",
" y = y[perm]\n",
" \n",
" numBatches = len(x) // batchSize\n",
"\n",
" for i in range(numBatches):\n",
" start = i * batchSize\n",
" end = start + batchSize\n",
" batchX = np.array(x[start: end], dtype=\"int64\")\n",
" batchY = np.array(y[start: end], dtype=\"float32\")\n",
" \n",
" yield batchX, batchY"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# 构建模型\n",
"class BiLSTMAttention(object):\n",
" def __init__(self, wordEmbedding):\n",
" self.inputX = tf.placeholder(tf.int32, [None, _sequenceLength], name=\"inputX\")\n",
" self.inputY = tf.placeholder(tf.int32, [None], name=\"inputY\")\n",
" self.dropoutKeepProb = tf.placeholder(tf.float32, name=\"dropoutKeepProb\")\n",
" \n",
" # define l2 loss\n",
" l2Loss = tf.constant(0.0)\n",
" \n",
" # word embedding\n",
" with tf.name_scope(\"embedding\"):\n",
" self.W = tf.Variable(tf.cast(wordEmbedding, dtype=tf.float32, name=\"word2vec\") ,name=\"W\")\n",
" self.embeddedWords = tf.nn.embedding_lookup(self.W, self.inputX)\n",
" \n",
" # 定义双向LSTM的模型结构\n",
" with tf.name_scope(\"Bi-LSTM\"):\n",
" lstmFwCell = tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.LSTMCell(num_units=_hiddenSize, state_is_tuple=True), output_keep_prob=_dropoutKeepProb)\n",
" lstmBwCell = tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.LSTMCell(num_units=_hiddenSize, state_is_tuple=True), output_keep_prob=_dropoutKeepProb)\n",
" outputs_, self.current_state = tf.nn.bidirectional_dynamic_rnn(lstmFwCell, lstmBwCell, self.embeddedWords, dtype=tf.float32, scope=\"bi-lstm\")\n",
"\n",
" self.embeddedWords = tf.concat(outputs_, 2)\n",
" print(\"lstm outputs shape\", self.embeddedWords.shape)\n",
" \n",
" # 将最后一层Bi-LSTM输出的结果分割成前向和后向的输出\n",
" outputs = tf.split(self.embeddedWords, 2, -1)\n",
" print('outputs shape', outputs[0].shape)\n",
" \n",
" # 在Bi-LSTM+Attention的论文中,将前向和后向的输出相加\n",
" with tf.name_scope(\"Attention\"):\n",
" H = outputs[0] + outputs[1]\n",
" print('attetion output H shape', H.shape)\n",
"\n",
" output = self.attention(H)\n",
" print(\"attetion output shape\", output.shape)\n",
" outputSize = _hiddenSize\n",
" \n",
" # fc layer output\n",
" with tf.name_scope(\"output\"):\n",
" # [256, 1]\n",
" outputW = tf.get_variable(\"outputW\", shape=[outputSize, _numClasses], initializer=tf.contrib.layers.xavier_initializer())\n",
" # [1, ]\n",
" outputB= tf.Variable(tf.constant(0.1, shape=[_numClasses]), name=\"outputB\")\n",
" l2Loss += tf.nn.l2_loss(outputW)\n",
" l2Loss += tf.nn.l2_loss(outputB)\n",
" self.logits = tf.nn.xw_plus_b(output, outputW, outputB, name=\"logits\")\n",
" \n",
" self.predictions = tf.cast(tf.math.argmax(self.logits), tf.float32, name=\"predictions\")\n",
"\n",
" \n",
" # softmax\n",
" with tf.name_scope(\"loss\"):\n",
" losses = tf.nn.softmax_cross_entropy_with_logits(logits=self.logits, labels=tf.cast(tf.reshape(self.inputY, [-1, 1]), dtype=tf.float32))\n",
" self.loss = tf.reduce_mean(losses) + _l2RegLambda * l2Loss\n",
" \n",
" def attention(self, H):\n",
" hiddenSize = _hiddenSize\n",
" \n",
" W = tf.Variable(tf.random_normal([hiddenSize], stddev=0.1))\n",
" print(\"attention W shape\", W.shape)\n",
"\n",
" M = tf.tanh(H)\n",
" \n",
" # e = W*tanh(H)\n",
" newM = tf.matmul(tf.reshape(M, [-1, hiddenSize]), tf.reshape(W, [-1, 1]))\n",
" \n",
" # 对newM做维度转换成[batch_size, time_step]\n",
" restoreM = tf.reshape(newM, [-1, _sequenceLength])\n",
" \n",
" # 用softmax做归一化处理[batch_size, time_step]\n",
" # # 𝛼𝑡=exp(𝑒𝑡') / ∑exp(𝑒𝑡′𝑘)\n",
" self.alpha = tf.nn.softmax(restoreM)\n",
" \n",
" # 利用求得的alpha的值对H进行加权求和,用矩阵运算直接操作\n",
" r = tf.matmul(tf.transpose(H, [0, 2, 1]), tf.reshape(self.alpha, [-1, _sequenceLength, 1]))\n",
" print('attetion output r shape', r.shape)\n",
" \n",
" # 将三维压缩成二维sequeezeR=[batch_size, hidden_size]\n",
" sequeezeR = tf.reshape(r, [-1, hiddenSize])\n",
" \n",
" sentenceRepren = tf.tanh(sequeezeR)\n",
" \n",
" # 对Attention的输出可以做dropout处理\n",
" output = tf.nn.dropout(sentenceRepren, self.dropoutKeepProb)\n",
" \n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"定义各类性能指标\n",
"\"\"\"\n",
"\n",
"def mean(item: list) -> float:\n",
" \"\"\"\n",
" 计算列表中元素的平均值\n",
" :param item: 列表对象\n",
" :return:\n",
" \"\"\"\n",
" res = sum(item) / len(item) if len(item) > 0 else 0\n",
" return res\n",
"\n",
"\n",
"def accuracy(pred_y, true_y):\n",
" \"\"\"\n",
" 计算二类和多类的准确率\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :return:\n",
" \"\"\"\n",
" if isinstance(pred_y[0], list):\n",
" pred_y = [item[0] for item in pred_y]\n",
" corr = 0\n",
" for i in range(len(pred_y)):\n",
" if pred_y[i] == true_y[i]:\n",
" corr += 1\n",
" acc = corr / len(pred_y) if len(pred_y) > 0 else 0\n",
" return acc\n",
"\n",
"\n",
"def binary_precision(pred_y, true_y, positive=1):\n",
" \"\"\"\n",
" 二类的精确率计算\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :param positive: 正例的索引表示\n",
" :return:\n",
" \"\"\"\n",
" corr = 0\n",
" pred_corr = 0\n",
" for i in range(len(pred_y)):\n",
" if pred_y[i] == positive:\n",
" pred_corr += 1\n",
" if pred_y[i] == true_y[i]:\n",
" corr += 1\n",
"\n",
" prec = corr / pred_corr if pred_corr > 0 else 0\n",
" return prec\n",
"\n",
"\n",
"def binary_recall(pred_y, true_y, positive=1):\n",
" \"\"\"\n",
" 二类的召回率\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :param positive: 正例的索引表示\n",
" :return:\n",
" \"\"\"\n",
" corr = 0\n",
" true_corr = 0\n",
" for i in range(len(pred_y)):\n",
" if true_y[i] == positive:\n",
" true_corr += 1\n",
" if pred_y[i] == true_y[i]:\n",
" corr += 1\n",
"\n",
" rec = corr / true_corr if true_corr > 0 else 0\n",
" return rec\n",
"\n",
"\n",
"def binary_f_beta(pred_y, true_y, beta=1.0, positive=1):\n",
" \"\"\"\n",
" 二类的f beta值\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :param beta: beta值\n",
" :param positive: 正例的索引表示\n",
" :return:\n",
" \"\"\"\n",
" precision = binary_precision(pred_y, true_y, positive)\n",
" recall = binary_recall(pred_y, true_y, positive)\n",
" try:\n",
" f_b = (1 + beta * beta) * precision * recall / (beta * beta * precision + recall)\n",
" except:\n",
" f_b = 0\n",
" return f_b\n",
"\n",
"\n",
"def multi_precision(pred_y, true_y, labels):\n",
" \"\"\"\n",
" 多类的精确率\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :param labels: 标签列表\n",
" :return:\n",
" \"\"\"\n",
" if isinstance(pred_y[0], list):\n",
" pred_y = [item[0] for item in pred_y]\n",
"\n",
" precisions = [binary_precision(pred_y, true_y, label) for label in labels]\n",
" prec = mean(precisions)\n",
" return prec\n",
"\n",
"\n",
"def multi_recall(pred_y, true_y, labels):\n",
" \"\"\"\n",
" 多类的召回率\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :param labels: 标签列表\n",
" :return:\n",
" \"\"\"\n",
" if isinstance(pred_y[0], list):\n",
" pred_y = [item[0] for item in pred_y]\n",
"\n",
" recalls = [binary_recall(pred_y, true_y, label) for label in labels]\n",
" rec = mean(recalls)\n",
" return rec\n",
"\n",
"\n",
"def multi_f_beta(pred_y, true_y, labels, beta=1.0):\n",
" \"\"\"\n",
" 多类的f beta值\n",
" :param pred_y: 预测结果\n",
" :param true_y: 真实结果\n",
" :param labels: 标签列表\n",
" :param beta: beta值\n",
" :return:\n",
" \"\"\"\n",
" if isinstance(pred_y[0], list):\n",
" pred_y = [item[0] for item in pred_y]\n",
"\n",
" f_betas = [binary_f_beta(pred_y, true_y, beta, label) for label in labels]\n",
" f_beta = mean(f_betas)\n",
" return f_beta\n",
"\n",
"\n",
"def get_binary_metrics(pred_y, true_y, f_beta=1.0):\n",
" \"\"\"\n",
" 得到二分类的性能指标\n",
" :param pred_y:\n",
" :param true_y:\n",
" :param f_beta:\n",
" :return:\n",
" \"\"\"\n",
" acc = accuracy(pred_y, true_y)\n",
" recall = binary_recall(pred_y, true_y)\n",
" precision = binary_precision(pred_y, true_y)\n",
" f_beta = binary_f_beta(pred_y, true_y, f_beta)\n",
" return acc, recall, precision, f_beta\n",
"\n",
"\n",
"def get_multi_metrics(pred_y, true_y, labels, f_beta=1.0):\n",
" \"\"\"\n",
" 得到多分类的性能指标\n",
" :param pred_y:\n",
" :param true_y:\n",
" :param labels:\n",
" :param f_beta:\n",
" :return:\n",
" \"\"\"\n",
" acc = accuracy(pred_y, true_y)\n",
" recall = multi_recall(pred_y, true_y, labels)\n",
" precision = multi_precision(pred_y, true_y, labels)\n",
" f_beta = multi_f_beta(pred_y, true_y, labels, f_beta)\n",
" return acc, recall, precision, f_beta"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From <ipython-input-15-9bc34f41179b>:18: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
"WARNING:tensorflow:From <ipython-input-15-9bc34f41179b>:20: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API\n",
"WARNING:tensorflow:From C:\\Users\\jerry\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\ops\\rnn.py:464: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
"WARNING:tensorflow:From C:\\Users\\jerry\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\ops\\rnn_cell_impl.py:958: Layer.add_variable (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `layer.add_weight` method instead.\n",
"WARNING:tensorflow:From C:\\Users\\jerry\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\ops\\rnn_cell_impl.py:962: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
"lstm outputs shape (?, 804, 512)\n",
"outputs shape (?, 804, 256)\n",
"attetion output H shape (?, 804, 256)\n",
"attention W shape (256,)\n",
"attetion output r shape (?, 256, 1)\n",
"WARNING:tensorflow:From <ipython-input-15-9bc34f41179b>:84: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
"attetion output shape (?, 256)\n",
"WARNING:tensorflow:\n",
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
"For more information, please see:\n",
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
" * https://github.com/tensorflow/addons\n",
" * https://github.com/tensorflow/io (for I/O related ops)\n",
"If you depend on functionality not listed there, please file an issue.\n",
"\n",
"WARNING:tensorflow:From <ipython-input-15-9bc34f41179b>:53: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"\n",
"Future major versions of TensorFlow will allow gradients to flow\n",
"into the labels input on backprop by default.\n",
"\n",
"See `tf.nn.softmax_cross_entropy_with_logits_v2`.\n",
"\n",
"INFO:tensorflow:Summary name embedding/W:0/grad/hist is illegal; using embedding/W_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name embedding/W:0/grad/sparsity is illegal; using embedding/W_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name bi-lstm/fw/lstm_cell/kernel:0/grad/hist is illegal; using bi-lstm/fw/lstm_cell/kernel_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name bi-lstm/fw/lstm_cell/kernel:0/grad/sparsity is illegal; using bi-lstm/fw/lstm_cell/kernel_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name bi-lstm/fw/lstm_cell/bias:0/grad/hist is illegal; using bi-lstm/fw/lstm_cell/bias_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name bi-lstm/fw/lstm_cell/bias:0/grad/sparsity is illegal; using bi-lstm/fw/lstm_cell/bias_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name bi-lstm/bw/lstm_cell/kernel:0/grad/hist is illegal; using bi-lstm/bw/lstm_cell/kernel_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name bi-lstm/bw/lstm_cell/kernel:0/grad/sparsity is illegal; using bi-lstm/bw/lstm_cell/kernel_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name bi-lstm/bw/lstm_cell/bias:0/grad/hist is illegal; using bi-lstm/bw/lstm_cell/bias_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name bi-lstm/bw/lstm_cell/bias:0/grad/sparsity is illegal; using bi-lstm/bw/lstm_cell/bias_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name Attention/Variable:0/grad/hist is illegal; using Attention/Variable_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name Attention/Variable:0/grad/sparsity is illegal; using Attention/Variable_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name outputW:0/grad/hist is illegal; using outputW_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name outputW:0/grad/sparsity is illegal; using outputW_0/grad/sparsity instead.\n",
"INFO:tensorflow:Summary name output/outputB:0/grad/hist is illegal; using output/outputB_0/grad/hist instead.\n",
"INFO:tensorflow:Summary name output/outputB:0/grad/sparsity is illegal; using output/outputB_0/grad/sparsity instead.\n",
"Writing to C:\\Users\\jerry\\Desktop\\Project\\greedy-ai-recommend-system\\projects\\project1\\summarys\n",
"\n",
"start training model\n",
"train: step: 1, loss: 102.622802734375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 2, loss: 100.93660736083984, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 3, loss: 104.3801040649414, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 4, loss: 111.20909118652344, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 5, loss: 114.00332641601562, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 6, loss: 120.47428894042969, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 7, loss: 136.00888061523438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 8, loss: 147.71319580078125, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 9, loss: 140.00100708007812, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 10, loss: 143.022216796875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 11, loss: 140.45101928710938, acc: 0.1, recall: 0.1, precision: 0.1, f_beta: 0.1\n",
"train: step: 12, loss: 155.61532592773438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 13, loss: 149.02059936523438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 14, loss: 136.99212646484375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 15, loss: 154.5734405517578, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 16, loss: 151.01498413085938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 17, loss: 157.79434204101562, acc: 0.1, recall: 0.03333333333333333, precision: 0.1, f_beta: 0.05\n",
"train: step: 18, loss: 158.71762084960938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 19, loss: 148.79803466796875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 20, loss: 137.8258056640625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 21, loss: 164.97897338867188, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 22, loss: 149.67916870117188, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 23, loss: 175.56698608398438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 24, loss: 153.5785369873047, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 25, loss: 147.6337890625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 26, loss: 158.0564422607422, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 27, loss: 149.98202514648438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 28, loss: 173.33285522460938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 29, loss: 151.09945678710938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 30, loss: 177.244140625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 31, loss: 154.50172424316406, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 32, loss: 157.1009521484375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 33, loss: 148.7662353515625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 34, loss: 165.9762420654297, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 35, loss: 159.74624633789062, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"train: step: 36, loss: 159.61480712890625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 37, loss: 165.6358184814453, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 38, loss: 155.81089782714844, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 39, loss: 163.665283203125, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 40, loss: 154.33572387695312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 41, loss: 173.3270263671875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 42, loss: 155.5060577392578, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 43, loss: 166.86642456054688, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 44, loss: 154.21658325195312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 45, loss: 154.5266571044922, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 46, loss: 146.83583068847656, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 47, loss: 155.21560668945312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 48, loss: 149.0507354736328, acc: 0.1, recall: 0.03333333333333333, precision: 0.05, f_beta: 0.04\n",
"train: step: 49, loss: 155.8804473876953, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 50, loss: 168.0790252685547, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 51, loss: 154.02737426757812, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 52, loss: 150.058349609375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 53, loss: 163.22488403320312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 54, loss: 155.2110137939453, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 55, loss: 157.154541015625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 56, loss: 152.18099975585938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 57, loss: 153.6139373779297, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 58, loss: 152.83905029296875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 59, loss: 165.41372680664062, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 60, loss: 168.79220581054688, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 61, loss: 151.7816162109375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 62, loss: 152.89500427246094, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"start training model\n",
"train: step: 63, loss: 159.3695831298828, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 64, loss: 155.75384521484375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 65, loss: 156.9619140625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 66, loss: 172.59521484375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 67, loss: 164.9029541015625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 68, loss: 159.6607666015625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 69, loss: 161.94857788085938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 70, loss: 151.3285369873047, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 71, loss: 156.85757446289062, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 72, loss: 155.03457641601562, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 73, loss: 145.22360229492188, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 74, loss: 148.1360321044922, acc: 0.1, recall: 0.05, precision: 0.1, f_beta: 0.06666666666666667\n",
"train: step: 75, loss: 163.36990356445312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 76, loss: 176.9347686767578, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 77, loss: 165.07211303710938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 78, loss: 151.34095764160156, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 79, loss: 165.01715087890625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 80, loss: 179.99488830566406, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 81, loss: 159.67709350585938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 82, loss: 137.9711151123047, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 83, loss: 143.71194458007812, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 84, loss: 161.2985076904297, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 85, loss: 159.32644653320312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 86, loss: 171.5772247314453, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 87, loss: 137.81948852539062, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 88, loss: 140.13958740234375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 89, loss: 148.71441650390625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 90, loss: 166.74429321289062, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 91, loss: 146.1815185546875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 92, loss: 163.51376342773438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 93, loss: 150.96951293945312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 94, loss: 152.66864013671875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 95, loss: 156.99627685546875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 96, loss: 151.715576171875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 97, loss: 162.76937866210938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 98, loss: 160.9897918701172, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 99, loss: 162.8643798828125, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 100, loss: 160.99794006347656, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"\n",
"Evaluation:\n",
"2020-08-22T16:21:36.755062, step: 100, loss: 127.72561340332031, acc: 0.013333333333333334,precision: 0.010000000000000002, recall: 0.010000000000000002, f_beta: 0.010000000000000002\n",
"Saved model checkpoint to model/Bi-LSTM-atten/model/my-model-100\n",
"\n",
"train: step: 101, loss: 150.53884887695312, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 102, loss: 143.13125610351562, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 103, loss: 144.27853393554688, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 104, loss: 177.16586303710938, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 105, loss: 165.8618621826172, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 106, loss: 158.39500427246094, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 107, loss: 149.60911560058594, acc: 0.1, recall: 0.05, precision: 0.1, f_beta: 0.06666666666666667\n",
"train: step: 108, loss: 160.65982055664062, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 109, loss: 158.87741088867188, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 110, loss: 155.51370239257812, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 111, loss: 162.40420532226562, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 112, loss: 166.9682159423828, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 113, loss: 161.95556640625, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 114, loss: 172.99569702148438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 115, loss: 164.12286376953125, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 116, loss: 153.69131469726562, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 117, loss: 168.01913452148438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 118, loss: 159.98190307617188, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 119, loss: 145.8074951171875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"train: step: 120, loss: 162.98858642578125, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 121, loss: 145.7887420654297, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 122, loss: 165.45664978027344, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 123, loss: 165.91122436523438, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 124, loss: 152.8857421875, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"start training model\n",
"train: step: 125, loss: 148.57711791992188, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 126, loss: 152.44699096679688, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 127, loss: 160.41847229003906, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 128, loss: 158.38734436035156, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 129, loss: 147.37933349609375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 130, loss: 150.34217834472656, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 131, loss: 163.60740661621094, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 132, loss: 158.37086486816406, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 133, loss: 169.33489990234375, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 134, loss: 152.22756958007812, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 135, loss: 152.2084503173828, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n",
"train: step: 136, loss: 163.14199829101562, acc: 0.0, recall: 0.0, precision: 0.0, f_beta: 0.0\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-17-7e5bd73b51fd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 74\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"start training model\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 75\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mbatchTrain\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mnextBatch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtrainNews\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrainLabels\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_batchSize\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 76\u001b[1;33m \u001b[0mloss\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0macc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprec\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrecall\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf_beta\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtrainStep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatchTrain\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatchTrain\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 77\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 78\u001b[0m \u001b[0mcurrentStep\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mglobal_step\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msess\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobalStep\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-17-7e5bd73b51fd>\u001b[0m in \u001b[0;36mtrainStep\u001b[1;34m(batchX, batchY)\u001b[0m\n\u001b[0;32m 55\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mtrainStep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbatchX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbatchY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 56\u001b[0m \u001b[0mfeed_dict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mlstm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minputX\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mbatchX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlstm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minputY\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mbatchY\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlstm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdropoutKeepProb\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0m_dropoutKeepProb\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 57\u001b[1;33m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msummary\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mloss\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpredictions\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msess\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mtrainOp\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msummaryOp\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobalStep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlstm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloss\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlstm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 58\u001b[0m \u001b[0mtimeStr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0misoformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 59\u001b[0m \u001b[0macc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrecall\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprec\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf_beta\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_multi_metrics\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpred_y\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrue_y\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbatchY\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlabelList\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\client\\session.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m 954\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 955\u001b[0m result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[1;32m--> 956\u001b[1;33m run_metadata_ptr)\n\u001b[0m\u001b[0;32m 957\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 958\u001b[0m \u001b[0mproto_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run\u001b[1;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m 1178\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1179\u001b[0m results = self._do_run(handle, final_targets, final_fetches,\n\u001b[1;32m-> 1180\u001b[1;33m feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[0;32m 1181\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1182\u001b[0m \u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_run\u001b[1;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m 1357\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1358\u001b[0m return self._do_call(_run_fn, feeds, fetches, targets, options,\n\u001b[1;32m-> 1359\u001b[1;33m run_metadata)\n\u001b[0m\u001b[0;32m 1360\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1361\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[1;34m(self, fn, *args)\u001b[0m\n\u001b[0;32m 1363\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1364\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1365\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1366\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1367\u001b[0m \u001b[0mmessage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[1;34m(feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[0;32m 1348\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_extend_graph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1349\u001b[0m return self._call_tf_sessionrun(options, feed_dict, fetch_list,\n\u001b[1;32m-> 1350\u001b[1;33m target_list, run_metadata)\n\u001b[0m\u001b[0;32m 1351\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1352\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\anaconda3\\envs\\tf\\lib\\site-packages\\tensorflow_core\\python\\client\\session.py\u001b[0m in \u001b[0;36m_call_tf_sessionrun\u001b[1;34m(self, options, feed_dict, fetch_list, target_list, run_metadata)\u001b[0m\n\u001b[0;32m 1441\u001b[0m return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict,\n\u001b[0;32m 1442\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1443\u001b[1;33m run_metadata)\n\u001b[0m\u001b[0;32m 1444\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1445\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_call_tf_sessionprun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# train the model\n",
"# 定义计算图\n",
"with tf.Graph().as_default():\n",
"\n",
" session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)\n",
" session_conf.gpu_options.allow_growth=True\n",
" session_conf.gpu_options.per_process_gpu_memory_fraction = 0.9 # 配置gpu占用率 \n",
"\n",
" sess = tf.Session(config=session_conf)\n",
" \n",
" # 定义会话\n",
" with sess.as_default():\n",
" lstm = BiLSTMAttention(wordEmbedding)\n",
" globalStep = tf.Variable(0, name=\"globalStep\", trainable=False)\n",
" optimizer = tf.train.AdamOptimizer(_learningRate)\n",
" gradsAndVars = optimizer.compute_gradients(lstm.loss)\n",
" trainOp = optimizer.apply_gradients(gradsAndVars, global_step=globalStep)\n",
" \n",
" # 用summary绘制tensorBoard\n",
" gradSummaries = []\n",
" for g, v in gradsAndVars:\n",
" if g is not None:\n",
" tf.summary.histogram(\"{}/grad/hist\".format(v.name), g)\n",
" tf.summary.scalar(\"{}/grad/sparsity\".format(v.name), tf.nn.zero_fraction(g))\n",
" \n",
" outDir = os.path.abspath(os.path.join(os.path.curdir, \"summarys\"))\n",
" print(\"Writing to {}\\n\".format(outDir))\n",
" \n",
" lossSummary = tf.summary.scalar(\"loss\", lstm.loss)\n",
" summaryOp = tf.summary.merge_all()\n",
" \n",
" trainSummaryDir = os.path.join(outDir, \"train\")\n",
" trainSummaryWriter = tf.summary.FileWriter(trainSummaryDir, sess.graph)\n",
" \n",
" evalSummaryDir = os.path.join(outDir, \"eval\")\n",
" evalSummaryWriter = tf.summary.FileWriter(evalSummaryDir, sess.graph)\n",
" \n",
" \n",
" # 保存模型的句柄\n",
" saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)\n",
" \n",
" # 保存模型的一种方式,保存为pb文件\n",
" savedModelPath = \"model/Bi-LSTM-atten/savedModel\"\n",
" if os.path.exists(savedModelPath):\n",
" os.rmdir(savedModelPath)\n",
" \n",
" checkpointPath = \"model/Bi-LSTM-atten/model/my-model\"\n",
" if os.path.exists(checkpointPath):\n",
" os.rmdir(checkpointPath)\n",
" \n",
" builder = tf.saved_model.builder.SavedModelBuilder(savedModelPath)\n",
" \n",
" sess.run(tf.global_variables_initializer())\n",
"\n",
" def trainStep(batchX, batchY): \n",
" feed_dict = {lstm.inputX: batchX, lstm.inputY: batchY, lstm.dropoutKeepProb: _dropoutKeepProb}\n",
" _, summary, step, loss, predictions = sess.run([trainOp, summaryOp, globalStep, lstm.loss, lstm.predictions], feed_dict)\n",
" timeStr = datetime.datetime.now().isoformat()\n",
" acc, recall, prec, f_beta = get_multi_metrics(pred_y=predictions, true_y=batchY, labels = labelList)\n",
" trainSummaryWriter.add_summary(summary, step)\n",
" \n",
" return loss, acc, prec, recall, f_beta\n",
"\n",
" def devStep(batchX, batchY):\n",
" feed_dict = {lstm.inputX: batchX, lstm.inputY: batchY, lstm.dropoutKeepProb: 1.0}\n",
" summary, step, loss, predictions = sess.run([summaryOp, globalStep, lstm.loss, lstm.predictions], feed_dict)\n",
" acc, precision, recall, f_beta = get_multi_metrics(pred_y=predictions, true_y=batchY, labels = labelList)\n",
" evalSummaryWriter.add_summary(summary, step)\n",
" \n",
" return loss, acc, precision, recall, f_beta\n",
" \n",
" for i in range(_epoches):\n",
" # 训练模型\n",
" print(\"start training model\")\n",
" for batchTrain in nextBatch(trainNews, trainLabels, _batchSize):\n",
" loss, acc, prec, recall, f_beta = trainStep(batchTrain[0], batchTrain[1])\n",
" \n",
" currentStep = tf.train.global_step(sess, globalStep) \n",
" print(\"train: step: {}, loss: {}, acc: {}, recall: {}, precision: {}, f_beta: {}\".format(currentStep, loss, acc, recall, prec, f_beta))\n",
" # 每隔100个batch,验证一次\n",
" if currentStep % _evaluateEvery == 0:\n",
" print(\"\\nEvaluation:\")\n",
" \n",
" losses = []\n",
" accs = []\n",
" f_betas = []\n",
" precisions = []\n",
" recalls = []\n",
" \n",
" for batchEval in nextBatch(evalNews, evalLabels, _batchSize):\n",
" loss, acc, precision, recall, f_beta = devStep(batchEval[0], batchEval[1])\n",
" losses.append(loss)\n",
" accs.append(acc)\n",
" f_betas.append(f_beta)\n",
" precisions.append(precision)\n",
" recalls.append(recall)\n",
" \n",
" time_str = datetime.datetime.now().isoformat()\n",
" print(\"{}, step: {}, loss: {}, acc: {},precision: {}, recall: {}, f_beta: {}\".format(time_str, currentStep, mean(losses), \n",
" mean(accs), mean(precisions),\n",
" mean(recalls), mean(f_betas)))\n",
" # 每隔100个batch,保存一次模型\n",
" if currentStep % _checkpointEvery == 0:\n",
" # 保存模型的另一种方法,保存checkpoint文件\n",
" path = saver.save(sess, checkpointPath, global_step=currentStep)\n",
" print(\"Saved model checkpoint to {}\\n\".format(path))\n",
" \n",
" inputs = {\"inputX\": tf.saved_model.utils.build_tensor_info(lstm.inputX),\n",
" \"keepProb\": tf.saved_model.utils.build_tensor_info(lstm.dropoutKeepProb)}\n",
"\n",
" outputs = {\"predictions\": tf.saved_model.utils.build_tensor_info(lstm.predictions)}\n",
"\n",
" prediction_signature = tf.saved_model.signature_def_utils.build_signature_def(inputs=inputs, outputs=outputs,\n",
" method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)\n",
" legacy_init_op = tf.group(tf.tables_initializer(), name=\"legacy_init_op\")\n",
" builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING],\n",
" signature_def_map={\"predict\": prediction_signature}, legacy_init_op=legacy_init_op)\n",
"\n",
" builder.save()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "tf",
"language": "python",
"name": "tf"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment