楼主: 9248_1572342225
813 0

[学习笔记] 【学习笔记】from functions import * from gradient import numerical_gradi ... [推广有奖]

  • 0关注
  • 0粉丝

本科生

83%

还不是VIP/贵宾

-

威望
0
论坛币
40 个
通用积分
14.1000
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
510 点
帖子
91
精华
0
在线时间
0 小时
注册时间
2019-10-29
最后登录
2020-1-12

楼主
9248_1572342225 发表于 2020-1-8 09:25:40 来自手机 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
from functions import *
from gradient import numerical_gradient

# 构造的是两层神经网络
class TwoLayerNet:
    # 初始化方法
    def __init__(self, input_size, hidden_size, output_size,
                    weight_init_std=0.01):
    # 初始化权重
        self.params = {}
        # 权重的初始值,使用标准正态分布生成, 再缩小100倍
        self.params[\'W1\'] = weight_init_std * np.random.randn(input_size, hidden_size)
        # 偏置的初始值设置为0
        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)
        
    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)
        a2 = np.dot(z1, W2) + b2
        y = softmax(a2)
        return y
   
    # x:输入数据, t:监督数据
    def loss(self, x, t):
        # 计算损失函数
        y = self.predict(x)
        # 交叉熵误差计算损失函数的值
        return cross_entropy_error(y, t)
    def accuracy(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])
        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
      
    def gradient(self, x, t):
        # 反向传播法 ,backward
        W1, W2 = self.params[\'W1\'], self.params[\'W2\']
        b1, b2 = self.params[\'b1\'], self.params[\'b2\']
        grads = {}
        
        batch_num = x.shape[0]
        
        # forward
        a1 = np.dot(x, W1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        y = softmax(a2)
        
        # backward
        dy = (y - t) / batch_num
        grads[\'W2\'] = np.dot(z1.T, dy)
        grads[\'b2\'] = np.sum(dy, axis=0)
        
        da1 = np.dot(dy, W2.T)
        dz1 = sigmoid_grad(a1) * da1
        grads[\'W1\'] = np.dot(x.T, dz1)
        grads[\'b1\'] = np.sum(dz1, axis=0)

        return grads
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Functions Numerical function gradient numeric

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-20 21:43