#简单的循环网络 #-*-coding:utf-8 -*- from keras.datasets import imdbfrom keras.preprocessing import sequence max_fetaures = 10000maxlen = 500batch_size = 32print("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 Densefrom keras.models import Sequentialfrom 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()
原文地址:https://www.cnblogs.com/shuimuqingyang/p/10430335.html
时间: 2024-10-08 16:46:04