利用keras搭建神经卷积网络(CNN)
来源:互联网 发布:论坛刷积分软件 编辑:程序博客网 时间:2024/05/21 06:43
CNN
因为之前上课的时候对CNN学的不是很好,所以在这次训练之前我在知乎上找到一篇对CNN讲解的文章,先进行阅读了一番。来自机器之心的一篇文章
http://mp.weixin.qq.com/s?__biz=MzA3MzI4MjgzMw==&mid=2650717691&idx=2&sn=3f0b66aa9706aae1a30b01309aa0214c#rd
https://www.zhihu.com/question/52668301
阅读文章后,对代码理解就很easy了,我没有用莫烦教程里的代码,因为那个准确率没有官方文档给的mnist_cnn的代码高,贴一下官方代码地址https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py
#!/usr/bin/python# -*- coding:utf8 -*-'''Trains a simple convnet on the MNIST dataset.Gets to 99.25% test accuracy after 12 epochs(there is still a lot of margin for parameter tuning).16 seconds per epoch on a GRID K520 GPU.'''from __future__ import print_functionimport kerasfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2Dfrom keras import backend as Kbatch_size = 128num_classes = 10epochs = 12# pretreatment 预处理# input image dimensionsimg_rows, img_cols = 28, 28# the data, shuffled and split between train and test sets(x_train, y_train), (x_test, y_test) = mnist.load_data()if K.image_data_format() == 'channels_first': #判断图片格式是 channel在前还是在后(channel:黑白为1,彩色为3) x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) #shape[0]指例子的个数 x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols)else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1)x_train = x_train.astype('float32')x_test = x_test.astype('float32')x_train /= 255x_test /= 255print('x_train shape:', x_train.shape)print(x_train.shape[0], 'train samples')print(x_test.shape[0], 'test samples')# convert class vectors to binary class matricesy_train = keras.utils.to_categorical(y_train, num_classes)y_test = keras.utils.to_categorical(y_test, num_classes)# build the neural net 建模型(卷积—relu-卷积-relu-池化-relu-卷积-relu-池化-全连接)model = Sequential()model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) # 32个过滤器,过滤器大小是3×3,32×26×26model.add(Conv2D(64, (3, 3), activation='relu')) #64×24×24model.add(MaxPooling2D(pool_size=(2, 2)))# 向下取样model.add(Dropout(0.25))model.add(Flatten()) #降维:将64×12×12降为1维(即把他们相乘起来)model.add(Dense(128, activation='relu'))model.add(Dropout(0.5))model.add(Dense(num_classes, activation='softmax')) #全连接2层# compile the model 编译模型model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])# train the model 训练模型model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))# test the model 测试模型score = model.evaluate(x_test, y_test, verbose=0)print('Test loss:', score[0])print('Test accuracy:', score[1])
训练结果
经过12个epoch后
阅读全文
0 0
- 利用keras搭建神经卷积网络(CNN)
- 卷积神经滤波CNN(一)
- 卷积神经网路(CNN)
- Keras CNN卷积神经网络(三)
- 利用Keras实现多层感知器(MLP)模型和卷积神经网络(CNN)模型并对手写数字图像分类
- 利用tensorflow keras搭建一个简单的卷积神经网络
- 深度神经卷积网络总结
- Keras上实现卷积神经网络CNN
- TensorFlow学习(九):卷积网络CNN
- TensorFlow 中的卷积网络(cnn)
- CNN卷积网络(概念介绍)
- 利用keras(tensorflow) 做cnn mnist识别
- 利用keras(tensorflow) 做cnn mnist识别
- 深度学习之:CNN卷积神经网基本概念理解
- 深度学习 —— 卷积神经网路 CNN
- keras中Convolution1D的使用(CNN情感分析yoom例子四) && Keras 1D,2D,3D卷积
- TensorFlow搭建CNN卷积神经网络
- cnn:深入理解卷积网络
- Android Framework学习(十三)之启动ServiceManager
- 教程学习 01-Building a RESTful Web Service 学习笔记
- ffmpeg常用命令(更新中)
- noip2004提高组
- 终端常用命令
- 利用keras搭建神经卷积网络(CNN)
- 比特币病毒来了,您是否注重过安全技术知识
- 隐马尔可夫模型(HMM)
- c++(函数返回值的及其优化)
- 我们这一代人的困惑
- Android 微信刷新&自带下拉刷新SwipeRefreshLayout(一)
- Android 加快编译时间
- 文章标题
- 域的组建、域的用户和组的管理