本部分来自课程+自己整理
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]])
"""