
x = torch.arange(10)
print('x[0]=', x[0])
print('x[-1]=', x[-1])
print('x[2:5]=', x[2:5])
print('x[:5]=', x[:5])
print('x[5:]=', x[5:])
print('x[::2]=', x[::2])
print('x[[1,2,5]]=', x[[1,2,5]])
x[0]= tensor(0)
x[-1]= tensor(9)
x[2:5]= tensor([2, 3, 4])
x[:5]= tensor([0, 1, 2, 3, 4])
x[5:]= tensor([5, 6, 7, 8, 9])
x[::2]= tensor([0, 2, 4, 6, 8])
x[[1,2,5]]= tensor([1, 2, 5])
x[0] = 10
print('x=', x)
x[2:5] = torch.tensor([20, 30, 40])
print('x=', x)
x= tensor([10, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x= tensor([10, 1, 20, 30, 40, 5, 6, 7, 8, 9])
x = torch.arange(9).reshape((3, 3))
print('x=', x)
x= tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
print('x[0,0]=', x[0,0])
print('x[0,:]=', x[0,:])
print('x[:,0]=', x[:,0])
print('x[:,[0,2]]=', x[:,[0,2]])
x[0,0]= tensor(0)
x[0,:]= tensor([0, 1, 2])
x[:,0]= tensor([0, 3, 6])
x[:,[0,2]]= tensor([[0, 2],
[3, 5],
[6, 8]])
print('x[0:2, 0:2]=', x[0:2, 0:2])
print('x[..., 0]=', x[..., 0])
x[0:2, 0:2]= tensor([[0, 1],
[3, 4]])
x[..., 0]= tensor([0, 3, 6])
y = torch.arange(9).reshape((1, 3, 3))
print('y=', y)
print('y[0, 0, 0]=', y[0, 0, 0])
print('y[:, :, 0]
x = torch.arange(12).reshape(3, 4)
print('x=', x)
indices = torch.randperm(x.shape[0])
print('indices=', indices)
y = x[indices]
print('y=', y)
x= tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
indices= tensor([1, 0, 2])
y= tensor([[ 4, 5, 6, 7],
[ 0, 1, 2, 3],
[ 8, 9, 10, 11]])
x = torch.arange(1, 10)
print('x=', x)
indx = range(x.shape[0])[::-1]
indx = torch.IntTensor(indx)
print('indx=', indx)
x= tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
indx= tensor([8, 7, 6, 5, 4, 3, 2, 1, 0], dtype=torch.int32)
y = torch.index_select(x, 0, indx)
print('y=', y)
y= tensor([9, 8, 7, 6, 5, 4, 3, 2, 1])
x = x.view(-1, 3)
print('x=', x)
indx = torch.IntTensor([0, 1])
y = x.index_select(0, indx)
print('y=', y)
z = x.index_select(1, indx)
print('z=', z)
x= tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
y= tensor([[1, 2, 3],
[4, 5, 6]])
z= tensor([[1, 2],
[4, 5],
[7, 8]])
torch.set_printoptions(precision=2)
x = torch.tensor([1,2])
print('x+1=', x+1)
print('x-1=', x-1)
print('x*2=', x*2)
print('x/2=', x/2)
print('x**2=', x**2)
x+1= tensor([2, 3])
x-1= tensor([0, 1])
x*2= tensor([2, 4])
x/2= tensor([0.50, 1.00])
x**2= tensor([1, 4])
set_printoptions는 소수점 몇째자리까지 출력하는지를 보여준다.
1. x는 1D텐서이고 1은 상수이다.
2. 상수와의 연산은 텐서의 각 element에 대해 해당 상수값을 각각 산술연산하여 텐서에 반환한다.
y = torch.tensor([3, 4])
print('x+y=', x+y)
print('x-y=', x-y)
print('x*y=', x*y)
print('x/y=', x/y)
print('x**y=', x**y)
x+y= tensor([4, 6])
x-y= tensor([-2, -2])
x*y= tensor([3, 8])
x/y= tensor([0.33, 0.50])
x**y= tensor([ 1, 16])
x = torch.tensor([[1,2],[3,4]])
y = torch.tensor([1,2])
print('shape of x:', x.shape)
print('shape of y:', y.shape)
print('x+y=', x+y)
print('x-y=', x-y)
print('x*y=', x*y)
print('x/y=', x/y)
print('x**y=', x**y)
shape of x: torch.Size([2, 2])
shape of y: torch.Size([2])
x+y= tensor([[2, 4],
[4, 6]])
x-y= tensor([[0, 0],
[2, 2]])
x*y= tensor([[1, 4],
[3, 8]])
x/y= tensor([[1., 1.],
[3., 2.]])
x**y= tensor([[ 1, 4],
[ 3, 16]])
x = torch.tensor([1,4,5,6])
y = torch.tensor([2,3,5,7])
print('x==1:', x==1)
print('x==y:', x==y)
print('x!=y:', x!=y)
print('x>1:', x>1)
print('x>y:', x>y)
print('x>=y:', x>=y)
print('x<y:', x<y)
print('x<=y:', x<=y)
print('~(x<=y):', ~(x<=y))
x==1: tensor([ True, False, False, False])
x==y: tensor([False, False, True, False])
x!=y: tensor([ True, True, False, True])
x>1: tensor([False, True, True, True])
x>y: tensor([False, True, False, False])
x>=y: tensor([False, True, True, False])
x<y: tensor([ True, False, False, True])
x<=y: tensor([ True, False, True, True])
~(x<=y): tensor([False, True, False, False])
z = torch.tensor([-1, 1, 2, -2, -3])
print('z>0=', z>0)
print('z[z>0]', z[z>0])
z[z<0] = 0
print('z=:', z)
z>0= tensor([False, True, True, False, False])
z[z>0] tensor([1, 2])
z=: tensor([0, 1, 2, 0, 0])
x = torch.tensor([True, True, False, False])
y = torch.tensor([True, False, True, False])
print('and(x, y):', torch.logical_and(x, y))
print('or(x, y):', torch.logical_or(x, y))
print('not(x):', torch.logical_not(x))
and(x, y): tensor([ True, False, False, False])
or(x, y): tensor([ True, True, True, False])
not(x): tensor([False, False, True, True])
x = torch.tensor([True, True, False, False])
y = torch.tensor([True, False, True, False])
print('bit_and(x,y):', torch.bitwise_and(x,y))
print('bit_or(x,y):', torch.bitwise_or(x,y))
print('bit_not(x):', torch.bitwise_not(x))
bit_and(x,y): tensor([ True, False, False, False])
bit_or(x,y): tensor([ True, True, True, False])
bit_not(x): tensor([False, False, True, True])
x = torch.tensor([1,2,3,-1], dtype=torch.int8)
y = torch.tensor([2,3,4,-2], dtype=torch.int8)
print('bit_and(x,y):', torch.bitwise_and(x,y))
print('bit_or(x,y):', torch.bitwise_or(x,y))
print('bit_not(x):', torch.bitwise_not(x))
print('x<<2:', torch.bitwise_left_shift(x,2))
print('x>>2:', torch.bitwise_right_shift(x,2))
bit_and(x,y): tensor([ 0, 2, 0, -2], dtype=torch.int8)
bit_or(x,y): tensor([ 3, 3, 7, -1], dtype=torch.int8)
bit_not(x): tensor([-2, -3, -4, 0], dtype=torch.int8)
x<<2: tensor([ 4, 8, 12, -4], dtype=torch.int8)
x>>2: tensor([ 0, 0, 0, -1], dtype=torch.int8)