forked from Ewenwan/MVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ce6faf
commit 529b790
Showing
19 changed files
with
1,605 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule mxnet
updated
from 87068e to 3bec7f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
MXnet/self_lear_mxnet/2_Multilayer_Percepton_FashionMNIST1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
#-*- coding:utf-8 -*- | ||
# 多层感知机 | ||
# 多层感知机与前面介绍的多类逻辑回归非常类似, | ||
# 主要的区别是我们在输入层和输出层之间插入了一到多个隐含层。 | ||
# 服饰 FashionMNIST 识别 | ||
from mxnet import ndarray as nd | ||
from mxnet import autograd | ||
import random | ||
from mxnet import gluon | ||
|
||
import matplotlib.pyplot as plt#画图 | ||
|
||
import sys | ||
sys.path.append('..') | ||
import utils #包含了自己定义的一些通用函数 如下载 载入数据集等 | ||
########################################################## | ||
#### 准备输入数据 ### | ||
#一个稍微复杂点的数据集,它跟MNIST非常像,但是内容不再是分类数字,而是服饰 | ||
## 准备 训练和测试数据集 | ||
batch_size = 256#每次训练 输入的图片数量 | ||
train_data, test_data = utils.load_data_fashion_mnist(batch_size) | ||
|
||
|
||
########################################################### | ||
### 定义模型 ################## | ||
#@@@@初始化模型参数 权重和偏置@@@@ | ||
num_inputs = 28*28##输入为图像尺寸 28*28 | ||
num_outputs = 10#输出10个标签 | ||
|
||
num_hidden = 256#定义一个只有一个隐含层的模型,这个隐含层输出256个节点 | ||
weight_scale = .01#初始化权重参数的 均匀分布均值 | ||
#输入到隐含层 权重 + 偏置 | ||
W1 = nd.random_normal(shape=(num_inputs, num_hidden), scale=weight_scale) | ||
b1 = nd.zeros(num_hidden) | ||
#隐含层到输出层 权重 + 偏置 | ||
W2 = nd.random_normal(shape=(num_hidden, num_outputs), scale=weight_scale) | ||
b2 = nd.zeros(num_outputs) | ||
params = [W1, b1, W2, b2]#所有参数 | ||
for param in params:#参数添加自动求导 | ||
param.attach_grad() | ||
|
||
##非线性激活函数 | ||
#为了让我们的模型可以拟合非线性函数,我们需要在层之间插入非线性的激活函数。 | ||
def relu(X): | ||
return nd.maximum(X, 0) | ||
### 定义模型 | ||
def net(X): | ||
X = X.reshape((-1, num_inputs)) | ||
h1 = relu(nd.dot(X, W1) + b1)# 隐含层输出 非线性激活 | ||
output = nd.dot(h1, W2) + b2 | ||
return output | ||
|
||
##Softmax和交叉熵损失函数 | ||
## softmax 回归实现 exp(Xi)/(sum(exp(Xi))) 归一化概率 使得 10类概率之和为1 | ||
#交叉熵损失函数 | ||
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss() | ||
|
||
## 开始训练 | ||
learning_rate = .5#学习率 | ||
epochs = 7 ##训练迭代训练集 次数 | ||
for epoch in range(epochs):##每迭代一次训练集 | ||
train_loss = 0.##损失 | ||
train_acc = 0. ##准确度 | ||
for data, label in train_data:#训练集 | ||
with autograd.record():#自动微分 | ||
output = net(data)#模型输出 向前传播 | ||
loss = softmax_cross_entropy(output, label)#计算损失 | ||
loss.backward()#向后传播 | ||
utils.SGD(params, learning_rate/batch_size)#随机梯度下降 训练更新参数 学习率递减 | ||
|
||
train_loss += nd.mean(loss).asscalar()#损失 | ||
train_acc += utils.accuracy(output, label)#准确度 | ||
|
||
test_acc = utils.evaluate_accuracy(test_data, net)#测试集测试 | ||
print("E次数 %d. 损失: %f, 训练准确度 %f, 测试准确度%f" % ( | ||
epoch, train_loss/len(train_data), | ||
train_acc/len(train_data), test_acc)) | ||
|
50 changes: 50 additions & 0 deletions
50
MXnet/self_lear_mxnet/2_Multilayer_Percepton_FashionMNIST2_glon.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python | ||
#-*- coding:utf-8 -*- | ||
# 多层感知机 使用glon库 | ||
# 多层感知机与前面介绍的多类逻辑回归非常类似, | ||
# 主要的区别是我们在输入层和输出层之间插入了一到多个隐含层。 | ||
# 服饰 FashionMNIST 识别 | ||
|
||
from mxnet import gluon | ||
## 定义模型 | ||
net = gluon.nn.Sequential()#空模型 | ||
with net.name_scope(): | ||
net.add(gluon.nn.Flatten())##数据变形 | ||
net.add(gluon.nn.Dense(256, activation="relu"))##中间隐含层 非线性激活 | ||
net.add(gluon.nn.Dense(10))#输出层 | ||
net.initialize()#模型初始化 | ||
|
||
##读取数据并训练 | ||
import sys | ||
sys.path.append('..') | ||
from mxnet import ndarray as nd | ||
from mxnet import autograd | ||
import utils#包含了自己定义的一些通用函数 如下载 载入数据集等 | ||
|
||
batch_size = 256#每次训练导入的图片数量 | ||
train_data, test_data = utils.load_data_fashion_mnist(batch_size)#载入数据 | ||
##Softmax和交叉熵损失函数 | ||
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()##损失函数 | ||
## 优化训练函数 随机梯度下降 | ||
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.5}) | ||
## 开始训练 | ||
#learning_rate = .5#学习率 | ||
epochs = 7 ##训练迭代训练集 次数 | ||
for epoch in range(epochs): | ||
train_loss = 0.##损失 | ||
train_acc = 0. ##准确度 | ||
for data, label in train_data:#训练集 | ||
with autograd.record():#自动微分 | ||
output = net(data)#模型输出 向前传播 | ||
loss = softmax_cross_entropy(output, label)#计算损失 | ||
loss.backward()#向后传播 | ||
trainer.step(batch_size)#随机梯度下降 训练更新参数 学习率递减 | ||
|
||
train_loss += nd.mean(loss).asscalar()#损失 | ||
train_acc += utils.accuracy(output, label)#准确度 | ||
|
||
test_acc = utils.evaluate_accuracy(test_data, net)#测试集测试 | ||
print("E次数 %d. 损失: %f, 训练准确度 %f, 测试准确度%f" % ( | ||
epoch, train_loss/len(train_data), | ||
train_acc/len(train_data), test_acc)) | ||
|
82 changes: 82 additions & 0 deletions
82
MXnet/self_lear_mxnet/2_Multilayer_Percepton_MNIST3_use_gluon.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/env python | ||
#-*- coding:utf-8 -*- | ||
# 多类别逻辑回归 gluon 实现 | ||
# 手写字体MNIST 多层感知器Multilayer Percepton (MLP)识别 | ||
# 多层神经网络 | ||
|
||
import mxnet as mx | ||
from mxnet import gluon, autograd, ndarray | ||
import numpy as np | ||
|
||
import sys | ||
sys.path.append('..') | ||
import utils #包含了自己定义的一些通用函数 如下载 载入数据集等 | ||
|
||
########################################################## | ||
#### 准备输入数据 ### | ||
#我们通过gluon的data.vision模块自动下载这个数据 | ||
batch_size = 256#每次训练 输入的图片数量 | ||
train_data, test_data = utils.load_data_mnist(batch_size) | ||
''' | ||
def transform(data, label): | ||
return data.astype('float32')/255, label.astype('float32') | ||
#下载数据 | ||
#mnist_train = gluon.data.vision.FashionMNIST(train=True, transform=transform) | ||
#mnist_test = gluon.data.vision.FashionMNIST(train=False, transform=transform) | ||
mnist_train = gluon.data.vision.MNIST(train=True, transform=transform) | ||
mnist_test = gluon.data.vision.MNIST(train=False, transform=transform) | ||
# 使用gluon.data.DataLoader载入训练数据和测试数据。 | ||
# 这个DataLoader是一个iterator对象类,非常适合处理规模较大的数据集。 | ||
train_data = gluon.data.DataLoader(mnist_train, batch_size=32, shuffle=True) | ||
test_data = gluon.data.DataLoader(train_data, batch_size=32, shuffle=False) | ||
''' | ||
|
||
########################################### | ||
####定义模型 | ||
# 先把模型做个初始化 | ||
net = gluon.nn.Sequential() | ||
# 然后定义模型架构 | ||
with net.name_scope(): ##线性模型就是使用对应的Dense层 | ||
net.add(gluon.nn.Dense(128, activation="relu")) # 第一层设置128个节点 | ||
net.add(gluon.nn.Dense(64, activation="relu")) # 第二层设置64个节点 | ||
net.add(gluon.nn.Dense(10)) # 输出层 | ||
|
||
|
||
|
||
############################################# | ||
##### 设置参数 | ||
# 先随机设置模型参数 | ||
# 数值从一个标准差为0.05正态分布曲线里面取 | ||
net.collect_params().initialize(mx.init.Normal(sigma=0.05)) | ||
|
||
|
||
#### 使用softmax cross entropy loss算法 | ||
# Softmax和交叉熵损失函数 | ||
# softmax 回归实现 exp(Xi)/(sum(exp(Xi))) 归一化概率 使得 10类概率之和为1 | ||
# 交叉熵损失函数 将两个概率分布的负交叉熵作为目标值,最小化这个值等价于最大化这两个概率的相似度 | ||
# 计算模型的预测能力 | ||
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss() | ||
|
||
### 优化模型 | ||
# 使用随机梯度下降算法(sgd)进行训练 | ||
# 并且将学习率的超参数设置为 .1 | ||
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1}) | ||
|
||
epochs = 10 ##训练 | ||
for e in range(epochs):#每一次训练整个训练集 | ||
train_loss = 0.# 损失 | ||
train_acc = 0. #准确度 | ||
for i, (data, label) in enumerate(train_data): ##训练集里的 每一批次样本和标签 | ||
data = data.as_in_context(mx.cpu()).reshape((-1, 784)) ## 28*28 转成 1*784 | ||
label = label.as_in_context(mx.cpu()) | ||
with autograd.record(): # 自动求微分 | ||
output = net(data) # 模型输出 向前传播 | ||
loss = softmax_cross_entropy(output, label)## 计算误差 | ||
loss.backward() # 向后传播 | ||
trainer.step(data.shape[0]) # 优化模型参数 data.shape[0] = batch_size | ||
# Provide stats on the improvement of the model over each epoch | ||
train_loss += ndarray.mean(loss).asscalar() ## 当前的误差损失 均值 | ||
train_acc += utils.accuracy(output, label) #准确度 | ||
test_acc = utils.evaluate_accuracy(test_data, net)#验证数据集的准确度 | ||
print("遍历训练集次数 {}. 训练误差: {}. 训练准确度: {}. 测试准确度: {}.".format( | ||
e, train_loss/len(train_data),train_acc/len(train_data), test_acc)) |
Oops, something went wrong.