本文主要是介绍【我的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] = 1return resultsx_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时的运行结果:
测试集损失为: 1.209260282618386
测试集准确率为: 0.778717720444884
epochs=9时的运行结果:
测试集损失为: 0.9810770916705246
测试集准确率为: 0.7880676759212865
这篇关于【我的python机器学习之路·6】用keras做路透社新闻分类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!