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 ...
随机推荐
- javascript 实现数据结构 - 栈
栈是一种遵从后进先出(LIFO)原则的有序集合.新添加的或待删除的元素都保存在栈的同一端,称作栈顶,另一端就叫栈底.在栈里,新元素都靠近栈顶,旧元素都接近栈底.栈就好像是一个底部密封的盒子,我们往里面 ...
- qml demo分析(threading-线程任务)
一.关键类说明 qml内置了WorkerScript组件,该组件有一个source属性,可以加载js文件,含有一个名为message的信号,意味着他有一个默认的onMessage槽函数,除此之外他还有 ...
- Ubuntu|ython3 :ImportError: cannot import name 'main'
1.问题 root@stephen-K55VD:/usr/bin# pip3 Traceback (most recent call last): File "/usr/bin/pip3&q ...
- 学习ASP.NET Core Razor 编程系列十六——排序
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- ubuntu16.04无法获取ip地址的解决方案
当我们无法获取ip地址时可以使用dhcp来动态获取ip地址,安装dhcpcd5和dhcpcd-gtk sudo apt-get install dhcpcd5 sudo apt-get install ...
- Powershell:关于PSCustomObject你想知道的一切(译)
PSCustomObject是Powershell里非常重要的一个工具,我们先从基础开始然后再循序渐进讲到一些更高级的话题.PSCustomObject旨在于用简单的方法来创建结构化数据.下面的第一个 ...
- Django-restframework 之频率源码分析
Django-restframework 之频率源码分析 一 前言 经过权限判断之后就是进行频率的判断了,而频率的判断和权限又不一样,认证.权限和频率的执行流程都差不多,使用配置里面的相关类来进行判断 ...
- SpringBoot系列——jar包与war包的部署
前言 Spring Boot支持传统部署和更现代的部署形式.jar跟war都支持,这里参考springboot参考手册学习记录 两种方式 jar springboot项目支持创建可执行Jar,参考手册 ...
- [Linux] 一个前端必会的 Nginx 免费教程-在虚拟机中用deepin测试
原文技术胖的 nginx 技术胖 专注于前端开发 deepin Linux Deepin 是一个基于 DEB 包管理的一个独立操作系统,和那些 Ubuntu(下个大版本是基于 debian 开发) 的 ...
- MySQL 笔记整理(16) --“order by”是怎么工作的?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 16) --“order by”是怎么工作的? 在林老师的课程中,第15 ...