20_感知机(单一感知机,多输出感知机)+ 推导

本部分来自课程+自己整理

1.16.感知机

1.16.1.单一感知机

神经网络最简单的结构就是单输出的单层感知机,单层感知机只有输入层和输出层,分别代表了神经感受器和神经中枢。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
案例:

# -*- coding: UTF-8 -*-

import torch
from torch.nn import functional as F

x = torch.randn(1, 10)
w = torch.randn(1, 10, requires_grad=True)

o = torch.sigmoid(x @ w.t())
print(o.shape)
"""
输出结果:
torch.Size([1, 1])
"""

loss = F.mse_loss(torch.ones(1, 1), o)
print(loss.shape)
"""
输出结果:
torch.Size([])
"""

print(loss.backward())
"""
None
"""

print(w.grad)
"""
输出结果:
tensor([[ 0.0300,  0.0465, -0.0368,  0.0050, -0.0418,  0.0025, -0.0293,  0.0376,
         -0.0009,  0.0066]])
"""

再如案例:

# -*- coding: UTF-8 -*-

import tensorflow as tf

x = tf.random.normal([1, 3])
w = tf.ones([3, 1])
b = tf.ones([3])
y = tf.constant([1])

with tf.GradientTape() as tape:
    tape.watch([w, b])
    prob = tf.sigmoid(x @ w + b)
    loss = tf.reduce_mean(tf.losses.MSE(y,prob))

grads = tape.gradient(loss, [w, b])

print(grads)
"""
输出结果:
[<tf.Tensor: shape=(3, 1), dtype=float32, numpy=
array([[-0.04824488],
       [ 0.02382893],
       [-0.00324614]], dtype=float32)>, <tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.01158172, -0.01158172, -0.01158172], dtype=float32)>]
"""
print("------------------------------")

print(grads[0])
"""
输出结果:
tf.Tensor(
[[-0.04824488]
 [ 0.02382893]
 [-0.00324614]], shape=(3, 1), dtype=float32)
"""

print("------------------------------")

print(grads[1])
"""
输出结果:
tf.Tensor([-0.01158172 -0.01158172 -0.01158172], shape=(3,), dtype=float32)
"""

1.16.2.多输出感知机

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
案例:

# -*- coding: UTF-8 -*-

import torch
from torch.nn import functional as F

x = torch.randn(1, 10)
w = torch.randn(2, 10, requires_grad=True)

# 关于sigmoid的用法:https://pytorch.org/docs/stable/generated/torch.sigmoid.html#torch.sigmoid
o = torch.sigmoid(x @ w.t())
print(o.shape)
"""
输出结果:
torch.Size([1, 2])
"""

loss = F.mse_loss(torch.ones(1, 1), o)
print(loss.backward())
"""
输出结果:
None
"""

print(w.grad)
"""
输出结果:
tensor([[-0.0496,  0.0335,  0.0017, -0.1368,  0.0107,  0.0259,  0.0089,  0.0476,
          0.0977, -0.0397],
        [-0.0902,  0.0610,  0.0032, -0.2490,  0.0195,  0.0471,  0.0162,  0.0867,
          0.1779, -0.0722]])
"""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值