Super Kai (Kazuya Ito)
Posted on • Edited on
trace, reciprocal and rsqrt in PyTorch
*Memos:
- My post explainssquare().
- My post explainspow().
- My post explainsfloat_power().
- My post explainsabs() andsqrt().
- My post explainsgcd() andlcm().
trace() can get the 0D tensor of the sum of the zero or more elements of diagonal from the 2D tensor of zero or more elements as shown below:
*Memos:
trace()
can be used withtorch or a tensor.- The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
orcomplex
).
importtorchmy_tensor=torch.tensor([[0,1,2],[3,4,5],[6,7,8]])torch.trace(input=my_tensor)my_tensor.trace()# tensor(12)my_tensor=torch.tensor([[0.,1.,2.],[3.,4.,5.],[6.,7.,8.]])torch.trace(input=my_tensor)# tensor(12.)my_tensor=torch.tensor([[0.+0.j,1.+0.j,2.+0.j],[3.+0.j,4.+0.j,5.+0.j],[6.+0.j,7.+0.j,8.+0.j]])torch.trace(input=my_tensor)# tensor(12.+0.j)my_tensor=torch.tensor([[0,1,2,3],[4,5,6,7],[8,9,10,11]])torch.trace(input=my_tensor)# tensor(15)my_tensor=torch.tensor([[0,1,2],[3,4,5]])torch.trace(input=my_tensor)# tensor(4)my_tensor=torch.tensor([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])torch.trace(input=my_tensor)# tensor(12)my_tensor=torch.tensor([[]])torch.trace(input=my_tensor)# tensor(0.)
reciprocal() can get the 0D or more D tensor of zero or more reciprocals from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
reciprocal()
can be used withtorch
or a tensor.- The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
):*Memos:out=
must be used.- My post explains
out
argument.
reciprocal()
returns afloat
type tensor except wheninput
or a tensor is acomplex
type tensor.
importtorchmy_tensor=torch.tensor(-4.)torch.reciprocal(input=my_tensor)my_tensor.reciprocal()# tensor(-0.2500)my_tensor=torch.tensor([-4.,-3.,-2.,-1.,0.,1.,2.,3.])torch.reciprocal(input=my_tensor)# tensor([-0.2500, -0.3333, -0.5000, -1.0000,inf,1.0000,0.5000,0.3333])my_tensor=torch.tensor([[-4.,-3.,-2.,-1.],[0.,1.,2.,3.]])torch.reciprocal(input=my_tensor)# tensor([[-0.2500, -0.3333, -0.5000, -1.0000],# [inf, 1.0000, 0.5000, 0.3333]])my_tensor=torch.tensor([[[-4.,-3.],[-2.,-1.]],[[0.,1.],[2.,3.]]])torch.reciprocal(input=my_tensor)# tensor([[[-0.2500, -0.3333], [-0.5000, -1.0000]],# [[inf, 1.0000], [0.5000, 0.3333]]])my_tensor=torch.tensor([[[-4,-3],[-2,-1]],[[0,1],[2,3]]])torch.reciprocal(input=my_tensor)# tensor([[[-0.2500, -0.3333], [-0.5000, -1.0000]],# [[inf, 1.0000], [0.5000, 0.3333]]])my_tensor=torch.tensor([[[-4.+0.j,-3.+0.j],[-2.+0.j,-1.+0.j]],[[0.+0.j,1.+0.j],[2.+0.j,3.+0.j]]])torch.reciprocal(input=my_tensor)# tensor([[[-0.2500-0.j, -0.3333-0.j], [-0.5000-0.j, -1.0000-0.j]],# [[nan+nanj, 1.0000-0.j], [ 0.5000-0.j, 0.3333-0.j]]])my_tensor=torch.tensor([[[True,False],[True,False]],[[False,True],[False,True]]])torch.reciprocal(input=my_tensor)# tensor([[[1., inf], [1., inf]],# [[inf, 1.], [inf, 1.]]])
rsqrt() can get the 0D or more D tensor of the zero or more reciprocals of square root from the 0D or more D tensor of zero or more elements as shown below:
*Memos:
rsqrt()
can be used withtorch
or a tensor.- The 1st argument(
input
) withtorch
or using a tensor(Required-Type:tensor
ofint
,float
,complex
orbool
). - There is
out
argument withtorch
(Optional-Default:None
-Type:tensor
):*Memos:out=
must be used.- My post explains
out
argument.
rsqrt()
returns afloat
type tensor except wheninput
or a tensor is acomplex
type tensor.
importtorchmy_tensor=torch.tensor(-3.)torch.rsqrt(input=my_tensor)my_tensor.rsqrt()# tensor(nan)my_tensor=torch.tensor([-3.,-2.,-1.,0.,1.,2.,3.,4.])torch.rsqrt(input=my_tensor)# tensor([nan, nan, nan, inf, 1.0000, 0.7071, 0.5774, 0.5000])my_tensor=torch.tensor([[-3.,-2.,-1.,0.],[1.,2.,3.,4.]])torch.rsqrt(input=my_tensor)# tensor([[nan, nan, nan, inf],# [1.0000, 0.7071, 0.5774, 0.5000]])my_tensor=torch.tensor([[[-3.,-2.],[-1.,0.]],[[1.,2.],[3.,4.]]])torch.rsqrt(input=my_tensor)# tensor([[[nan, nan],# [nan, inf]],# [[1.0000, 0.7071],# [0.5774, 0.5000]]])my_tensor=torch.tensor([[[-3,-2],[-1,0]],[[1,2],[3,4]]])torch.rsqrt(input=my_tensor)# tensor([[[nan, nan],# [nan, inf]],# [[1.0000, 0.7071],# [0.5774, 0.5000]]])my_tensor=torch.tensor([[[-3.+0.j,-2.+0.j],[-1.+0.j,0.+0.j]],[[1.+0.j,2.+0.j],[3.+0.j,4.+0.j]]])torch.rsqrt(input=my_tensor)# tensor([[[0.0000-0.5774j, 0.0000-0.7071j],# [0.0000-1.0000j, nan+nanj]],# [[1.0000-0.0000j, 0.7071-0.0000j],# [0.5774-0.0000j, 0.5000-0.0000j]]])my_tensor=torch.tensor([[[True,False],[True,False]],[[False,True],[False,True]]])torch.rsqrt(input=my_tensor)# tensor([[[1., inf],# [1., inf]],# [[inf, 1.],# [inf, 1.]]])
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse