'''
rnn 實現文字分類
文字生成
'''import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
imdb = keras.datasets.imdb
vocab_size = 10000
index_from = 3
#載入資料
(train_data,train_labels),(test_data,test_labels) = imdb.load_data(num_words = vocab_size,index_from = index_from)
#載入詞表
word_index = imdb.get_word_index()
word_index = #把所有的id往上偏移 3
#幾個特殊字元
word_index[''] = 0
word_index['start'] = 1
word_index['unk'] = 2
word_index['end'] = 3
reverse_word_index = dict([(value, key) for key, value in word_index.items()])
#解碼看一下文字是什麼:構建詞表索引
def decode_review(text_ids):
return ''.join([reverse_word_index.get(word_id,"unk") for word_id in text_ids])
decode_review(train_data[0])#解碼顯示句子
#對資料進行補全(padding)
max_length = 500 #句子長度低於500的句子會被補全,高於500 的會被截斷
train_data = keras.preprocessing.sequence.pad_sequences(
train_data,#list of list
value = word_index[''],
padding = 'post',#post是把padding安在句子的後面,pre是把padding安在句子的前面
maxlen = max_length
)test_data = keras.preprocessing.sequence.pad_sequences(
test_data,#list of list
value = word_index[''],
padding = 'post',#post是把padding安在句子的後面,pre是把padding安在句子的前面
maxlen = max_length
)#列印第乙個樣本
print(train_data[0])
#定義模型
embedding_dim = 16#每個word都embedding成長度為16的向量
batch_size = 128
double_rnn_model = keras.models.sequential([
keras.layers.embedding(vocab_size,embedding_dim,input_length = max_length),#第一層是embedding層
#-----------實現多層rnn的部分-------------------#
keras.layers.bidirectional(keras.layers.******rnn(units = 16,return_sequences = false)),
keras.layers.dense(16,activation="relu"),
keras.layers.dropout(0.1),
keras.layers.dense(1,activation="sigmoid"),
])double_rnn_model.summary()
double_rnn_model.compile(optimizer = "adam",
loss = "binary_crossentropy",
metrics = ['accuracy'])
#開始訓練,加上batch資訊,從訓練集裡劃分出20%作為驗證集
history_double_rnn = double_rnn_model.fit(train_data,
train_labels,
epochs = 30,
batch_size = batch_size,
validation_split = 0.2
)def plot_learning_curves(history,label,epochs,min_value,max_value):
data = {}
data[label] = history.history[label]
data['val_'+label] = history.history['val_'+label]
pd.dataframe(data).plot(figsize = (8,5))
plt.grid(true)
plt.axis([0,epochs,min_value,max_value])
plt.show()
plot_learning_curves(history_double_rnn, 'accuracy', 30, 0, 1)
plot_learning_curves(history_double_rnn, 'loss', 30, 0, 1.5)
#在測試集上驗證一下
double_rnn_model.evaluate(test_data, test_labels,batch_size = batch_size,)
運算結果:
訓練集上:
測試集上:
畫圖展示:
tensorflow實現普通RNN
coding utf 8 author zhangxianke file test.py time 2018 11 09 from tensorflow.examples.tutorials.mnist import input data import tensorflow as tf data i...
使用torch實現RNN
本文對的結果進行了復現。在實驗室的專案遇到了困難,弄不明白lstm的原理。到網上搜尋,發現lstm是rnn的變種,那就從rnn開始學吧。帶隱藏狀態的rnn可以用下面兩個公式來表示 可以看出,乙個rnn的引數有w xh,w hh,b h,w hq,b q和h t 其中h t 是步數的函式。參考的文章考...
單層for迴圈實現99乘法表
今天老師提問了乙個問題,使用乙個for迴圈實現99乘法表,當時就蒙了,因為以前都是使用雙層for迴圈實現的99乘法表,還真沒試過使用乙個for迴圈實現99乘法表,想了一會兒也沒想起來怎麼弄,網上查了一下,看了一下別人 然後又自己寫了一遍,特此記錄一下。這是雙層for迴圈,就不再詳細說明了 for i...