Keras实现卷积神经网络
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 11:25:29 2019 @author: zhen
""" import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D # 加载数据
(x_train, y_train), (x_test, y_test) = mnist.load_data("../test_data_home")
# 转化训练数据为四维张量形式
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype("float32")
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype("float32")
# 归一化
x_train /= 255
x_test /= 255 #转化为one hot 编码
def to_one_hot(y):
y_one_hot = np.zeros(10) # 生成全零向量
y_one_hot[y] = 1
return y_one_hot # 重置标签
y_train_one_hot = np.array([to_one_hot(y_train[i]) for i in range(len(y_train))])
y_test_one_hot = np.array([to_one_hot(y_test[i]) for i in range(len(y_test))])
# 搭建卷积神经网络
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), padding='same', input_shape=(28, 28, 1),
activation='relu'))
# 添加最大池化层
model.add(MaxPooling2D(pool_size=(2, 2)))
# 添加Dropout层
model.add(Dropout(0.2))
# 构建深度网络
model.add(Conv2D(64, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(128, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
# 展开
model.add(Flatten())
# 构造全连接层
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))
# 定义损失函数
model.compile(loss='categorical_crossentropy', optimizer='adagrad',
metrics=['accuracy'])
# 训练
model.fit(x_train, y_train_one_hot, validation_data=(x_test, y_test_one_hot),
epochs=2, batch_size=128)
# 评估
# verbose : 0表示不显示数据,1表示显示进度
scores = model.evaluate(x_test, y_test_one_hot, verbose=0)
print(scores)
结果:

Keras实现卷积神经网络的更多相关文章
- visualization of filters keras 基于Keras的卷积神经网络(CNN)可视化
https://adeshpande3.github.io/adeshpande3.github.io/ https://blog.csdn.net/weiwei9363/article/detail ...
- keras与卷积神经网络(CNN)实现识别minist手写数字
在本篇博文当中,笔者采用了卷积神经网络来对手写数字进行识别,采用的神经网络的结构是:输入图片——卷积层——池化层——卷积层——池化层——卷积层——池化层——Flatten层——全连接层(64个神经元) ...
- 深度学习:Keras入门(二)之卷积神经网络(CNN)
说明:这篇文章需要有一些相关的基础知识,否则看起来可能比较吃力. 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的运算.(具体含义或者数学公式 ...
- 【Python】keras卷积神经网络识别mnist
卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...
- 1.keras实现-->使用预训练的卷积神经网络(VGG16)
VGG16内置于Keras,可以通过keras.applications模块中导入. --------------------------------------------------------将 ...
- 深度学习:Keras入门(二)之卷积神经网络(CNN)【转】
本文转载自:https://www.cnblogs.com/lc1217/p/7324935.html 说明:这篇文章需要有一些相关的基础知识,否则看起来可能比较吃力. 1.卷积与神经元 1.1 什么 ...
- 深度学习:Keras入门(二)之卷积神经网络(CNN)(转)
转自http://www.cnblogs.com/lc1217/p/7324935.html 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的 ...
- Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例
CNN 卷积神经网络 卷积 池化 https://www.cnblogs.com/peng8098/p/nlp_16.html 中有介绍 以数据集MNIST构建一个卷积神经网路 from keras. ...
- 了解1D和3D卷积神经网络 | Keras
当我们说卷积神经网络(CNN)时,通常是指用于图像分类的2维CNN.但是,现实世界中还使用了其他两种类型的卷积神经网络,即1维CNN和3维CNN.在本指南中,我们将介绍1D和3D CNN及其在现实世界 ...
随机推荐
- 关于Flutter初始化流程,我必须告诉你的是...
1. 引言 最近在做性能优化的时候发现,在混合栈开发中,第一次启动Flutter页面的耗时总会是第二次启动Flutter页面耗时的两倍左右,这样给人感觉很不好.分析发现第一次启动Flutter页面会做 ...
- MyBatis源码解析(十一)——Parsing解析模块之通用标记解析器(GenericTokenParser)与标记处理器(TokenHandler)
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6724223.html 1.回顾 上面的几篇解析了类型模块,在MyBatis中类型模块包含的 ...
- 分布式系统监视zabbix讲解九之使用snmp监控windows--技术流ken
前言 使用zabbix监控windows主要有两种方法,一种是在windows端安装zabbix-agent客户端工具,安装麻烦.另外一种是使用snmp协议,配置简单快捷.两种配置都可以实现同样的效果 ...
- vmware vcsa-6.5 网络架构之虚拟机的标准交换机
一.配置虚拟机网络 1.概述(esxi 比workstation,vmware server,网络功能更强大) workstation和vmware server每块物理网卡可以给多个虚拟机使用,多个 ...
- [转]VirtualBox centos7扩容
本文转自:https://www.cnblogs.com/xd502djj/p/7367704.html 有时候扩容还真不如重新建立一个大硬盘的系统,但是如果你安装了好多东西的话,那还是来扩容一下吧. ...
- [转]How to Add Bootstrap to an Angular CLI project
本文转自:https://loiane.com/2017/08/how-to-add-bootstrap-to-an-angular-cli-project/ In this article we w ...
- 第一册:lesson thirty seven。
原文: Making a bookcase. A:You are working hard,George. What are you doing . B:I am making a bookcase. ...
- 从零开始学安全(十九)●PHP数组函数
$temp= array(1,2,3,,,,) 创建一个数组赋值给temp $id=range(1,6,2); 成长值 1到6 跨度为2 就是3个长度数组 也可以是字符“a” &quo ...
- 详解-制作根文件系统,并使用yaffs,jffs,nfs挂载系统(2)
1.安装mkyaffsimage, mkyaffs2image命令(用来制作yaffs文件系统) 第一个命令针对Flash小页512B,第二个针对Flash大页2KB首先下载压缩文件 yaffs_so ...
- PS换脸操作
1,使用套索工具抠出人的五官. 2,Ctrl+C复制黏贴到另一张头像中,调节透明度50%,与需要换脸的头像的眼睛,嘴巴,鼻子重合,透明度回归100%. 3,为了不该变原图,需要新建一张原图. 4,在抠 ...