数据挖掘实战(十二)--用深度学习方法为图像中的物体进行分类

本文介绍了使用深度学习库Lasagne和nolearn构建神经网络,涉及Theano的基础、卷积层和池化层的概念,以及如何创建和训练模型。此外,还展示了用Keras处理MNIST数据集的示例,包括数据预处理和模型构建过程。
部署运行你感兴趣的模型镜像

一、深度神经网络

  1. 简介

深度神经网络和基本神经网络的差别在于规模大小。至少包含两层隐含层的神经网络被称为深度神经网络。

  1. 实现

主要由Lasagnenolearn两个库来完成规模更大,定制化程度更高的神经网络。这两个库依赖于擅长处理数学表达式的Theano

Theano库:创建和运行数学表达式的工具。在Theano中,我们定义函数要做什么而不是怎么做 ;Theano能只在需要时对表达式求值,而不是定义式。

Lasagne库:专门用来构建神经网络的,使用Theano进行计算。Lasagne实现了内置网络层、删除层、噪音层、卷积层和池化层等。

卷积层使用少量相 互连接的神经元,分析一部分输入值(比如我们这里的一张图像),便于神经网络实现对数据的 标准转换,比如对图像数据的转换 。

池 化层接收某个区域最大输出值,可以降低图像中的微小变动带来的噪音,减少(down-sample, 降采样)信息量,这样后续各层所需工作量也会相应减少 。

  1. 用Lasagne创建一个简单的卷积神经网络

# 1、加载数据集
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data.astype(np.float32)
# Lasagne对数据类型有特殊要求,因此,需要把类别值转换为int32类型(在原始数据集中用int64类型存储)
y_true = iris.target.astype(np.int32)

# 划分训练集和验证集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y_true, random_state=14)

# 2、创建卷积神经网络(数据集有4个特征和3个类别)
import lasagne
input_layer = lasagne.layers.InputLayer(shape=(10, X.shape[1]))
hidden_layer = lasagne.layers.DenseLayer(input_layer, num_units=12, nonlinearity=lasagne.nonlinearities.sigmoid)
output_layer = lasagne.layers.DenseLayer(hidden_layer, num_units=3, nonlinearity=lasagne.nonlinearities.softmax)
# 定义几个Theano训练函数训练创建的网络。
import theano.tensor as T
net_input = T.matrix('net_input')
net_output = output_layer.get_output(net_input)
true_output = T.ivector('true_output')
# 定义损失函数
loss = T.mean(T.nnet.categorical_crossentropy(net_output, true_output))
# 定义修改网络权重的函数
all_params = lasagne.layers.get_all_params(output_layer)
updates = lasagne.updates.sgd(loss, all_params, learning_rate=0.1)
# 创建两个Theano函数,一个训练网络,一个获取网络输出
import theano
train = theano.function([net_input, true_output], loss, updates=updates)
get_output = theano.function([net_input

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.9

TensorFlow-v2.9

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值