当前位置:   article > 正文

深度学习入门基于python的理论与实现 4章gradient_simplenet.py 完全解析_simplenet代码解读

simplenet代码解读
# coding: utf-8
#深度学习入门基于python的理论与实现 4章gradient_simplenet.py 完全解析
import sys, os
sys.path.append(os.pardir)  # 为了导入父目录中的文件而进行的设定
import numpy as np
from common.functions import softmax, cross_entropy_error
from common.gradient import numerical_gradient

class simpleNet:
    def __init__(self):
        self.W = np.random.randn(2,3)#用高斯分布进行初始化 维度2*3

    def predict(self, x):
        return np.dot(x, self.W)#点积

    def loss(self, x, t):
        z = self.predict(x)#x点积W
        y = softmax(z)#将多个神经元的输出,映射到(0,1)区间内
        loss = cross_entropy_error(y, t)#交叉熵代价函数 y:输出结果。t:监督数据

        return loss

x = np.array([0.6, 0.9])#输入数据
t = np.array([0, 0, 1])#正确结果,监督数据

net = simpleNet()

f = lambda w: net.loss(x, t)#匿名函数输入w输出net.loss(x, t)
dW = numerical_gradient(f, net.W)#求函数梯度(f,net.W)

print(dW)
#函数:交叉熵 对权重W求偏导数即求正确解和输出参数最小值
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

[ 0.46320449 0.04464085 -0.50784534]
[ 0.69480673 0.06696128 -0.76176801]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/537956
推荐阅读
相关标签
  

闽ICP备14008679号