import numpy as np
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers.core import Dense, Activation, Dropout
from keras.utils import np_utils import matplotlib.pyplot as plt
import matplotlib.image as processimage # Load mnist RAW dataset
# 训练集28*28的图片X_train = (60000, 28, 28) 训练集标签Y_train = (60000,1)
# 测试集图片X_test = (10000, 28, 28) 测试集标签Y_test = (10000,1)
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
print(X_train.shape, Y_train.shape)
print(X_test.shape, Y_test.shape) '''
第一步,准备数据
'''
# Prepare 准备数据
# Reshape 60k个图片,每个28*28的图片,降维成一个784的一维数组
X_train = X_train.reshape(60000, 784) # 28*28 = 784
X_test = X_test.reshape(10000, 784)
# set type into float32 设置成浮点型,因为使用的是GPU,GPU可以加速运算浮点型
# CPU使用int型计算会更快
X_train = X_train.astype('float32') # astype SET AS TYPE INTO
X_test = X_test.astype('float32')
# 归一化颜色
X_train = X_train/255 # 除以255个颜色,X_train(0, 255)-->(0, 1) 更有利于浮点运算
X_test = X_test/255 '''
第二步,给神经网络设置基本参数
'''
# Prepare basic setups
batch_sizes = 4096 # 一次给神经网络注入多少数据,别超过6万,和GPU内存有关
nb_class = 10 # 设置多少个分类
nb_epochs = 10 # 60k数据训练20次,一般小数据10次就够了 '''
第三步,设置标签
'''
# Class vectors label(7) into [0,0,0,0,0,0,0,1,0,1] 把7设置成向量
Y_test = np_utils.to_categorical(Y_test, nb_class) # Label
Y_train = np_utils.to_categorical(Y_train, nb_class) '''
第四步,设置网络结构
'''
model = Sequential() # 顺序搭建层
# 1st layer
model.add(Dense(512, input_shape=(784,))) # Dense是输出给下一层, input_dim = 784 [X*784]
model.add(Activation('relu')) # tanh
model.add(Dropout(0.2)) # overfitting # 2nd layer
model.add(Dense(256)) # 256是因为上一层已经输出512了,所以不用标注输入
model.add(Activation('relu'))
model.add(Dropout(0.2)) # 3rd layer
model.add(Dense(10))
model.add(Activation('softmax')) # 根据10层输出,softmax做分类 '''
第五步,编译compile
'''
model.compile(
loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy']
) # 启动网络训练 Fire up
Trainning = model.fit(
X_train, Y_train,
batch_size=batch_sizes,
epochs=nb_epochs,
validation_data=(X_test, Y_test)
)
# 以上就可运行 '''
最后,检查工作
'''
# Trainning.history # 检查训练历史
# Trainning.params # 检查训练参数 # 拉取test里的图
testrun = X_test[9999].reshape(1, 784) testlabel = Y_test[9999]
print('label:-->', testlabel)
print(testrun.shape)
plt.imshow(testrun.reshape([28, 28])) # 判断输出结果
pred = model.predict(testrun)
print(testrun)
print('label of test same Y_test[9999]-->>', testlabel)
print('预测结果-->>', pred)
print([final.argmax() for final in pred]) # 找到pred数组中的最大值 # 用自己的画的图28*28预测一下 (不太准,可以用卷积)
# 可以用PS创建28*28像素的图,且是灰度,没有色彩
target_img = processimage.imread('/.../picture.jpg')
print(' before reshape:->>', target_img.shape)
plt.imshow(target_img)
target_img = target_img.reshape(1, 784) # reshape
print(' after reshape:->>', target_img.shape) target_img = np.array(target_img) # img --> numpy array
target_img = target_img.astype('float32') # int --> float32
target_img /= 255 # (0,255) --> (0,1) print(target_img) mypred = model.predict(target_img)
print(mypred)
print(myfinal.argmax() for myfinal in mypred)

参考:https://www.bilibili.com/video/av29806227

keras01 - hello world ~ 搭建第一个神经网络的更多相关文章

  1. 深度学习实践系列(3)- 使用Keras搭建notMNIST的神经网络

    前期回顾: 深度学习实践系列(1)- 从零搭建notMNIST逻辑回归模型 深度学习实践系列(2)- 搭建notMNIST的深度神经网络 在第二篇系列中,我们使用了TensorFlow搭建了第一个深度 ...

  2. AI - TensorFlow - 第一个神经网络(First Neural Network)

    Hello world # coding=utf-8 import tensorflow as tf import os os.environ[' try: tf.contrib.eager.enab ...

  3. 从环境搭建到回归神经网络案例,带你掌握Keras

    摘要:Keras作为神经网络的高级包,能够快速搭建神经网络,它的兼容性非常广,兼容了TensorFlow和Theano. 本文分享自华为云社区<[Python人工智能] 十六.Keras环境搭建 ...

  4. 使用SpringMVC搭建第一个项目

    概述 使用SpringMVC搭建第一个项目,入门教程,分享给大家. 详细 代码下载:http://www.demodashi.com/demo/10596.html 一.概述 1.什么是Spring ...

  5. 用pytorch1.0快速搭建简单的神经网络

    用pytorch1.0搭建简单的神经网络 import torch import torch.nn.functional as F # 包含激励函数 # 建立神经网络 # 先定义所有的层属性(__in ...

  6. 用pytorch1.0搭建简单的神经网络:进行多分类分析

    用pytorch1.0搭建简单的神经网络:进行多分类分析 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib ...

  7. 用pytorch1.0搭建简单的神经网络:进行回归分析

    搭建简单的神经网络:进行回归分析 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib.pyplot as p ...

  8. keras02 - hello convolution neural network 搭建第一个卷积神经网络

    本项目参考: https://www.bilibili.com/video/av31500120?t=4657 训练代码 # coding: utf-8 # Learning from Mofan a ...

  9. 矩池云 | 搭建浅层神经网络"Hello world"

    作为图像识别与机器视觉界的 "hello world!" ,MNIST ("Modified National Institute of Standards and Te ...

随机推荐

  1. ASP.NET Core中使用GraphQL - 第八章 在GraphQL中处理一对多关系

    ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间 ...

  2. 论文学习-系统评估卷积神经网络各项超参数设计的影响-Systematic evaluation of CNN advances on the ImageNet

    博客:blog.shinelee.me | 博客园 | CSDN 写在前面 论文状态:Published in CVIU Volume 161 Issue C, August 2017 论文地址:ht ...

  3. 补习系列(12)-springboot 与邮件发送

    目录 一.邮件协议 关于数据传输 二.SpringBoot 与邮件 A. 添加依赖 B. 配置文件 C. 发送文本邮件 D.发送附件 E. 发送Html邮件 三.CID与图片 参考文档 一.邮件协议 ...

  4. docker(3)容器管理命令

    接着上一篇,今天说一下Docker 有关容器的常用命令.算是比较详细了吧. docker run  命令: 注:此命令作用是使用一个镜像运行启动一个容器. 在启动运行的时候 会检查docker 中是否 ...

  5. relief中visio图出现问题处理

    需安装visio2010版本, 安装DsoFramer_KB311765_x86.exe 管理员权限打开cmd,运行regsvr32 dsoframer.ocx

  6. java爬虫系列第一讲-爬虫入门

    1. 概述 java爬虫系列包含哪些内容? java爬虫框架webmgic入门 使用webmgic爬取 http://ady01.com 中的电影资源(动作电影列表页.电影下载地址等信息) 使用web ...

  7. 零基础学Python--------第10章 文件及目录操作

    第10章 文件及目录操作 10.1 基本文件操作 在Python中,内置了文件(File)对象.在使用文件对象时,首先需要通过内置的open() 方法创建一个文件对象,然后通过对象提供的方法进行一些基 ...

  8. 观察者模式与.Net Framework中的委托与事件

    本文文字内容均选自<大话设计模式>一书. 解释:观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够 ...

  9. 你应该学会的Python多版本管理工具Pyenv

    目录 Pyenv 简介 安装pyenv 通过pyenv安装python各种发行版 pyenv命令 多版本Python的管理 Pyenv常见问题Wiki Pyenv 简介 首先,该工具是在类linux环 ...

  10. EF时,数据库字段和实体类不一致问题

    场景:由于一些原因,实体中属性比数据库中字段多了一个startPage属性.PS:controllers中用实体类去接收参数,但是传入的参数比数据库中实体表多了一个字段, 这种情况下,应该建一个vie ...