深度学习第一次作业np_mnist实验报告,:np_mnist实验报告,深度学习初探有何独到之处?
摘要:任务描述 用 numpy 实现训练 MLP 网络识别手写数字 MNIST 最终版本:网络结构 (text{[784, 512, 256, 10]}),损失函数设定为交叉熵函数 cross_entropy,激活函数设定为 ReLU,输出
任务描述
用 numpy 实现训练 MLP 网络识别手写数字 MNIST
最终版本:网络结构 \(\text{[784, 512, 256, 10]}\),损失函数设定为交叉熵函数 cross_entropy,激活函数设定为 ReLU,输出层激活函数采用 softmax,学习率 0.01,最终在测试集上准确率为 \(95.51\%\)
代码 np_mnist.py 实现了三种激活函数 ReLU,tanh,sigmoid,分别设定了不同的权重初始化函数 init_weights,输出三种情况的损失值,准确率
其中 ReLU 效果最好
如图所示
剩下两种情况如下,明显不如 RuLU
也尝试过不同的网络结构,如 \(\text{[784, 256, 10],[784, 256, 128, 10],[784, 512, 128, 10]}\)
最终 \(\text{[784, 512, 256, 10]}\) 效果最好
\(\text{np_mnist.py}\)
# -*- coding: utf-8 -*-
"""
@ author: Yuanze Lei
"""
# 作业内容:更改loss函数、网络结构、激活函数,完成训练MLP网络识别手写数字MNIST数据集
import numpy as np
from tqdm import tqdm
# 加载数据集,numpy格式
X_train = np.load('./mnist/X_train.npy') # (60000, 784), 数值在0.0~1.0之间
y_train = np.load('./mnist/y_train.npy') # (60000, )
y_train = np.eye(10)[y_train] # (60000, 10), one-hot编码
X_val = np.load('./mnist/X_val.npy') # (10000, 784), 数值在0.0~1.0之间
y_val = np.load('./mnist/y_val.npy') # (10000,)
y_val = np.eye(10)[y_val] # (10000, 10), one-hot编码
X_test = np.load('./mnist/X_test.npy') # (10000, 784), 数值在0.0~1.0之间
y_test = np.load('./mnist/y_test.npy') # (10000,)
y_test = np.eye(10)[y_test] # (10000, 10), one-hot编码
# 定义激活函数
def ReLU(x):
return np.maximum(0, x)
def ReLU_prime(x):
return (x > 0).astype(float)
def sigmoid(x):
return 1 / (1 + np.exp(-np.clip(x, -500, 500)))
def sigmoid_prime(x):
s = sigmoid(x)
return s * (1 - s)
def tanh(x):
return np.tanh(x)
def tanh_prime(x):
return 1 - np.tanh(x) ** 2
#输出层激活函数
def softmax(x):
exp_x = np.exp(x - np.max(x, axis=1, keepdims=True))
return exp_x / (np.sum(exp_x, axis=1, keepdims=True) + 1e-8)
def softmax_prime(x):
s = softmax(x)
return s * (1 - s)
# 定义损失函数
def cross_entropy_loss(y_true, y_pred):
y_pred = np.clip(y_pred, 1e-8, 1-1e-8)
return -np.mean(np.sum(y_true * np.log(y_pred), axis=1))
def cross_entropy_loss_prime(y_true, y_pred):
return y_pred - y_true
# 定义权重初始化函数
def init_weights(shape=(), activation='ReLU'):
if activation == 'ReLU': z = 2.0 /
