Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

positive, neg and copysign in PyTorch

Buy Me a Coffee

positive() can just get the same tensor as the input tensor which is the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • positive() 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(7)torch.positive(input=my_tensor)my_tensor.positive()# tensor(7)my_tensor=torch.tensor([7,-1,5,-7,-9,-3,0,6])torch.positive(input=my_tensor)# tensor([7, -1, 5, -7, -9, -3, 0, 6])my_tensor=torch.tensor([[7,-1,5,-7],[-9,-3,0,6]])torch.positive(input=my_tensor)# tensor([[7, -1, 5, -7],#         [-9, -3, 0, 6]])my_tensor=torch.tensor([[[7,-1],[5,-7]],[[-9,-3],[0,6]]])torch.positive(input=my_tensor)# tensor([[[7, -1], [5, -7]],#         [[-9, -3], [0, 6]]])my_tensor=torch.tensor([[[7.,-1.],[5.,-7.]],[[-9.,-3.],[0.,6.]]])torch.positive(input=my_tensor)# tensor([[[7., -1.], [5., -7.]],#         [[-9., -3.], [0., 6.]]])my_tensor=torch.tensor([[[7.+0.j,-1.+0.j],[5.+0.j,-7.+0.j]],[[-9.+0.j,-3.+0.j],[0.+0.j,6.+0.j]]])torch.positive(input=my_tensor)# tensor([[[7.+0.j, -1.+0.j],#          [5.+0.j, -7.+0.j]],#         [[-9.+0.j, -3.+0.j],#          [0.+0.j, 6.+0.j]]])
Enter fullscreen modeExit fullscreen mode

neg() can get the 0D or more D tensor of the zero or more elements changed from+ to- and from- to+ from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • neg() can be used withtorch or a tensor.
  • The 1st argument(input) withtorch or using a tensor(Required-Type:tensor ofint,float orcomplex).
  • There isout argument withtorch(Optional-Default:None-Type:tensor):*Memos:
    • out= must be used.
    • My post explainsout argument.
  • negative() is the alias ofneg().
importtorchmy_tensor=torch.tensor(7)torch.neg(input=my_tensor)my_tensor.neg()# tensor(-7)my_tensor=torch.tensor([7,-1,5,-7,-9,-3,0,6])torch.neg(input=my_tensor)# tensor([-7, 1, -5, 7, 9, 3, 0, -6])my_tensor=torch.tensor([[7,-1,5,-7],[-9,-3,0,6]])torch.neg(input=my_tensor)# tensor([[-7, 1, -5, 7],#         [9, 3, 0, -6]])my_tensor=torch.tensor([[[7,-1],[5,-7]],[[-9,-3],[0,6]]])torch.neg(input=my_tensor)# tensor([[[-7, 1], [-5, 7]],#         [[9, 3], [0, -6]]])my_tensor=torch.tensor([[[7.,-1.],[5.,-7.]],[[-9.,-3.],[0.,6.]]])torch.neg(input=my_tensor)# tensor([[[-7., 1.], [-5., 7.]],#         [[9., 3.], [-0., -6.]]])my_tensor=torch.tensor([[[7.+0.j,-1.+0.j],[5.+0.j,-7.+0.j]],[[-9.+0.j,-3.+0.j],[0.+0.j,6.+0.j]]])torch.neg(input=my_tensor)# tensor([[[-7.+0.j, 1.+0.j], [-5.+0.j, 7.+0.j]],#         [[9.+0.j, 3.+0.j], [0.+0.j, -6.+0.j]]])
Enter fullscreen modeExit fullscreen mode

copysign() can get the 0D or more D tensor of the zero or more floating point numbers changed+ and- byother tensor from two of 0D or more D tensors as shown below:

*Memos:

  • copysign() can be used withtorch or a tensor.
  • The 1st argument(input) withtorch or using a tensor(Required-Type:tensor ofint,float,bool).
  • The 2st argument withtorch or the 1st argument isother(Required-Type:tensor orscalar ofint,float orbool). *The sign(+ or-) is applied to the returned tensor.
  • There isout argument withtorch(Optional-Default:None-Type:tensor):*Memos:
    • out= must be used.
    • My post explainsout argument.
importtorchtensor1=torch.tensor([[7.,-1.,5.,-7.],[-9.,-3.,0.,6.]])tensor2=torch.tensor([-1.,0.,1.,2.])torch.copysign(input=tensor1,other=tensor2)tensor1.copysign(other=tensor2)# tensor([[-7., 1., 5., 7.],#         [-9., 3., 0., 6.]])torch.copysign(input=tensor1,other=2.)# tensor([[7., 1., 5., 7.],#         [9., 3., 0., 6.]])tensor1=torch.tensor([[[7.,-1.],[5.,-7.]],[[-9.,-3.],[0.,6.]]])tensor2=torch.tensor(-1.)torch.copysign(input=tensor1,other=tensor2)# tensor([[[-7., -1.], [-5., -7.]],#         [[-9., -3.], [-0., -6.]]])torch.copysign(input=tensor1,other=2.)# tensor([[[7., 1.], [5., 7.]],#         [[9., 3.], [0., 6.]]])tensor1=torch.tensor([[[7,-1],[5,-7]],[[-9,-3],[0,6]]])tensor2=torch.tensor(-1)torch.copysign(input=tensor1,other=tensor2)torch.copysign(input=tensor1,other=-1)# tensor([[[-7., -1.], [-5., -7.]],#         [[-9., -3.], [-0., -6.]]])torch.copysign(input=tensor1,other=2)# tensor([[[7., 1.], [5., 7.]],#         [[9., 3.], [0., 6.]]])tensor1=torch.tensor([[[True,False],[True,False]],[[False,True],[False,True]]])tensor2=torch.tensor(True)torch.copysign(input=tensor1,other=tensor2)torch.copysign(input=tensor1,other=False)# tensor([[[1., 0.], [1., 0.]],#         [[0., 1.], [0., 1.]]])
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a web developer.Buy Me a Coffee: ko-fi.com/superkaiSO: stackoverflow.com/users/3247006/super-kai-kazuya-itoX(Twitter): twitter.com/superkai_kazuyaFB: facebook.com/superkai.kazuya
  • Joined

More fromSuper Kai (Kazuya Ito)

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp