多输出感知机及其梯度

Multi-output Perceptron

23-多输出感知机及其梯度-2多分类模.jpg

\[ E=\frac{1}{2}\sum(O_i^1-t_i)^2 \]

对于多输出感知机,每个输出元只和输出元上的x和w和\(\sigma\)有关。

import tensorflow as tf
x = tf.random.normal([2, 4])
w = tf.random.normal([4, 3])
b = tf.zeros([3])
y = tf.constant([2, 0])

with tf.GradientTape() as tape:
    tape.watch([w, b])
    # axis=1,表示结果[b,3]中的3这个维度为概率
    prob = tf.nn.softmax(x @ w + b, axis=1)
    # 2 --> 001; 0 --> 100
    loss = tf.reduce_mean(tf.losses.MSE(tf.one_hot(y, depth=3), prob))

grads = tape.gradient(loss, [w, b])
grads[0]
<tf.Tensor: id=92, shape=(4, 3), dtype=float32, numpy=
array([[ 0.00842961, -0.02221732,  0.01378771],
       [ 0.02969089, -0.04625662,  0.01656573],
       [ 0.05807886, -0.08139262,  0.02331377],
       [-0.06571108,  0.11157083, -0.04585974]], dtype=float32)>
grads[1]
<tf.Tensor: id=90, shape=(3,), dtype=float32, numpy=array([-0.05913186,  0.09886257, -0.03973071], dtype=float32)>

转载于:https://www.cnblogs.com/nickchen121/p/10914273.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值