安装Keras

pip install keras

代码

'''Trains a simple convnet on the MNIST dataset.Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID 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, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as Kbatch_size = 128
num_classes = 10
epochs = 12# input image dimensions
img_rows, img_cols = 28, 28# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()if K.image_data_format() == 'channels_first':x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)input_shape = (1, img_rows, img_cols)
else:x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)input_shape = (img_rows, img_cols, 1)x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])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])

Keras MNIST相关推荐

  1. Keras-2 Keras Mnist

    Keras Mnist 在这里,我们将利用Keras搭建一个深度学习网络对mnist数据集进行识别. 本文参考 keras-mnist-tutorial 整个代码分为三个部分: 数据准备 模型搭建 训 ...

  2. input date保存值_涛哥文集(36):R keras保存和还原模型

    在训练之后和训练过程中都可以保存模型进度. 这意味着模型可以从中断的地方继续进行,避免了长时间的训练. 保存还意味着您可以共享模型,而其他人可以重新创建您的作品. 在发布研究模型和技术时,大多数机器学 ...

  3. Keras-4 mnist With CNN

    Keras mnist With CNN 这次,我们将在Keras下利用卷积神经网络(CNN)对mnist进行训练和预测 关于卷积神经网络,强烈推荐零基础入门深度学习(4) - 卷积神经网络,有详细解 ...

  4. opencv+mnist识别手写数字串

    介绍 为了实现对手写数字串的识别,本次用到了opencv+mnist技术,使用opencv能更好的处理图片,分割图片,使用mnist数据集进行预测识别率大概达到96%左右,但真实测试的时候识别率要低一 ...

  5. Tensorflow:如何保存/恢复模型?

    在Tensorflow中训练模型后: 您如何保存经过训练的模型? 您以后如何还原此保存的模型? #1楼 对于TensorFlow版本<0.11.0RC1: 保存的检查点包含模型中Variable ...

  6. linux 内核日志等级,Linux系统中日志级别详情

    日志信息分类 1.等级由低到高:debug 2.区别: debug 级别最低,可以随意的使用于任何觉得有利于在调试时更详细的了解系统运行状态的东东: info  重要,输出信息:用来反馈系统的当前状态 ...

  7. Ray----Tune(6):Tune 的实例(一)

    在我们的存储库中,我们为各种用例和调优特性提供了各种示例. 一. 一般的例子: 1.自定义日志记录器和自定义试验名命名的示例. ray/python/ray/tune/examples/logging ...

  8. Classification and inference with machine learning

    machine learning作业代写.代做Markdown留学生作业.代写python, C/C++编程语言作业 Project 3 Classification and inference wi ...

  9. Ray----Tune(2):Tune的用户指南

    Tune概述 本篇主要介绍Tune的使用向导,首先是Tune的运行框架(如下图),然后具体介绍实验配置信息(Training API.启动和自定义的trial).Trailing特征(Tune的搜索空 ...

最新文章

  1. 数据分析从零开始实战,Pandas读写Excel/XML数据
  2. UA SIE545 优化理论基础9 优先与分治策略1 文件的最优存储顺序
  3. 从这3个方面,帮你大幅度提升用户的搜索体验
  4. circle函数用法 turtle_Turtle库与Time库基础知识分享(详细)
  5. 挑战弱监督学习的三大热门问题,AutoWSL2019挑战赛正式开赛
  6. 获得jmp esp地址
  7. npu算力如何计算_CPU、GPU、NPU、FPGA等芯片架构特点分析
  8. python 内置运算
  9. Codeforces Round #315 (Div. 1) A. Primes or Palindromes? 暴力
  10. anaconda查看删除增加镜像源
  11. 区分execl与system——应用程序中执行命令
  12. Java框架之Hibernate(二)
  13. 40. Element getAttribute() 方法
  14. C++搭建集群聊天室(二):安装muduo网络库
  15. 微弱信号放大电路(电磁超声)
  16. 圆的内接正n边形的周长
  17. 蓝桥杯——六面体染色
  18. BUUCTF 打卡 21/9/1 Many-Time-Pad
  19. Discuznbsp;x2.5单页制作的教程
  20. YAML——基本语法

热门文章

  1. 关于C#的Main(String[] args)参数输入问题
  2. CodeForces - 946E Largest Beautiful Number(贪心+模拟)
  3. CodeForces - 487B Strip(线段树+dp+二分)
  4. 深度学习项目-人脸表情识别
  5. 脚注交叉引用序号不一样_期末干货|蛋黄君的论文不秃头指南
  6. 斯特林反演[bzoj4671]异或图
  7. windows终止处理程序( __try __finally) 简单解析
  8. 如何使用Appverifier ?
  9. Flink 状态管理:算子状态、键值分区状态、状态后端、有状态算子的扩缩容
  10. 千万不要在深夜提交代码