keras绘制损失函数曲线

# -*- coding: utf-8 -*-
'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
''' from __future__ import print_function import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
import matplotlib.pyplot as plt batch_size = 128
num_classes = 10
epochs = 20 # the data, shuffled and split between train and test sets
# (x_train, y_train), (x_test, y_test) = mnist.load_data() (x_train, y_train), (x_test, y_test) = mnist.load_data(path='/home/duchao/下载/mnist.npz') # import numpy as np
#
# path = '/home/duchao/下载/mnist.npz'
# f = np.load(path)
# x_train, y_train = f['x_train'], f['y_train']
# x_test, y_test = f['x_test'], f['y_test']
# f.close() x_train = x_train.reshape(60000, 784).astype('float32')
x_test = x_test.reshape(10000, 784).astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices
# label为0~9共10个类别,keras要求格式为binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes) # add by hcq-20171106
# Dense of keras is full-connection.
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax')) model.summary() model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy']) history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1]) history_dict=history.history
loss_value=history_dict["loss"]
val_loss_value=history_dict["val_loss"] epochs=range(1,len(loss_value)+1)
plt.plot(epochs,loss_value,"bo",label="Training loss")
plt.plot(epochs,val_loss_value,"b",label="Validation loss")
plt.xlabel("epochs")
plt.ylabel("loss")
plt.legend()
plt.show()
# -*- coding: utf-8 -*-
'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
''' from __future__ import print_function import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
import matplotlib.pyplot as plt batch_size = 128
num_classes = 10
epochs = 40 # the data, shuffled and split between train and test sets
# (x_train, y_train), (x_test, y_test) = mnist.load_data() (x_train, y_train), (x_test, y_test) = mnist.load_data(path='/home/duchao/下载/mnist.npz') # import numpy as np
#
# path = '/home/duchao/下载/mnist.npz'
# f = np.load(path)
# x_train, y_train = f['x_train'], f['y_train']
# x_test, y_test = f['x_test'], f['y_test']
# f.close() x_train = x_train.reshape(60000, 784).astype('float32')
x_test = x_test.reshape(10000, 784).astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices
# label为0~9共10个类别,keras要求格式为binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes) # add by hcq-20171106
# Dense of keras is full-connection.
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax')) model.summary() model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy']) history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1]) # ##绘制训练损失和验证损失
# history_dict=history.history
# loss_value=history_dict["loss"]
# val_loss_value=history_dict["val_loss"]
#
# epochs=range(1,len(loss_value)+1)
# plt.plot(epochs,loss_value,"bo",label="Training loss")
# plt.plot(epochs,val_loss_value,"b",label="Validation loss")
# plt.xlabel("epochs")
# plt.ylabel("loss")
# plt.legend()
# plt.show() ##绘制训练精度和验证精度 plt.clf()
history_dict=history.history
acc=history_dict["acc"]
val_acc=history_dict["val_acc"] loss_value=history_dict["loss"]
val_loss_value=history_dict["val_loss"] epochs=range(1,len(val_acc)+1) plt.plot(epochs,acc,"bo",label="Training acc")
plt.plot(epochs,val_acc,"b",label="Validation acc") plt.plot(epochs,loss_value,"bo",label="Training loss")
plt.plot(epochs,val_loss_value,"b",label="Validation loss") plt.xlabel("epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
# -*- coding: utf-8 -*-
'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''
#
# from keras import layers
# from keras.datasets import boston_housing
#
#
# (train_data, train_labels), (test_data, test_labels) = boston_housing.load_data() from keras.datasets import boston_housing
from keras import models
from keras import layers
import numpy as np
import matplotlib.pyplot as plt # train_data.shape:(404, 13),test_data.shape:(102, 13),
# train_targets.shape:(404,),test_targets.shape:(102,)
# the data compromises 13 features
# the targets are the median values of owner-occupied homes,in thousands of dollars
(train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()
# feature-wise normalization
mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std
# never use any quantity computed on the test data
test_data -= mean
test_data /= std # build the model
# because we need to build a model several times,we use function to cons
def build_model():
model = models.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(train_data.shape[1],)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1))
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
return model #
# # K-fold validation
# k = 4
# num_val_samples = len(train_data) // k
# num_epochs = 500
# all_scores = []
# all_mae_histories = []
# K-fold validation and logs
k = 4
num_val_samples = len(train_data) // k
num_epochs = 50
all_scores = []
all_mae_histories = []
for i in range(k):
print('正在处理fold #', i)
# preparing the validation data:data from partition #k
val_data = train_data[i * num_val_samples:(i + 1) * num_val_samples]
val_targets = train_targets[i * num_val_samples:(i + 1) * num_val_samples]
# preparing the training data:data from all other partitions
partial_train_data = np.concatenate(
[train_data[:i * num_val_samples],
train_data[((i + 1) * num_val_samples):]],
axis=0
)
partial_train_targets = np.concatenate(
[train_targets[:i * num_val_samples],
train_targets[((i + 1) * num_val_samples):]],
axis=0
)
# build the model
model = build_model()
# train the model,silent mode
history = model.fit(partial_train_data, partial_train_targets, validation_data=(val_data, val_targets),
epochs=num_epochs, batch_size=1, verbose=0)
# evaluate the model in the validation data
mae_history = history.history['val_mean_absolute_error']
val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
all_scores.append(val_mae)
all_mae_histories.append(mae_history) print("Complete!")
average_mae_history = [
np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]
mean_score = np.mean(all_scores)
print("mean_score:", mean_score) #plotting validation scores
plt.plot(range(1,len(average_mae_history)+1),average_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()
 
 

最新文章

  1. Socket简单使用
  2. C#微信公众号开发系列教程三(消息体签名及加解密)
  3. IOS UI多线程 NSThread 下载并显示图片到UIImageView
  4. Objective C 快速入门学习二
  5. 用Python给你的博客加上水印
  6. 你需要知道的swift必备函数 map
  7. centos mongodb安装及简单实例
  8. facebook海量图片存储系统与淘宝TFS系统比较
  9. HDU 3749 Financial Crisis 经济危机(点双连通分量)
  10. Delphi ControlCount和ComponentCount的区别
  11. 使用 CustomScript 扩展程序自动执行 Linux 虚拟机自定义任务
  12. python之模块:decimal
  13. C# RSA在服务上使用出现拒绝方法错误的解决方法
  14. 操作VCF卡片信息的第三方jar包:ez-vcard
  15. UVA 10559 Blocks
  16. 007-declare 声明变量的类型
  17. 《全栈营销之如何制作个人博客》之二:php环境安装及个人博客后台搭建 让你的博客跑起来
  18. CF Educational Codeforces Round 57划水记
  19. μCOS-II移植 - 基于CortexM3
  20. caffe matlab matcaffe 加载输入网络net时报错

热门文章

  1. 使用docker快速体验kali linux
  2. P 1015 德才论
  3. 【分布式】流式计算Storm框架
  4. cf 525D.Arthur and Walls
  5. 67.ORM查询条件:range的使用,使用make_aware将navie time 转换为aware time
  6. 一、SAP中添加一个模块到收藏夹后,显示事务代码
  7. Java平台上的AOP实现机制
  8. JS高级学习笔记(10) 之 js 时怎么解析HTML标签的
  9. Codeforces 405E DFS
  10. Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题