这是一个来自官网的示例:https://github.com/keras-team/keras/blob/master/examples/antirectifier.py

与之前的MINST手写数字识别全连接网络相比,只是本实例使用antirectifier替换ReLU激活函数.

 '''The example demonstrates how to write custom layers for Keras.
# Keras自定义层编写示范 We build a custom activation layer called 'Antirectifier',
建立了一个自定义的激活 'Antirectifier'(反校正) which modifies the shape of the tensor that passes through it.
它修改通过它的张量的形状。 We need to specify two methods: `compute_output_shape` and `call`.
需要指定两种方法: `compute_output_shape` and `call`. Note that the same result can also be achieved via a Lambda layer.
注意,同样的结果也可以通过Lambda层来实现 Because our custom layer is written with primitives from the Keras
自定义层是用keras底层编写的, backend (`K`), our code can run both on TensorFlow and Theano.
代码可基于TensorFlow and Theano框架运行
''' from __future__ import print_function
import keras
from keras.models import Sequential
from keras import layers
from keras.datasets import mnist
from keras import backend as K class Antirectifier(layers.Layer): '''This is the combination of a sample-wise
L2 normalization with the concatenation of the
positive part of the input with the negative part
of the input. The result is a tensor of samples that are
twice as large as the input samples.
这是示例性的L2归一化与输入的正样本与输入的负样本的级联的组合。结果张量样本是输入样本的两倍大。 It can be used in place of a ReLU.
可以使用RelU(Rectified Linear Unit,线性整流函数, 激活函数)替换 # Input shape
输入形状
2D tensor of shape (samples, n)
形状的2维张量 # Output shape
输出形状
2D tensor of shape (samples, 2*n)
形状的2维张量 # Theoretical justification
理论证明
When applying ReLU, assuming that the distribution
of the previous output is approximately centered around 0.,
使用ReLU时,假设前一个输出分布的以0中心分布 you are discarding half of your input. This is inefficient.
放弃了一半的输入。这是低效的。 Antirectifier allows to return all-positive outputs like ReLU,
without discarding any data.
反校正返回了所有正样本输出,像ReLU一样,没有丢弃数据。 Tests on MNIST show that Antirectifier allows to train networks
with twice less parameters yet with comparable
classification accuracy as an equivalent ReLU-based network.
基于MINIST(数据集)训练,展示反校正训练网络和同类ReLU-based网络相比,使用少于2倍的参数参数,但是实现了类似的分类准确度。
''' def compute_output_shape(self, input_shape):
shape = list(input_shape)
assert len(shape) == 2 # only valid for 2D tensors
shape[-1] *= 2
return tuple(shape) def call(self, inputs):
inputs -= K.mean(inputs, axis=1, keepdims=True)
inputs = K.l2_normalize(inputs, axis=1)
pos = K.relu(inputs)
neg = K.relu(-inputs)
return K.concatenate([pos, neg], axis=1) # global parameters
# 全局变量
batch_size = 128
num_classes = 10
epochs = 40 # 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')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples') # convert class vectors to binary class matrices
# 类别向量转为多分类矩阵
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes) # build the model
# 建立模型
model = Sequential()
model.add(layers.Dense(256, input_shape=(784,)))
model.add(Antirectifier())
model.add(layers.Dropout(0.1))
model.add(layers.Dense(256))
model.add(Antirectifier())
model.add(layers.Dropout(0.1))
model.add(layers.Dense(num_classes))
model.add(layers.Activation('softmax')) # compile the model
# 编译模型
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
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)) # next, compare with an equivalent network
# with2x bigger Dense layers and ReLU
# 下一步,使用同结构网络比较,该网络有2倍打的全连接层和ReLU激活函数

执行结果:

60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/4
60000/60000 [==============================] - 4s 62us/step - loss: 0.6030 - acc: 0.9131 - val_loss: 0.1637 - val_acc: 0.9565
Epoch 2/4
60000/60000 [==============================] - 3s 58us/step - loss: 0.1264 - acc: 0.9652 - val_loss: 0.0910 - val_acc: 0.9730
Epoch 3/4
60000/60000 [==============================] - 3s 57us/step - loss: 0.0822 - acc: 0.9762 - val_loss: 0.0836 - val_acc: 0.9757
Epoch 4/4
60000/60000 [==============================] - 3s 57us/step - loss: 0.0638 - acc: 0.9810 - val_loss: 0.0762 - val_acc: 0.9780
<keras.callbacks.History at 0x7f355fba6c88>

评估模型:

score = model.evaluate(x_test, y_test,
verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])

评估结果:

10000/10000 [==============================] - 1s 59us/step
Test score: 0.07624727148264647
Test accuracy: 0.978

参考链接:

1、https://github.com/keras-team/keras/blob/master/examples/antirectifier.py

2、https://blog.csdn.net/wyx100/article/details/80678735

MINST手写数字识别(三)—— 使用antirectifier替换ReLU激活函数的更多相关文章

  1. MINST手写数字识别(一)—— 全连接网络

    这是一个简单快速入门教程——用Keras搭建神经网络实现手写数字识别,它大部分基于Keras的源代码示例 minst_mlp.py. 1.安装依赖库 首先,你需要安装最近版本的Python,再加上一些 ...

  2. MINST手写数字识别(二)—— 卷积神经网络(CNN)

    今天我们的主角是keras,其简洁性和易用性简直出乎David 9我的预期.大家都知道keras是在TensorFlow上又包装了一层,向简洁易用的深度学习又迈出了坚实的一步. 所以,今天就来带大家写 ...

  3. 【TensorFlow-windows】(三) 多层感知器进行手写数字识别(mnist)

    主要内容: 1.基于多层感知器的mnist手写数字识别(代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...

  4. 【PaddlePaddle系列】手写数字识别

      最近百度为了推广自家编写对深度学习框架PaddlePaddle不断推出各种比赛.百度声称PaddlePaddle是一个“易学.易用”的开源深度学习框架,然而网上的资料少之又少.虽然百度很用心地提供 ...

  5. C#中调用Matlab人工神经网络算法实现手写数字识别

    手写数字识别实现 设计技术参数:通过由数字构成的图像,自动实现几个不同数字的识别,设计识别方法,有较高的识别率 关键字:二值化  投影  矩阵  目标定位  Matlab 手写数字图像识别简介: 手写 ...

  6. CNN 手写数字识别

    1. 知识点准备 在了解 CNN 网络神经之前有两个概念要理解,第一是二维图像上卷积的概念,第二是 pooling 的概念. a. 卷积 关于卷积的概念和细节可以参考这里,卷积运算有两个非常重要特性, ...

  7. 【深度学习系列】手写数字识别卷积神经--卷积神经网络CNN原理详解(一)

    上篇文章我们给出了用paddlepaddle来做手写数字识别的示例,并对网络结构进行到了调整,提高了识别的精度.有的同学表示不是很理解原理,为什么传统的机器学习算法,简单的神经网络(如多层感知机)都可 ...

  8. 机器学习(二)-kNN手写数字识别

    一.kNN算法是机器学习的入门算法,其中不涉及训练,主要思想是计算待测点和参照点的距离,选取距离较近的参照点的类别作为待测点的的类别. 1,距离可以是欧式距离,夹角余弦距离等等. 2,k值不能选择太大 ...

  9. 手写数字识别 ----卷积神经网络模型官方案例注释(基于Tensorflow,Python)

    # 手写数字识别 ----卷积神经网络模型 import os import tensorflow as tf #部分注释来源于 # http://www.cnblogs.com/rgvb178/p/ ...

随机推荐

  1. c++友元函数友元类

    友元函数和友元类的需要: 类具有封装和信息隐藏的特性.只有类的成员函数才能访问类的私有成员,程序中的其他函数是无法访问私有成员的.非成员函数可以访问类中的公有成员,但是如果将数据成员都定义为公有的,这 ...

  2. 20个Flutter实例视频教程-第05节: 酷炫的路由动画-1

    视屏地址: https://www.bilibili.com/video/av39709290/?p=5 博客地址: https://jspang.com/post/flutterDemo.html# ...

  3. Flutter实战视频-移动电商-04.底部导航栏切换效果

    04.底部导航栏切换效果 博客地址: https://jspang.com/post/FlutterShop.html#toc-291 我们要做的效果图: 新建四个页面 home_page.dart ...

  4. 关于weblogic 10.3.6.0 的漏洞复现(2)

    今天小R又学会了一个工具的使用,而且这个工具很强大很强大. 待会介绍. 一.需要的试验环境: 一台宿主机,虚拟机(kali+window2008或其他版本的windows)  1.宿主机需要的工具:B ...

  5. 真正可用的Asdoc生成工具及技巧和注意事项

    经过一个上午的折腾,终于总结出一套完全能够正确运行得到asdoc api的工具.使用方便,里面写了许多注意事项,帮你顺利完成 api 的生成. ::        注意: ::        1.所以 ...

  6. 一个例子读懂 JS 异步编程: Callback / Promise / Generator / Async

    JS异步编程实践理解 回顾JS异步编程方法的发展,主要有以下几种方式: Callback Promise Generator Async 需求 显示购物车商品列表的页面,用户可以勾选想要删除商品(单选 ...

  7. GIL 已经被杀死了么?

    GIL 已经被杀死了么? 本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/8KvQemz0SWq2hw-2aBPv2Q ...

  8. HDU - 1019 - Least Common Multiple - 质因数分解

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++ ...

  9. vm安装mac

    需要 vm虚拟机:vm10 mac系统:   mac10.9 vm安装mac补丁 :    unlokc-all-v120 vm tools for mac10.9: darwin6.0.3.iso ...

  10. [Xcode 实际操作]九、实用进阶-(16)给图片添加水印效果

    目录:[Swift]Xcode实际操作 本文将演示如何截取屏幕画面,并将截取图片,存入系统相册. 在项目文件夹[DemoApp]上点击鼠标右键 ->[New File]创建一个扩展文件-> ...