keras实战(二)--imdb影评分类/路透社语料多分类

【keras实战(二)--imdb影评分类/路透社语料多分类】Reference
N-gram模型
Keras深度神经网络训练IMDB情感分类的四种方法
Deep learning with Python
1.语料来源
由于下载太慢,可以使用以下链接下载,并将其拷贝到 ~/.keras/datasets目录下:
imdb链接:https://pan.baidu.com/s/1J9y40T3zIOlsMk0xHeVBqg 密码:y74j
reuters链接:https://pan.baidu.com/s/1R9kEK3-cCuU0YZpuXBqNDQ 密码:r7e4
2.代码:

from keras.datasets import reuters import numpy as np from keras.utils import np_utils from keras.layers import Dense from keras.models import Sequential (train_data,train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000) # print(len(train_data)) # print(len(train_labels)) # print(train_labels[0]) def vectorize_sequences(sequences, dimension=10000): results = np.zeros((len(sequences), dimension)) for i, sequence in enumerate(sequences): results[i, sequence] = 1. return resultsx_train = vectorize_sequences(train_data) x_test = vectorize_sequences(test_data)y_train = np_utils.to_categorical(train_labels) y_test = np_utils.to_categorical(test_labels)model = Sequential() model.add(Dense(64, activation='relu', input_shape=(10000,))) model.add(Dense(64, activation='relu')) model.add(Dense(46, activation='softmax')) model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy']) x_val = x_train[:1000] partial_x_train = x_train[1000:] y_val = y_train[:1000] partial_y_train = y_train[1000:]model.fit(partial_x_train, partial_y_train, epochs=8, batch_size=512, validation_data=https://www.it610.com/article/(x_val, y_val)) results = model.evaluate(x_test,y_test)

这里写代码片

    推荐阅读