莫凡Python 3
莫凡Python 3

CNN 卷积神经网络
参考资料
- https://morvanzhou.github.io/tutorials/machine-learning/keras/2-3-CNN/
- https://keras-cn.readthedocs.io/
数据预处理
- X_train = X_train.reshape(-1, 1, 28 , 28)
这种处理我不是很理解
建立模型
- batch_input_shape=(None, 1, 28, 28)
shape : 形状
None 可以参考: https://blog.csdn.net/qq_36490364/article/details/83594271
代码
# -*- coding: utf-8 -*-
""" CNN 卷积神经网络 """
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten
import matplotlib.pyplot as plt
from keras.utils import np_utils
from keras.optimizers import Adam
from keras.datasets import mnist
from keras import backend
backend.set_image_data_format('channels_first')
# %% 数据预处理
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 1, 28 , 28)/255.
X_test = X_test.reshape(-1, 1,28, 28)/255.
Y_train = np_utils.to_categorical(Y_train, num_classes=10)
Y_test = np_utils.to_categorical(Y_test, num_classes=10)
# %% 建立模型
model = Sequential()
model.add(
Convolution2D(
batch_input_shape=(None, 1, 28, 28),
filters=32,
kernel_size=5,
strides=1,
padding='same'
)
)
model.add(Activation('relu'))
model.add(
MaxPooling2D(
pool_size=2,
strides=2,
padding='same',
)
)
model.add(Activation('relu'))
model.add(Convolution2D(64, 5, strides=1, padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(2,2,'same'))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
adam = Adam(lr=1e-4)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
# %% 训练
print('Training ------------')
model.fit(X_train,Y_train,batch_size=64)
print('\nTesting ------------')
loss, accuracy = model.evaluate(X_test, Y_test)
# %% 评估
print('\ntest loss: ', loss)
print('\ntest accuracy: ', accuracy)
莫凡Python 3的更多相关文章
- 好用的函数,assert,random.sample,seaborn tsplot, tensorflow.python.platform flags 等,持续更新
python 中好用的函数,random.sample等,持续更新 random.sample random.sample的函数原型为:random.sample(sequence, k),从指定序列 ...
- #017 python实验课第五周
总结写在最前面: 1.语法还是不会...(每周强制留的C语言一百题都没空写PS.团委诶....)都是现查现用(莫凡Python这个网站特别好用知识点一个视频就一分钟B站的播放器没广告,用啥学啥,还配有 ...
- Ubuntu环境下安装CUDA9.0
前言: 本篇文章是基于安装CUDA 9.0的经验写,CUDA9.0目前支持Ubuntu16.04和Ubuntu17.04两个版本,如下图所示(最下面的安装方式我们选择第一个,即runfile方式): ...
- Pytorch实现卷积神经网络CNN
Pytorch是torch的Python版本,对TensorFlow造成很大的冲击,TensorFlow无疑是最流行的,但是Pytorch号称在诸多性能上要优于TensorFlow,比如在RNN的训练 ...
- pytorch构建优化器
这是莫凡python学习笔记. 1.构造数据,可以可视化看看数据样子 import torch import torch.utils.data as Data import torch.nn.func ...
- pytorch批训练数据构造
这是对莫凡python的学习笔记. 1.创建数据 import torch import torch.utils.data as Data BATCH_SIZE = 8 x = torch.linsp ...
- pytorch搭建网络,保存参数,恢复参数
这是看过莫凡python的学习笔记. 搭建网络,两种方式 (1)建立Sequential对象 import torch net = torch.nn.Sequential( torch.nn.Line ...
- 关于深度学习框架 TensorFlow、Theano 和 Keras
[TensorFlow] ——( https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/) 1.TensorFlow是啥 ...
- 【优质blog、网址】置顶
一.大公司等技术blog: blog1: http://blog.csdn.net/mfcing/article/details/51577173 blog2: http://blog.csdn. ...
- Ubuntu 16.04 上安装 CUDA 9.0 详细教程
https://blog.csdn.net/QLULIBIN/article/details/78714596 前言: 本篇文章是基于安装CUDA 9.0的经验写,CUDA9.0目前支持Ubuntu1 ...
随机推荐
- 最长上升子序列 II 时间复杂度(nlogn)
题目:最长上升子序列 II 给定一个长度为 N 的数列,求数值严格单调递增的子序列的长度最长是多少. 输入格式 第一行包含整数 N. 第二行包含 N个整数,表示完整序列. 输出格式 输出一个整数,表示 ...
- P15_了解小程序的版本阶段与上线的主要步骤
协同工作和发布 - 小程序的版本 软件开发过程中的不同版本 在软件开发过程中,根据时间节点的不同,会产出不同的软件版本,例如: 开发者编写代码的同时,对项目代码进行自测(开发版本) 直到程序达到一个稳 ...
- The Missing Semester - 第四讲 学习笔记
第四讲 数据整理 课程视频地址:https://www.bilibili.com/video/BV1ym4y197iZ 课程讲义地址:https://missing-semester-cn.githu ...
- JAVA 进阶第一阶段 59-69
10/10号笔记 私有与公共 用private在类中定义的成员变量 只有在这个类的内部才支持访问和编写 public 公共的 用这个定义的在任何地方都可以访问 比如public calss clock ...
- 你想知道的do{...}while(0)的作用,都在这里了
0.引言 我们在嵌入式开发的过程中,经常可以碰到在一些宏定义或者是代码段中使用了do {...} while(0)的语句,从语义上理解,do {...} while(0)内的逻辑就只执 ...
- GPT接入飞书
GPT接入飞书 在体验ChatGPT这方面,我算是晚的.使用下来,更多的是对于这种应用形式感到兴奋,而不是ChatGPT的专业能力. 得知OpenAI提供GPT3的Api接口后,我想到了将其接入团队飞 ...
- ubuntu20.04安装fastdfs遇到的问题
说明:git clone在线安装与离线安装都不成功后,选择原来可以正常运行的fastdfs服务,进行tar打包下载,再在新项目上进行解压部署.但由于打包压缩动态库的软连接 失效,所以启动出现如下报错信 ...
- Deer_GF之IOS出热更包
Hi,今天介绍一下Deer_GF_Wolong,框架基于HybridCLR热更新技术,出IOS热更包 首先我们需要准备的工作: 环境:Mac Unity 版本 2021.3.1f1c1 热更方案:Hy ...
- 0627.selenium请求库*1
今日内容: 一 Selenium请求库 一 Selenium请求库 1.什么是selenium? selenium是一个自动测试工具,它可以帮我通过代码 去实现驱动浏览器自动执行相应的操作. 所以我们 ...
- 基于AD9361的双收双发射频FMC子卡
FMC177-基于AD9361的双收双发射频FMC子卡 一.板卡介绍 FMC177射频模块分别包含两个接收通道与发射通道,其频率可覆盖达到70MHz~6GHz,AD9361芯片提供具有成本效益的实验平 ...