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及其在现实世界 ...
随机推荐
- TypeError: Cannot red property 'style' of null 错误解决
错误信息如下: JSP代码如下: <c:if test ="${not empty excelErrors}"> <div id="excelError ...
- mysql 开发进阶篇系列 18 MySQL Server(innodb_buffer_pool_size)
从这篇开始,讲innodb存储引擎中,对于几个重要的服务器参数配置.这些参数以innodb_xx 开头. 1. innodb_buffer_pool_size的设置 这个参数定义了innodb存储引擎 ...
- sql server 索引阐述系列八 统计信息
一.概述 sql server在快速查询值时只有索引还不够,还需要知道操作要处理的数据量有多少,从而估算出复杂度,选择一个代价小的执行计划,这样sql server就知道了数据的分布情况.索引的统计值 ...
- 我可能不懂Array.prototype.sort
今天 fix 我们后台系统的一些 bug.系统是基于 beego 和模板开发的,各种前后端代码揉作一团,没有格式,没有 eslint,全局变量满天飞,连 js 代码都有后端的插值,读起来非常 酸爽. ...
- Java NIO的工作方式
1.BIO带来的挑战 BIO即阻塞IO,不管是磁盘IO,还是网络IO,数据在写入OutputStream或者从InputStream读取时都有可能发生阻塞,一旦有阻塞,当前线程将会被挂起,即线程进入非 ...
- springboot情操陶冶-@SpringBootApplication注解解析
承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上对@SpringBootApplication注解作下简单的分析 @SpringBootApplicat ...
- Spring Cloud Stream消费失败后的处理策略(四):重新入队(RabbitMQ)
应用场景 之前我们已经通过<Spring Cloud Stream消费失败后的处理策略(一):自动重试>一文介绍了Spring Cloud Stream默认的消息重试功能.本文将介绍Rab ...
- 第一册:lesson twentynine..
原文:Come in ,Amy. A:Come in B. Shut the door,please. This bedroom's very untidy. B:What must I do Mrs ...
- nginx静态资源文件无法访问,403 forbidden错误
在安装 nginx 服务器后,我想把网站的根目录设置为 /root/www/ ,于是对 nginx 的 nginx.conf 文件进行配置 先打开 nginx.conf #user nobody; w ...
- double在输出为字符串的几种方法效率测试
测试结果: double->none 366msdouble->long 161msdouble->long2 188msdouble->format 564msdouble- ...