【我的python机器学习之路·6】用keras做路透社新闻分类

本系列日记GitHub:

https://github.com/zhengyuv/MyPyMLRoad

欢迎follow和star。

代码:

# -*- coding: utf-8 -*-
"""
Created on Fri Nov 30 09:22:03 2018

@author: zhengyuv
"""
from keras.datasets import reuters
import numpy as np
from keras import models
from keras import layers
import matplotlib.pyplot as plt
from keras import metrics

#read dataset
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(path=r'D:\datasets\reuters.npz', num_words=10000)

def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1
    return results

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)

y_train = vectorize_sequences(train_labels, dimension=46)
y_test = vectorize_sequences(test_labels, dimension=46)

#model definition
model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(10000, )))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(46, activation='softmax'))

#model compile
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

#split data
x_val = x_train[:1000]
x_train = x_train[1000:]
y_val = y_train[:1000]
y_train = y_train[1000:]

#fit model
history = model.fit(x_train,
                    y_train,
                    epochs=20,
                    batch_size=512,
                    validation_data=(x_val,y_val))

#plot
loss = history.history['loss']
val_loss = history.history['val_loss']
acc = history.history['acc']
val_acc = history.history['val_acc']
epochs = range(1, len(loss)+1)

plt.plot(epochs, loss, 'bo', label='training loss')
plt.plot(epochs, val_loss, 'r', label='validation loss')
plt.title('training and validation loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.show()

plt.clf()
plt.plot(epochs, acc, 'bo', label='training acc')
plt.plot(epochs, val_acc, 'r', label='validation acc')
plt.title('training and validation accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()

#test
score = model.evaluate(x_test, y_test)
print("测试集损失为:", score[0])
print("测试集准确率为:",score[1])

epochs=20时的运行结果:

【我的python机器学习之路·6】用keras做路透社新闻分类_第1张图片

【我的python机器学习之路·6】用keras做路透社新闻分类_第2张图片

测试集损失为: 1.209260282618386
测试集准确率为: 0.778717720444884

epochs=9时的运行结果:

【我的python机器学习之路·6】用keras做路透社新闻分类_第3张图片

【我的python机器学习之路·6】用keras做路透社新闻分类_第4张图片

测试集损失为: 0.9810770916705246
测试集准确率为: 0.7880676759212865

 

你可能感兴趣的:(学习日记,我的python机器学习之路)