PYTHON深度学习6.2RNN循环网络

Posted 水木清扬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PYTHON深度学习6.2RNN循环网络相关的知识,希望对你有一定的参考价值。

#简单的循环网络

#-*-coding:utf-8 -*-


from keras.datasets import imdb
from keras.preprocessing import sequence

max_fetaures = 10000
maxlen = 500
batch_size = 32
print("Loading data...")
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="/home/duchao/projects(my)/keras/kagge/6/6.1/imdb.npz",num_words=max_fetaures)
print(len(x_train), ‘train sequences‘)
print(len(x_test), ‘test sequences‘)

print(‘Pad sequences (sample x time)‘)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)

print(‘x_train shape:‘, x_train.shape)
print(‘x_test shape:‘, x_test.shape)



from keras.layers import Dense
from keras.models import Sequential
from keras.layers import Embedding,SimpleRNN

model = Sequential()
model.add(Embedding(max_fetaures, 32))
model.add(SimpleRNN(32))
model.add(Dense(1, activation=‘sigmoid‘))

model.compile(optimizer=‘rmsprop‘, loss=‘binary_crossentropy‘, metrics=[‘acc‘])
history = model.fit(x_train, y_train, epochs=10, batch_size=128, validation_split=0.2)




import matplotlib.pyplot as plt

acc = history.history[‘acc‘]
val_acc = history.history[‘val_acc‘]
loss = history.history[‘loss‘]
val_loss = history.history[‘val_loss‘]

epochs = range(1, len(acc) + 1)

plt.plot(epochs, acc, ‘bo‘, label=‘Training acc‘)
plt.plot(epochs, val_acc, ‘b‘, label=‘Validation acc‘)
plt.title(‘Training and validation accuracy‘)
plt.legend()

plt.figure()

plt.plot(epochs, loss, ‘bo‘, label=‘Training loss‘)
plt.plot(epochs, val_loss, ‘b‘, label=‘Validation loss‘)
plt.title(‘Training and validation loss‘)
plt.legend()

plt.show()

以上是关于PYTHON深度学习6.2RNN循环网络的主要内容,如果未能解决你的问题,请参考以下文章

Python深度学习循环神经网络RNN中文分词实战(附源码)

Keras深度学习实战(27)——循环神经详解与实现

Keras深度学习实战(27)——循环神经详解与实现

深度学习面试题32:循环神经网络原理(RNN)

水很深的深度学习-Task05循环神经网络RNN

手把手写深度学习——用RNN循环神经网络自动生成张靓颖歌词