gpt4 book ai didi

python - Mxnet 元素明智的相乘

转载 作者:太空宇宙 更新时间:2023-11-04 00:31:48 27 4
gpt4 key购买 nike

在 MXNet 中,如果我想创建一个权重向量来乘以每个输入,即具有 w*x_i 然后反向传播权重 w 我将如何做这个?

我试过:

 y_hat = input
w1 = mx.sym.Variable("w1")
y_hat = mx.symbol.broadcast_mul(w1, y_hat)

最佳答案

您可以根据点积进行计算:

x = mx.nd.array([[1, 2, 3], [4, 5, 6]])
w = mx.nd.array([2,2,2])
mx.nd.dot(w, x.T)

如您所愿,结果将是 [12. 30.]。

现在只需随机初始化w,计算输出和目标输出之间的损失,然后反向传播。您可以为此使用新的 gluon 接口(interface) ( http://gluon.mxnet.io/ )。

具体来说,让我们看一个改编自 http://mxnet.io/tutorials/gluon/gluon.html 的最小示例和 http://gluon.mxnet.io/P01-C05-autograd.html

准备数据

label = mx.nd.array([12,30])
x = mx.nd.array([[1, 2, 3], [4, 5, 6]])
w = random weights
w.attach_grad()

然后训练

with autograd.record():
output = mx.nd.dot(w, x.T)
loss = gluon.loss.L2Loss(output, label)
loss.backward()

别忘了 updating the weights使用您在向后传递中计算的梯度。渐变将在 w.grad 中可用。在一个循环中将训练代码与权重更新一起运行,因为单个更新可能不足以收敛。

关于python - Mxnet 元素明智的相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45540893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com