? ? ? ? 本周任務(wù):按照標(biāo)準的數(shù)據(jù)集劃分顿天,即訓(xùn)練集:2 測試集:1的比例劃分數(shù)據(jù),測試模型效果蔑担。
? ? ? ? 首先牌废,劃分數(shù)據(jù)集蟀架,按照上述的比例葛虐,源數(shù)據(jù):71532條,訓(xùn)練集拘泞;50000條恨统,測試集:20000條
? ? ? ? 沒有在原始數(shù)據(jù)集劃分叁扫,而是在數(shù)據(jù)遞交中劃分,因為lstm需要循環(huán)訓(xùn)練畜埋,因此需要在源數(shù)據(jù)的基礎(chǔ)上取余操作:
def next_batch(batch_size, step):
? ? return vec_lists[step%(50000//batch_size)], tag_lists[step%(50000//batch_size)]
def test_Data(step):
? ? return vec_list[50000//batch_size + step%(20000//batch_size)], tag_lists[50000//batch_size + step%(20000//batch_size)]
? ? ? ? ? 不斷的在測試集上檢測莫绣,求得相關(guān)正確率:
if __name__ == "__main__":
? ? tag_list = init_Tag()
? ? vec_list = init_Vec()
? ? init_Data()
? ? pred = RNN(x, weights, biases)
? ? cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits=pred))
? ? train_op = tf.train.AdamOptimizer(lr).minimize(cost)
? ? correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
? ? accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
? ? # init= tf.initialize_all_variables() # tf 馬上就要廢棄這種寫法
? ? # 替換成下面的寫法:
? ? init = tf.global_variables_initializer()
? ? list = []
? ? with tf.Session() as sess:
? ? ? ? sess.run(init)
? ? ? ? step = 0
? ? ? ? test_step = 0
? ? ? ? while step * batch_size <= training_iters:
? ? ? ? ? ? batch_xs, batch_ys = next_batch(batch_size, step)
? ? ? ? ? ? batch_xs = np.array(batch_xs)
? ? ? ? ? ? batch_xs = batch_xs.reshape([batch_size, n_steps, n_inputs])
? ? ? ? ? ? sess.run([train_op], feed_dict={
? ? ? ? ? ? ? ? x: batch_xs,
? ? ? ? ? ? ? ? y: batch_ys,
? ? ? ? ? ? })
? ? ? ? ? ? if step == training_iters//batch_size:
? ? ? ? ? ? ? ? while test_step * batch_size < 20000:
? ? ? ? ? ? ? ? ? ? test_batch_xs, test_batch_ys = test_Data(test_step)
? ? ? ? ? ? ? ? ? ? test_batch_xs = np.array(batch_xs)
? ? ? ? ? ? ? ? ? ? test_batch_xs = test_batch_xs.reshape([batch_size, n_steps, n_inputs])
? ? ? ? ? ? ? ? ? ? a = sess.run(accuracy, feed_dict={
? ? ? ? ? ? ? ? ? ? ? ? x: test_batch_xs,
? ? ? ? ? ? ? ? ? ? ? ? y: test_batch_ys,
? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? list.append(a)
? ? ? ? ? ? ? ? ? ? print(a)
? ? ? ? ? ? ? ? ? ? test_step += 1
? ? ? ? ? ? step += 1
? ? ? ? print(" average num = ")
? ? ? ? print(sum(list)/len(list))
? ? ? ? print(" max num = ")
? ? ? ? print(np.max(list))
? ? ? ? ?得出最終的訓(xùn)練效果:
? ? ? ? 平均正確率百分之95.3
? ? ? ? 這樣的一個效果,依舊不是很滿意悠鞍,在經(jīng)過小組討論之后对室,發(fā)現(xiàn)我的詞向量存在問題,因為我們直接使用的是word2vec咖祭,自動訓(xùn)練的代碼掩宜,對中文的向量化效果不是很好,所以我們修改了詞向量庫么翰,改用了搜狗的詞向量庫牺汤,重新生成的詞向量進行訓(xùn)練。