DEV Community

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

Posted on • Edited on

diff in PyTorch

Buy Me a Coffee

diff() can get the 1D or more D tensor of the zero or more elements of difference from the 1D or more D tensor of zero or more elements as shown below:

*Memos:

  • diff() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 3rd argument is n(Optional-Default:1-Type:int). *This can get the difference of the difference of diff...
  • The 3rd argument with torch or the 4th argument is dim(Optional-Default:-1-Type:int).
  • The 4th argument with torch or the 5th argument is prepend(Optional-Default:None-Type:tensor of int, float, complex or bool). *A tensor is added before input tensor.
  • The 5th argument with torch or the 6th argument is append(Optional-Default:None-Type:tensor of int, float, complex or bool). *A tensor is added after input tensor.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.

A 1D tensor:

import torch

my_tensor = torch.tensor([4, 9, 1, 3, 0, 7])

torch.diff(input=my_tensor)
my_tensor.diff()
torch.diff(input=my_tensor, n=1, dim=0)
torch.diff(input=my_tensor, n=1, dim=-1)
# tensor([5, -8, 2, -3, 7])

torch.diff(input=my_tensor, n=2, dim=0)
torch.diff(input=my_tensor, n=2, dim=-1)
# tensor([-13, 10, -5, 10])

torch.diff(input=my_tensor, n=3, dim=0)
torch.diff(input=my_tensor, n=3, dim=-1)
# tensor([23, -15, 15])

torch.diff(input=my_tensor, n=4, dim=0)
torch.diff(input=my_tensor, n=4, dim=-1)
# tensor([-38, 30])

torch.diff(input=my_tensor, n=5, dim=0)
torch.diff(input=my_tensor, n=5, dim=-1)
# tensor([68])

torch.diff(input=my_tensor, n=0, dim=0)
torch.diff(input=my_tensor, n=0, dim=1)
torch.diff(input=my_tensor, n=0, dim=2)
etc..
# tensor([4, 9, 1, 3, 0, 7])

torch.diff(input=my_tensor, n=6, dim=0)
torch.diff(input=my_tensor, n=6, dim=-1)
# tensor([], dtype=torch.int64)

tensor1 = torch.tensor([4, 9, 1, 3, 0, 7])
tensor2 = torch.tensor([6])
tensor3 = torch.tensor([1, 4])
# [6, 4, 9, 1, 3, 0, 7, 1, 4]

torch.diff(input=tensor1, n=1, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=1, dim=-1, prepend=tensor2, append=tensor3)
# tensor([-2, 5, -8, 2, -3, 7, -6, 3])

torch.diff(input=tensor1, n=2, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=2, dim=-1, prepend=tensor2, append=tensor3)
# tensor([7, -13, 10, -5, 10, -13, 9])

torch.diff(input=tensor1, n=3, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=3, dim=-1, prepend=tensor2, append=tensor3)
# tensor([-20, 23, -15, 15, -23, 22])

torch.diff(input=tensor1, n=4, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=4, dim=-1, prepend=tensor2, append=tensor3)
# tensor([43, -38, 30, -38, 45])

torch.diff(input=tensor1, n=5, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=5, dim=-1, prepend=tensor2, append=tensor3)
# tensor([-81, 68, -68, 83])

torch.diff(input=tensor1, n=6, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=6, dim=-1, prepend=tensor2, append=tensor3)
# tensor([149, -136, 151])

torch.diff(input=tensor1, n=7, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=7, dim=-1, prepend=tensor2, append=tensor3)
# tensor([-285, 287])

torch.diff(input=tensor1, n=8, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=8, dim=-1, prepend=tensor2, append=tensor3)
# tensor([572])

torch.diff(input=tensor1, n=0, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=0, dim=-1, prepend=tensor2, append=tensor3)
# tensor([4, 9, 1, 3, 0, 7])

torch.diff(input=tensor1, n=8, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=8, dim=-1, prepend=tensor2, append=tensor3)
# tensor([572])

tensor1 = torch.tensor([4., 9., True, 3., False, 7+0j])
tensor2 = torch.tensor([6+0j])
tensor3 = torch.tensor([True, 4+0j])
# [6, 4, 9, 1, 3, 0, 7, 1, 4]

torch.diff(input=tensor1, n=1, dim=0, prepend=tensor2, append=tensor3)
# tensor([-2.+0.j, 5.+0.j, -8.+0.j, 2.+0.j,
#         -3.+0.j, 7.+0.j, -6.+0.j, 3.+0.j])
Enter fullscreen mode Exit fullscreen mode

A 2D tensor:

import torch

my_tensor = torch.tensor([[4, 9, 1], [3, 0, 7]])

torch.diff(input=my_tensor)
torch.diff(input=my_tensor, n=1, dim=1)
torch.diff(input=my_tensor, n=1, dim=-1)
# tensor([[5, -8], [-3, 7]])

torch.diff(input=my_tensor, n=2, dim=1)
torch.diff(input=my_tensor, n=2, dim=-1)
# tensor([[-13], [10]])

torch.diff(input=my_tensor, n=1, dim=0)
torch.diff(input=my_tensor, n=1, dim=-2)
# tensor([[-1, -9, 6]])

torch.diff(input=my_tensor, n=0, dim=0)
torch.diff(input=my_tensor, n=0, dim=1)
torch.diff(my_tensor, n=0, dim=2)
etc...
# tensor([[4, 9, 1], [3, 0, 7]])

torch.diff(input=my_tensor, n=2, dim=0)
torch.diff(input=my_tensor, n=2, dim=-2)
torch.diff(input=my_tensor, n=3, dim=0)
torch.diff(input=my_tensor, n=3, dim=-2)
# tensor([], size=(0, 3), dtype=torch.int64)

torch.diff(input=my_tensor, n=3, dim=1)
torch.diff(input=my_tensor, n=3, dim=-1)
# tensor([], size=(2, 0), dtype=torch.int64)

tensor1 = torch.tensor([[4, 9, 1], [3, 0, 7]])
tensor2 = torch.tensor([[6, 0, 5], [8, 1, 4]])
tensor3 = torch.tensor([[1, 4, 8], [0, 2, 9]])
# [[6, 0, 5, 4, 9, 1, 1, 4, 8],
#  [8, 1, 4, 3, 0, 7, 0, 2, 9]]
# Or
# [[6, 0, 5],
#  [8, 1, 4],
#  [4, 9, 1],
#  [3, 0, 7],
#  [1, 4, 8],
#  [0, 2, 9]]

torch.diff(input=tensor1, n=5, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=5, dim=-2, prepend=tensor2, append=tensor3)
# tensor([[19, -103, 44]])

torch.diff(input=tensor1, n=1, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=1, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[-6, 5, -1, 5, -8, 0, 3, 4],
#         [-7, 3, -1, -3, 7, -7, 2, 7]])

torch.diff(input=tensor1, n=2, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=2, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[11, -6, 6, -13, 8, 3, 1],
#         [10, -4, -2, 10, -14, 9, 5]])

torch.diff(input=tensor1, n=3, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=3, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[-17, 12, -19, 21, -5, -2],
#         [-14, 2, 12, -24, 23, -4]])

torch.diff(input=tensor1, n=4, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=4, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[29, -31, 40, -26, 3],
#         [16, 10, -36, 47, -27]])

torch.diff(input=tensor1, n=5, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=5, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[-60, 71, -66, 29],
#         [ -6, -46, 83, -74]])

torch.diff(input=tensor1, n=6, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=6, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[131, -137, 95],
#         [-40, 129, -157]])

torch.diff(input=tensor1, n=7, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=7, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[-268, 232],
#         [169, -286]])

torch.diff(input=tensor1, n=8, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=8, dim=-1, prepend=tensor2, append=tensor3)
# tensor([[500],
#         [-455]])

torch.diff(input=tensor1, n=1, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=1, dim=-2, prepend=tensor2, append=tensor3)
# tensor([[2, 1, -1],
#         [-4, 8, -3],
#         [-1, -9, 6],
#         [-2, 4, 1],
#         [-1, -2, 1]])

torch.diff(input=tensor1, n=2, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=2, dim=-2, prepend=tensor2, append=tensor3)
# tensor([[-6, 7, -2],
#         [3, -17, 9],
#         [-1, 13, -5],
#         [1, -6, 0]])

torch.diff(input=tensor1, n=3, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=3, dim=-2, prepend=tensor2, append=tensor3)
# tensor([[9, -24, 11],
#         [-4, 30, -14],
#         [2, -19, 5]])

torch.diff(input=tensor1, n=4, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=4, dim=-2, prepend=tensor2, append=tensor3)
# tensor([[-13, 54, -25],
#         [6, -49, 19]])

torch.diff(input=tensor1, n=0, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=0, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=0, dim=-1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=0, dim=-2, prepend=tensor2, append=tensor3)
# tensor([[4, 9, 1], [3, 0, 7]])

torch.diff(input=tensor1, n=6, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=6, dim=-2, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=7, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=7, dim=-2, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=8, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=8, dim=-2, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=9, dim=0, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=9, dim=-2, prepend=tensor2, append=tensor3)
# tensor([], size=(0, 3), dtype=torch.int64)

torch.diff(input=tensor1, n=9, dim=1, prepend=tensor2, append=tensor3)
torch.diff(input=tensor1, n=9, dim=-1, prepend=tensor2, append=tensor3)
# tensor([], size=(2, 0), dtype=torch.int64)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay