深度学习:二层神经网络的类

common.functions.py

# coding: utf-8
import numpy as np


def identity_function(x):
    return x


def step_function(x):
    return np.array(x > 0, dtype=np.int)


def sigmoid(x):
    return 1 / (1 + np.exp(-x))    


def sigmoid_grad(x):
    return (1.0 - sigmoid(x)) * sigmoid(x)
    

def relu(x):
    return np.maximum(0, x)


def relu_grad(x):
    grad = np.zeros(x)
    grad[x>=0] = 1
    return grad
    

def softmax(x):
    if x.ndim == 2:
        x = x.T
        x = x - np.max(x, axis=0)
        y = np.exp(x) / np.sum(np.exp(x), axis=0)
        return y.T 

    x = x - np.max(x) # 溢出对策
    return np.exp(x) / np.sum(np.exp(x))


def mean_squared_error(y, t):
    return 0.5 * np.sum((y-t)**2)


def cross_entropy_error(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
        
    # 监督数据是one-hot-vector的情况下,转换为正确解标签的索引
    if t.size == y.size:
        t = t.argmax(axis=1)
             
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size


def softmax_loss(X, t):
    y = softmax(X)
    return cross_entropy_error(y, t)
common.gradient.py
# coding: utf-8
import numpy as np

def _numerical_gradient_1d(f, x):
    h = 1e-4 # 0.0001
    grad = np.zeros_like(x)
    
    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = float(tmp_val) + h
        fxh1 = f(x) # f(x+h)
        
        x[idx] = tmp_val - h 
        fxh2 = f(x) # f(x-h)
        grad[idx] = (fxh1 - fxh2) / (2*h)
        
        x[idx] = tmp_val # 还原值
        
    return grad


def numerical_gradient_2d(f, X):
    if X.ndim == 1:
        return _numerical_gradient_1d(f, X)
    else:
        grad = np.zeros_like(X)
        
        for idx, x in enumerate(X):
            grad[idx] = _numerical_gradient_1d(f, x)
        
        return grad


def numerical_gradient(f, x):
    h = 1e-4 # 0.0001
    grad = np.zeros_like(x)
    
    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
    while not it.finished:
        idx = it.multi_index
        tmp_val = x[idx]
        x[idx] = float(tmp_val) + h
        fxh1 = f(x) # f(x+h)
        
        x[idx] = tmp_val - h 
        fxh2 = f(x) # f(x-h)
        grad[idx] = (fxh1 - fxh2) / (2*h)
        
        x[idx] = tmp_val # 还原值
        it.iternext()   
        
    return grad

类的实现:

import sys,os
sys.path.append(os.pardir)
from common.functions import*
from common.gradient import numerical_gradient
class TwoLayerNet:

    def __init__(self,imput_size,hidden_size,output_size,weight_init_std=0.01):
        #权重初始化
        #随机生成权重和偏置
        self.params={}
        self.params['W1'] = weight_init_std * np.random.randn(imput_size,hidden_size)
        self.params['b1'] = np.zeros(hidden_size)#隐藏层大小
        self.params['W2'] = weight_init_std * np.random.randn(hidden_size,output_size)
        self.params['b2'] = np.zeros(output_size)#输出层大小



    #进行识别推理
    #参数x是图像数据
    def predict(self,x):
        W1,W2 = self.params['W1'],self.params['W2']
        b1,b2 = self.params['b1'],self.params['b2']

        a1 = np.dot(x,W1) + b1
        z1 = sigmoid(a1)#隐藏层激活函数sigmoid
        a2 = np.dot(z1,W2) + b2
        y = softmax(a2)#输出层激活函数

        return y

    #x:输入数据  ,t:监督数据
    def loss(self,x,t):
        y = self.predict(x)
        y = np.argmax(y,axis=1)
        t = np.argmax(t,axis=1)
        #计算识别精度
        accuracy = np.sum(y==t) / float(x.shape[0])#shape[0]表示一共有多少组数据
        return accuracy

    #x:输入数据,t:监督数据
    #计算权重参数的梯度
    def numerical_gradient(self,x,t):#权重梯度
        loss_W = lambda W: self.loss(x,t)
        grads = {}
        grads['W1'] = numerical_gradient(loss_W,self.params['W1'])
        grads['b1'] = numerical_gradient(loss_W,self.params['b1'])
        grads['W2'] = numerical_gradient(loss_W,self.params['W2'])
        grads['b2'] = numerical_gradient(loss_W,self.params['b2'])
        return grads

应用:

net = TwoLayerNet(imput_size=784,hidden_size=100,output_size=10)
print(net.params['W1'].shape)#(784,100)
print(net.params['b1'].shape)#(100,)
print(net.params['W2'].shape)#(100,10)
print(net.params['b2'].shape)#(10,)
#可以看出params中保存了该神经网络所需的全部参数

x = np.random.rand(10,784)#伪输入数据(100)笔
y = net.predict(x)


t = np.random.rand(10,10)#伪正确解标签(100)笔
print(t)
grads = net.numerical_gradient(x,t)#计算梯度
print(grads['W1'].shape)
print(grads['b1'].shape)
print(grads['W2'].shape)
print(grads['b2'].shape)

输出结果

(784, 100)
(100,)
(100, 10)
(10,)
[[0.29694963 0.39172591 0.45395475 0.78773254 0.2699895  0.96166945
  0.58095296 0.68167919 0.14098212 0.61053719]
 [0.15973614 0.64798599 0.94427932 0.30118005 0.55454825 0.26392499
  0.19571352 0.20752753 0.80114141 0.3713859 ]
 [0.36179787 0.27865806 0.52137278 0.23726289 0.80048652 0.71567743
  0.58383638 0.42465049 0.37974454 0.45870188]
 [0.83572802 0.16930842 0.0643046  0.61345084 0.76799399 0.39888309
  0.42063373 0.6873873  0.62777339 0.84477147]
 [0.85905376 0.99702188 0.82595326 0.46600146 0.03853113 0.63084745
  0.48260706 0.4551653  0.11791387 0.46354961]
 [0.76124738 0.47334844 0.74483796 0.12660477 0.34050768 0.41293154
  0.92784762 0.79116959 0.27746968 0.8558673 ]
 [0.44296686 0.13089578 0.65452171 0.63114145 0.39088669 0.56022915
  0.35195681 0.16387344 0.07708389 0.78608756]
 [0.05573513 0.29089227 0.80115905 0.61012422 0.02186971 0.57758427
  0.09351393 0.51152083 0.37361508 0.24740838]
 [0.34903988 0.13903771 0.42187931 0.86032388 0.70094337 0.4707722
  0.79415092 0.75004601 0.76655749 0.8683633 ]
 [0.31643783 0.89042693 0.7583274  0.90587254 0.81948292 0.23047035
  0.32024196 0.67433587 0.77232858 0.62860014]]
(784, 100)
(100,)
(100, 10)
(10,)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值