keras01 - hello world ~ 搭建第一个神经网络
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 ~ 搭建第一个神经网络的更多相关文章
- 深度学习实践系列(3)- 使用Keras搭建notMNIST的神经网络
前期回顾: 深度学习实践系列(1)- 从零搭建notMNIST逻辑回归模型 深度学习实践系列(2)- 搭建notMNIST的深度神经网络 在第二篇系列中,我们使用了TensorFlow搭建了第一个深度 ...
- AI - TensorFlow - 第一个神经网络(First Neural Network)
Hello world # coding=utf-8 import tensorflow as tf import os os.environ[' try: tf.contrib.eager.enab ...
- 从环境搭建到回归神经网络案例,带你掌握Keras
摘要:Keras作为神经网络的高级包,能够快速搭建神经网络,它的兼容性非常广,兼容了TensorFlow和Theano. 本文分享自华为云社区<[Python人工智能] 十六.Keras环境搭建 ...
- 使用SpringMVC搭建第一个项目
概述 使用SpringMVC搭建第一个项目,入门教程,分享给大家. 详细 代码下载:http://www.demodashi.com/demo/10596.html 一.概述 1.什么是Spring ...
- 用pytorch1.0快速搭建简单的神经网络
用pytorch1.0搭建简单的神经网络 import torch import torch.nn.functional as F # 包含激励函数 # 建立神经网络 # 先定义所有的层属性(__in ...
- 用pytorch1.0搭建简单的神经网络:进行多分类分析
用pytorch1.0搭建简单的神经网络:进行多分类分析 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib ...
- 用pytorch1.0搭建简单的神经网络:进行回归分析
搭建简单的神经网络:进行回归分析 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib.pyplot as p ...
- keras02 - hello convolution neural network 搭建第一个卷积神经网络
本项目参考: https://www.bilibili.com/video/av31500120?t=4657 训练代码 # coding: utf-8 # Learning from Mofan a ...
- 矩池云 | 搭建浅层神经网络"Hello world"
作为图像识别与机器视觉界的 "hello world!" ,MNIST ("Modified National Institute of Standards and Te ...
随机推荐
- MySQL命令窗口下中文显示乱码的解决过程
在dos客户端输出窗口中查询表中的数据,还有项目部署到服务器上时前台的页面,中文数据都显示成乱码,如下图所示: 这个问题困扰了我一天,后来解决了才发现原来我的方向错了,一直我以为是SpringBoot ...
- Python300篇电子书免费送
此电子书集合由猪哥整理,免费发布于微信公众号:裸睡的猪. 此电子书集合将持续更新,获取最新电子书集合请关注微信公众号:裸睡的猪,回复:电子书此电子书集合仅用作个人学习,请勿用于商业获利!!! 获取方式 ...
- RecyclerViewItemTouchHelperDemo【使用ItemTouchHelper进行拖拽排序功能】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录使用ItemTouchHelper对Recyclerview进行拖拽排序功能的实现. 效果图 代码分析 ItemTouchHel ...
- Ubuntu:命令行下浏览网页
前述 兴起,试一下不用图形化界面浏览 安装w3m 直接进入root账号 apt-get install w3m 检验是否成功 w3m www.baidu.com 就这样成功的进入baidu了,纯文本模 ...
- 访问了一次百度网页,你都经历了什么?https及tcp协议揭秘
打开https://www.baidu.com/ 网页一个简单的动作,都经历了什么?你想探究内部的原理吗?那我们一起去探索吧 1.准备工作 安装好wireshark.Wireshark(前称Ether ...
- RDIFramework.NET V3.3 Web框架主界面新增横向菜单功能
功能描述 响应重多客户的要求与心声,RDIFramework.NET框架Web版本主界面新增横向菜单功能.横向菜单更加直观,用户可操作与展示的空间更多,符合实际应用要求. 一.效果展示 最终界面效果: ...
- QuickBI助你成为分析师——计算字段功能
摘要: 在用户创建报表时,通过现有字段数据不能直接满足展示需求,需要进行一定建模操作.目前产品支持在数据集编辑界面进行初步建模,下面主要介绍新建字段功能,以达到展示需求. 在用户创建报表时,有时通过现 ...
- webstorm 2018.2.3 license server
推荐地址:http://idea.lanyus.com/ 使用前请将“0.0.0.0 account.jetbrains.com”添加到hosts文件中
- align-content 与 align-items 区别
align-content 和 align-items : 1:共同点:它们对齐方向为交叉轴 2:不同点:align-content 应用于为 多行 而 align-items:应用于单行. 单 ...
- python自定义pi函数的代码
下边内容是关于python自定义pi函数的内容. def pi(): # Compute digits of Pi. # Algorithm due to LGLT Meertens. k, a, b ...