keras:1)初体验-MLP神经网络实现MNIST手写识别
来源:互联网 发布:网络测试是什么 编辑:程序博客网 时间:2024/05/16 07:21
Keras是基于Theano和TensorFlow的深度学习库,具体介绍请参考
官方网:https://keras.io/
中文文档:http://keras-cn.readthedocs.io/en/latest/
先贴出官方在Git的源码例子minist_mlp,可以和之前对应的tensorflow博文tensorflow:1)简单的神经网络对比学习
'''Trains a simple deep NN on the MNIST dataset.Gets to 98.40% test accuracy after 20 epochs(there is *a lot* of margin for parameter tuning).2 seconds per epoch on a K520 GPU.'''from __future__ import print_functionimport kerasfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Dense, Dropoutfrom keras.optimizers import RMSpropbatch_size = 128num_classes = 10epochs = 20# the data, shuffled and split between train and test sets(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train = x_train.reshape(60000, 784)x_test = x_test.reshape(10000, 784)x_train = x_train.astype('float32')x_test = x_test.astype('float32')#除以255,把数据正则化到0~1之间x_train /= 255x_test /= 255print(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)model = Sequential()model.add(Dense(512, activation='relu', input_shape=(784,)))model.add(Dropout(0.2))model.add(Dense(512, activation='relu'))model.add(Dropout(0.2))model.add(Dense(10, activation='softmax'))model.summary()model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])#verbose=1可以显示每一步的信息history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1)score = model.evaluate(x_test, y_test, verbose=0)print('Test loss:', score[0])print('Test accuracy:', score[1])
Q1:
源码需要下载10MB大小的MNIST数据集,但是国内好像下载不来(原因,你懂得) ,下面提供mnist.npz下载地址。但注意需放到~.keras\datasets目录下,比如win10的C:\Users\Javis.keras\datasets
Q2:
Dense(512, activation='relu', input_shape=(784,))
可以先看下这个类的注释说明:
"""Just your regular densely-connected NN layer. `Dense` implements the operation: `output = activation(dot(input, kernel) + bias)` where `activation` is the element-wise activation function passed as the `activation` argument, `kernel` is a weights matrix created by the layer, and `bias` is a bias vector created by the layer (only applicable if `use_bias` is `True`). Note: if the input to the layer has a rank greater than 2, then it is flattened prior to the initial dot product with `kernel`. # Example ```python # as first layer in a sequential model: model = Sequential() model.add(Dense(32, input_shape=(16,))) # now the model will take as input arrays of shape (*, 16) # and output arrays of shape (*, 32) # after the first layer, you don't need to specify # the size of the input anymore: model.add(Dense(32))
注释非常给力,densely-connected就是之前说是的全连接层
Q3:
model.summary()
可以显示网络的结构,很人性化有木有
_________________________________________________________________Layer (type) Output Shape Param # =================================================================dense_1 (Dense) (None, 512) 401920 _________________________________________________________________dropout_1 (Dropout) (None, 512) 0 _________________________________________________________________dense_2 (Dense) (None, 512) 262656 _________________________________________________________________dropout_2 (Dropout) (None, 512) 0 _________________________________________________________________dense_3 (Dense) (None, 10) 5130 =================================================================
阅读全文
0 0
- keras:1)初体验-MLP神经网络实现MNIST手写识别
- Keras 入门课1 -- 用MLP识别mnist手写字符
- 神经网络实现手写数字识别(MNIST)
- 神经网络-tensorflow实现mnist手写数字识别
- Tensorflow训练神经网络实现mnist手写识别
- tensorflow中MLP识别mnist手写数字
- 【TensorFlow】神经网络MNIST手写识别
- Keras 浅尝之MNIST手写数字识别
- tensorflow 学习笔记7 普通神经网络实现mnist手写识别
- 使用tensorflow卷积神经网络实现mnist手写数字识别
- keras/构建卷积神经网络识别mnist
- [Keras实战] 构建LeNet实现手写数字识别(mnist数据集)
- 使用Keras构建神经网络进行Mnist手写字体分类
- keras 手把手入门#1-MNIST手写数字识别 深度学习实战闪电入门
- Keras中将LSTM用于mnist手写数字识别
- Keras入门课2 -- 使用CNN识别mnist手写数字
- 用Tensorflow搭建CNN卷积神经网络,实现MNIST手写数字识别
- 利用tensorflow一步一步实现基于MNIST 数据集进行手写数字识别的神经网络,逻辑回归
- 点、线导出含高程值的表格
- android笔记201707
- 4_2 私有继承的访问规则举例2
- [RK3288][Android6.0] WiFi之WifiWatchdogStateMachine
- jadclipse安装后不显示
- keras:1)初体验-MLP神经网络实现MNIST手写识别
- OpenCV 学习方法
- 阿牛的EOF牛肉串
- 深拷贝、浅拷贝
- MongoDB 简练命令
- mybatis进阶
- ORACLE 去除多余的重复记录语句
- 安卓中白天模式和夜间模式的切换
- 使用snmp协议获取MIB的数据