单层神经网络梯度下降

import numpy as np
# 定义softmax函数,softmax:输出0-1之间的数,其和为1,可以用来表征判定是某个类别的概率
def softmax(x):
    
    # x为一维数据时
    if x.ndim == 1:
        return np.exp(x-np.max(x))/np.sum(np.exp(x-np.max(x)))
    
    # x为二维数据时
    elif x.ndim == 2:
        val_num = np.zeros_like(x)
        for i in range(len(x)):
            part_x = x[i]
            val_num[i] = np.exp(part_x-np.max(part_x))/np.sum(np.exp(part_x-np.max(part_x)))
        return val_num
# 定义损失函数cross_entropy_error
def cross_entropy_error(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
    batch_size = y.shape[0]
    return -np.sum(t * np.log(y[np.arange(batch_size)] + 1e-7))/ batch_size
# 梯度下降法求极小值,在本题中,输入x为二维数据

def numerical_gradient(f, x):
    h = 1e-4
    x_shape = x.shape
    grap = np.zeros_like(x.reshape(-1))
    if x.ndim == 2:
        x = x.reshape(-1)
        for idx in range(x.size):
            tmp_val = x[idx]
            x[idx] = tmp_val + h
            fxh1 = f(x)

            x[idx] = tmp_val - h
            fxh2 = f(x)

            grap[idx] = (fxh1 - fxh2) / (h * 2)
            x[idx] = tmp_val
    grap = grap.reshape(x_shape)
    return grap
# 神经网络类

class simpleNet:
    def __init__(self):
        self.W = np.random.randn(2,3)
        
    def predict(self, x):
        return np.dot(x, self.W)
    
    def loss(self, x, t):
        z = self.predict(x)
        y = softmax(z)
        loss = cross_entropy_error(y ,t)
        
        return loss
net = simpleNet()
# 初始W值
net.W
array([[-0.86904767,  0.33483853, -1.22539511],
       [-0.66378371,  0.09628129, -1.07447355]])
x = np.array([0.3, 0.9])
net.predict(x)
array([-0.85811964,  0.18710472, -1.33464473])
t = np.array([0,0,1])
net.loss(x, t)
1.9727877342731845
def f(W):
    return net.loss(x, t)
# 求出loss最小时的W
numerical_gradient(f, net.W)
array([[ 0.06718959,  0.19108966, -0.25827926],
       [ 0.20156878,  0.57326898, -0.77483777]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值