Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on

sub in PyTorch

Buy Me a Coffee

*Memos:

sub() can do subtraction with two of the 0D or more D tensors of zero or more elements or scalars or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • sub() can be used withtorch or a tensor.
  • The 1st argument(input) withtorch(Type:tensor orscalar ofint,float orcomplex) or using a tensor(Type:tensor ofint,float orcomplex)(Required).
  • The 2nd argument withtorch or the 1st argument with a tensor isother(Required-Type:tensor orscalar ofint,float orcomplex).
  • The 3rd argument withtorch or the 2nd argument with a tensor isalpha(Optional-Default:1-Type:tensor orscalar ofint,float orcomplex). *otheris multiplied byalpha(input or a tensor-(otherxalpha)).
  • There isout argument withtorch(Optional-Default:None-Type:tensor):*Memos:
    • out= must be used.
    • My post explainsout argument.
  • subtract() is the alias ofsub().
importtorchtensor1=torch.tensor([9,7,6])tensor2=torch.tensor([[4,-4,3],[-2,5,-5]])torch.sub(input=tensor1,other=tensor2)tensor1.sub(other=tensor2)torch.sub(input=tensor1,other=tensor2,alpha=1)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(1))# tensor([[5, 11, 3], [11, 2, 11]])torch.sub(input=tensor1,other=tensor2,alpha=0)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(0))# tensor([[9, 7, 6], [9, 7, 6]])torch.sub(input=tensor1,other=tensor2,alpha=2)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(2))# tensor([[1, 15, 0], [13, -3, 16]])torch.sub(input=tensor1,other=tensor2,alpha=-1)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(-1))# tensor([[13, 3, 9], [7, 12, 1]])torch.sub(input=tensor1,other=tensor2,alpha=-2)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(-2))# tensor([[17, -1, 12], [5, 17, -4]])torch.sub(input=9,other=tensor2)torch.sub(input=9,other=tensor2,alpha=1)torch.sub(input=9,other=tensor2,alpha=torch.tensor(1))# tensor([[5, 13, 6], [11, 4, 14]])torch.sub(input=tensor1,other=4)torch.sub(input=tensor1,other=4,alpha=1)torch.sub(input=tensor1,other=4,alpha=torch.tensor(1))# tensor([5, 3, 2])torch.sub(input=9,other=4)torch.sub(input=9,other=4,alpha=1)torch.sub(input=9,other=4,alpha=torch.tensor(1))# tensor(5)tensor1=torch.tensor([9.,7.,6.])tensor2=torch.tensor([[4.,-4.,3.],[-2.,5.,-5.]])torch.sub(input=tensor1,other=tensor2)torch.sub(input=tensor1,other=tensor2,alpha=1.)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(1.))# tensor([[5., 11., 3.], [11., 2., 11.]])torch.sub(input=9.,other=tensor2)torch.sub(input=9.,other=tensor2,alpha=1.)torch.sub(input=9.,other=tensor2,alpha=torch.tensor(1.))# tensor([[5., 13., 6.], [11., 4., 14.]])torch.sub(input=tensor1,other=4)torch.sub(input=tensor1,other=4,alpha=1.)torch.sub(input=tensor1,other=4,alpha=torch.tensor(1.))# tensor([5., 3., 2.])torch.sub(input=9.,other=4)torch.sub(input=9.,other=4,alpha=1.)torch.sub(input=9.,other=4,alpha=torch.tensor(1.))# tensor(5.)tensor1=torch.tensor([9.+0.j,7.+0.j,6.+0.j])tensor2=torch.tensor([[4.+0.j,-4.+0.j,3.+0.j],[-2.+0.j,5.+0.j,-5.+0.j]])torch.sub(input=tensor1,other=tensor2)torch.sub(input=tensor1,other=tensor2,alpha=1.+0.j)torch.sub(input=tensor1,other=tensor2,alpha=torch.tensor(1.+0.j))# tensor([[5.+0.j, 11.+0.j, 3.+0.j],#         [11.+0.j, 2.+0.j, 11.+0.j]])torch.sub(input=9.+0.j,other=tensor2)torch.sub(input=9.+0.j,other=tensor2,alpha=1.+0.j)torch.sub(input=9.+0.j,other=tensor2,alpha=torch.tensor(1.+0.j))# tensor([[5.+0.j, 13.+0.j, 6.+0.j],#         [11.+0.j, 4.+0.j, 14.+0.j]])torch.sub(input=tensor1,other=4.+0.j)torch.sub(input=tensor1,other=4.+0.j,alpha=1.+0.j)torch.sub(input=tensor1,other=4.+0.j,alpha=torch.tensor(1.+0.j))# tensor([5.+0.j, 3.+0.j, 2.+0.j])torch.sub(input=9.+0.j,other=4.+0.j)torch.sub(input=9.+0.j,other=4.+0.j,alpha=1.+0.j)torch.sub(input=9.+0.j,other=4.+0.j,alpha=torch.tensor(1.+0.j))# tensor(5.+0.j)
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