资源简介
基于Python+Theano实现的逻辑回归Lenet5源代码,内附有详细注释,让新手尽可能了解每一个函数及变量的作用。这个源代码还需要mlp.py,可以从我的资源中免费下载到:http://download.csdn.net/detail/niuwei22007/9170435
代码片段和文件信息
# -*- coding: utf-8 -*-
import os
import sys
import timeit
import numpy
import theano
import theano.tensor as T
from theano.tensor.signal import downsample
from theano.tensor.nnet import conv
#from logistic_sgd import LogisticRegression load_data
from mlp import HiddenlayerLR as LogisticRegressionload_data
class LeNetConvPoollayer(object):
“““卷积层+采样层“““
def __init__(self rng input filter_shape image_shape poolsize=(2 2)):
“““
rnginput在之前的MLP中已经说过。
filter_shape: 长度为4的元组或list
filter_shape: (过滤器数目 输入特征图数目 过滤器高度 过滤器宽度)
image_shape: 长度为4的元组或list
image_shape: (样本块大小 输入特征图数目 图像高度 图像宽度)
poolsize: 长度为2的元组或list
poolsize: 下采样的shape大小(#rows #cols)
“““
# 断言,确定image_shape的1号元素与filter_shape的1号元素相等
# 因为从以上定义中可以知道,这个元素代表输入特征图的数量
assert image_shape[1] == filter_shape[1]
self.input = input
# prod()返回元素之积。如果filter_shape=(2433)
# 那么filter_shape[1:]=(433)
# prod(filter_shape[1:])=4*3*3=36
fan_in = numpy.prod(filter_shape[1:])
# each unit in the lower layer receives a gradient from:
# “num output feature maps * filter height * filter width“ /
# pooling size
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
numpy.prod(poolsize))
# 用随机均匀分布初始化权值W
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(
numpy.asarray(
rng.uniform(low=-W_bound high=W_bound size=filter_shape)
dtype=theano.config.floatX
)
borrow=True
)
# 每一张输出特征图都有一个一维的偏置值,初始化为0。
b_values = numpy.zeros((filter_shape[0]) dtype=theano.config.floatX)
self.b = theano.shared(value=b_values borrow=True)
# 将输入特征图与过滤器进行卷积操作
conv_out = conv.conv2d(
input=input
filters=self.W
filter_shape=filter_shape
image_shape=image_shape
)
# 用maxpooling方法下采样每一个张特征图
pooled_out = downsample.max_pool_2d(
input=conv_out
ds=poolsize
ignore_border=True
)
# 先把偏置进行张量扩张,由1维扩展为4维张量(1*2*1*1)
# 再把扩展后的偏置累加到采样输出
# 把累加结果送入tanh非线性函数得到本层的网络输出
self.output = T.tanh(pooled_out + self.b.dimshuffle(‘x‘ 0 ‘x‘ ‘x‘))
# store parameters of this layer
self.params = [self.W self.b]
# keep track of model input
self.input = input
def evaluate_lenet5(learning_rate=0.1 n_epochs=200
dataset=‘mnist.pkl.gz‘
nkerns=[20 50] batch_size=500):
“““ Demonstrates lenet on MNIST dataset
实验数据集是MNIST数据集。
:type learning_rate: float
:param learning_rate: learning rate used (factor for the stochastic
g
- 上一篇:Python 中文手册.chm
- 下一篇:pyQt写的一个读取图像的程序,
相关资源
- 人工智能算法实现mnist手写数字识别
- 手写数字识别.ipynb
- OCR:一个有趣的网页版手写数字识别
- mnist手写数字识别数据集npz文件.zip
- 深度学习Theano教程,python语言
- tensorflow手写数字识别完整版.zip
- 神经网络实现简单的手写数字识别
- 手写数字识别:Python+BP神经网络+PYQ
- 基于TensorFlow的手写数字识别程序
- 多层感知器实现,机器学习,神经网
- openCV+PYTHON实现的knn算法手写数字识别
- tensorflow手写数字识别python源码案例
- 基于卷积神经网络的手写数字识别
- 基于python2.7的LeNet5源代码实现
- tensorflow2.0实现mnist手写数字识别代码
- 手写数字识别源码 识别率98%以上 无
- 手写数字识别
- 基于python实现的手写数字识别
- python手写数字识别(包附minist数据集
- CNN实现手写数字识别
- TensorFlow手写数字识别代码
- 基于kNN方法的MNIST手写数字识别Tenso
- Python TensorFlow手写数字识别
评论
共有 条评论