App下載

PyTorch平方根報(bào)錯(cuò)的處理方案

草莓配可樂 2021-08-17 10:21:41 瀏覽數(shù) (2238)
反饋

我們?cè)谑褂胮ytorch求平方根的時(shí)候,有時(shí)候會(huì)出現(xiàn)報(bào)錯(cuò),那么小編是如何解決pytorch平方根報(bào)錯(cuò)的呢?來看看小編的處理方案。

問題描述

初步使用PyTorch進(jìn)行平方根計(jì)算,通過range()創(chuàng)建一個(gè)張量,然后對(duì)其求平方根。

a = torch.tensor(list(range(9)))
b = torch.sqrt(a)

報(bào)出以下錯(cuò)誤:

RuntimeError: sqrt_vml_cpu not implemented for 'Long'

原因

Long類型的數(shù)據(jù)不支持log對(duì)數(shù)運(yùn)算, 為什么Tensor是Long類型? 因?yàn)閯?chuàng)建List數(shù)組時(shí)默認(rèn)使用的是int, 所以從List轉(zhuǎn)成torch.Tensor后, 數(shù)據(jù)類型變成了Long。

print(a.dtype)

torch.int64

解決方法

提前將數(shù)據(jù)類型指定為浮點(diǎn)型, 重新執(zhí)行:

b = torch.sqrt(a.to(torch.double))
print(b)

tensor([0.0000, 1.0000, 1.4142, 1.7321, 2.0000, 2.2361, 2.4495, 2.6458, 2.8284], dtype=torch.float64)

補(bǔ)充:pytorch10 pytorch常見運(yùn)算詳解

矩陣與標(biāo)量

這個(gè)是矩陣(張量)每一個(gè)元素與標(biāo)量進(jìn)行操作。

import torch
a = torch.tensor([1,2])
print(a+1)
>>> tensor([2, 3])

哈達(dá)瑪積

這個(gè)就是兩個(gè)相同尺寸的張量相乘,然后對(duì)應(yīng)元素的相乘就是這個(gè)哈達(dá)瑪積,也成為element wise。

a = torch.tensor([1,2])
b = torch.tensor([2,3])
print(a*b)
print(torch.mul(a,b))
>>> tensor([2, 6])
>>> tensor([2, 6])

這個(gè)torch.mul()和*是等價(jià)的。

當(dāng)然,除法也是類似的:

a = torch.tensor([1.,2.])
b = torch.tensor([2.,3.])
print(a/b)
print(torch.div(a/b))
>>> tensor([0.5000, 0.6667])
>>> tensor([0.5000, 0.6667])

我們可以發(fā)現(xiàn)的torch.div()其實(shí)就是/, 類似的:torch.add就是+,torch.sub()就是-,不過符號(hào)的運(yùn)算更簡(jiǎn)單常用。

矩陣乘法

如果我們想實(shí)現(xiàn)線性代數(shù)中的矩陣相乘怎么辦呢?

這樣的操作有三個(gè)寫法:

torch.mm()

torch.matmul()

@,這個(gè)需要記憶,不然遇到這個(gè)可能會(huì)挺蒙蔽的

a = torch.tensor([[1.],[2.]])
b = torch.tensor([2.,3.]).view(1,2)
print(torch.mm(a, b))
print(torch.matmul(a, b))
print(a @ b)

矩陣乘法

這是對(duì)二維矩陣而言的,假如參與運(yùn)算的是一個(gè)多維張量,那么只有torch.matmul()可以使用。等等,多維張量怎么進(jìn)行矩陣的乘法?在多維張量中,參與矩陣運(yùn)算的其實(shí)只有后兩個(gè)維度,前面的維度其實(shí)就像是索引一樣,舉個(gè)例子:

a = torch.rand((1,2,64,32))
b = torch.rand((1,2,32,64))
print(torch.matmul(a, b).shape)
>>> torch.Size([1, 2, 64, 64])

矩陣乘法

a = torch.rand((3,2,64,32))
b = torch.rand((1,2,32,64))
print(torch.matmul(a, b).shape)
>>> torch.Size([3, 2, 64, 64])

這樣也是可以相乘的,因?yàn)檫@里涉及一個(gè)自動(dòng)傳播Broadcasting機(jī)制,這個(gè)在后面會(huì)講,這里就知道,如果這種情況下,會(huì)把b的第一維度復(fù)制3次 ,然后變成和a一樣的尺寸,進(jìn)行矩陣相乘。

冪與開方

print('冪運(yùn)算')
a = torch.tensor([1.,2.])
b = torch.tensor([2.,3.])
c1 = a ** b
c2 = torch.pow(a, b)
print(c1,c2)
>>> tensor([1., 8.]) tensor([1., 8.])

和上面一樣,不多說了。開方運(yùn)算可以用torch.sqrt(),當(dāng)然也可以用a**(0.5)。

對(duì)數(shù)運(yùn)算

在上學(xué)的時(shí)候,我們知道ln是以e為底的,但是在pytorch中,并不是這樣。

pytorch中l(wèi)og是以e自然數(shù)為底數(shù)的,然后log2和log10才是以2和10為底數(shù)的運(yùn)算。

import numpy as np
print('對(duì)數(shù)運(yùn)算')
a = torch.tensor([2,10,np.e])
print(torch.log(a))
print(torch.log2(a))
print(torch.log10(a))
>>> tensor([0.6931, 2.3026, 1.0000])
>>> tensor([1.0000, 3.3219, 1.4427])
>>> tensor([0.3010, 1.0000, 0.4343]) 

近似值運(yùn)算

.ceil() 向上取整

.floor()向下取整

.trunc()取整數(shù)

.frac()取小數(shù)

.round()四舍五入

.ceil() 向上取整.floor()向下取整.trunc()取整數(shù).frac()取小數(shù).round()四舍五入

a = torch.tensor(1.2345)
print(a.ceil())
>>>tensor(2.)
print(a.floor())
>>> tensor(1.)
print(a.trunc())
>>> tensor(1.)
print(a.frac())
>>> tensor(0.2345)
print(a.round())
>>> tensor(1.)

剪裁運(yùn)算

這個(gè)是讓一個(gè)數(shù),限制在你自己設(shè)置的一個(gè)范圍內(nèi)[min,max],小于min的話就被設(shè)置為min,大于max的話就被設(shè)置為max。這個(gè)操作在一些對(duì)抗生成網(wǎng)絡(luò)中,好像是WGAN-GP,通過強(qiáng)行限制模型的參數(shù)的值。

a = torch.rand(5)
print(a)
print(a.clamp(0.3,0.7))

運(yùn)行結(jié)果

以上就是PyTorch平方根報(bào)錯(cuò)的處理方案,希望能給大家一個(gè)參考,也希望大家多多支持W3Cschool。



0 人點(diǎn)贊