计算机的错误计算(四十九)

摘要  计算机的错误计算(四十八)讨论了GPU下一个深度学习中的“Hello World”程序的计算精度问题。本节探讨扩展后的程序在GPU下的计算精度问题。具体扩展为:首先归一化“Hello World”程序的结果矩阵Y,然后对其应用Tanh激活函数,最后与新生成的矩阵Z进行第二次矩阵乘法。结果表明,该扩展后的程序的误差也大为“扩展”。

       先看代码:

import torch

# 设置随机种子
torch.manual_seed(0)

# 创建张量并移动到GPU
W = torch.randn(5, 3) * 10
W = W.to('cuda')
X = torch.randn(3, 5) * 10
X = X.to('cuda')

# 计算矩阵乘法
Y = torch.mm(W, X)

# 在GPU上执行归一化操作
min_val = Y.min()
max_val = Y.max()
Y_normalized = (Y - min_val) / (max_val - min_val)

# 应用tanh函数
torch_tanh = torch.tanh(Y_normalized)

# 创建另一个张量并移动到GPU
Z = torch.randn(5, 4) * 10
Z = Z.to('cuda')

# 计算矩阵乘法
R_tanh = torch.mm(torch_tanh, Z)

# 设置小数位数
torch.set_printoptions(precision=8)

# 输出
print(R_tanh)

然后是输出:

       在上面的输出中,有 2个值包含 3位错误数字,8个值包含 2位错误数字。这10个值的正确结果是:

\textup{R\_tanh}[0,0]=1.35825\textcolor{red}{69}\,,\\ \textup{R\_tanh}[0,1]=2.82822\textcolor{red}{84}\,,\\ \textup{R\_tanh}[0,3]=4.30409\textcolor{red}{80}\,,\\ \textup{R\_tanh}[1,0]=-0.76126\textcolor{red}{413}\,,\\ \textup{R\_tanh}[1,2]=-2.12987\textcolor{red}{53}\,,\\ \textup{R\_tanh}[2,0]=0.88321\textcolor{red}{183}\,,\\ \textup{R\_tanh}[2,2]=-5.80109\textcolor{red}{33}\,,\\ \textup{R\_tanh}[3,0]=-1.19670\textcolor{red}{52}\,,\\ \textup{R\_tanh}[3,3]=1.69697\textcolor{red}{61}\,,\\ \textup{R\_tanh}[4,0]=-2.60537\textcolor{red}{32}\,.

其中红颜色数字表明 Python对应位置的数字不正确。因此,错误率不小于 2/8=25%的占比达到 10/20=50%.  它是计算机的错误计算(四十三)中CPU下错误率占比的 2倍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值