In [1]:
import torch
In [2]:
x = torch.tensor([3.0])
In [3]:
y = torch.tensor([2.0])
In [4]:
x + y, x * y, x / y, x**y
Out[4]:
In [5]:
x = torch.arange(12)
In [6]:
x
Out[6]:
In [7]:
x[3]
Out[7]:
In [8]:
len(x)
Out[8]:
In [9]:
x.shape
Out[9]:
In [10]:
y = torch.ones(12, dtype=torch.float32)
In [11]:
y
Out[11]:
In [12]:
x = x.float()
In [13]:
x
Out[13]:
In [14]:
torch.dot(x, y)
Out[14]:
In [15]:
torch.sum(x * y)
Out[15]:
In [16]:
torch.prod(x)
Out[16]:
In [17]:
torch.prod(y)
Out[17]:
In [18]:
x + y
Out[18]:
In [19]:
torch.sum(x)
Out[19]:
In [20]:
torch.sum(y)
Out[20]:
In [21]:
x.mean()
Out[21]:
In [22]:
x.numel()
Out[22]:
In [23]:
x_size = float(x.numel())
In [24]:
x.sum() / x_size
Out[24]:
In [25]:
x.reshape(3, 4)
Out[25]:
In [26]:
X = torch.arange(12, dtype=torch.float32).reshape((3,4))
In [27]:
X
Out[27]:
In [28]:
torch.argmax(X, dim = 0)
Out[28]:
In [29]:
torch.argmax(X, dim = 1)
Out[29]:
In [30]:
X.sum(axis = 0)
Out[30]:
In [31]:
X.sum(axis = 1)
Out[31]:
In [32]:
X.sum(axis = [0, 1])
Out[32]:
In [33]:
X.sum()
Out[33]:
In [34]:
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
In [35]:
Y
Out[35]:
In [36]:
torch.argmax(Y, dim = 0)
Out[36]:
In [37]:
torch.argmax(Y, dim = 1)
Out[37]:
In [38]:
torch.cat((X, Y), dim=0)
Out[38]:
In [39]:
torch.cat((X, Y), dim=1)
Out[39]:
In [40]:
X + Y
Out[40]:
In [41]:
Z = X.T
In [42]:
Z
Out[42]:
In [43]:
X * Y
Out[43]:
In [44]:
A = torch.mm(Z, Z.T)
In [45]:
A
Out[45]:
In [46]:
A_symm = (A + A.T) / 2.0
In [47]:
A_symm
Out[47]:
In [48]:
A_symm == A_symm.T
Out[48]:
In [49]:
sum_X = X.sum(axis=1, keepdims=True)
In [50]:
sum_X
Out[50]:
In [51]:
X / sum_X
Out[51]:
In [52]:
X.cumsum(axis=0)
Out[52]:
In [53]:
X.cumsum(axis=1)
Out[53]:
In [54]:
Z = torch.arange(24).reshape(2, 3, 4)
In [55]:
Z
Out[55]:
In [56]:
a = 2
In [57]:
a + Z
Out[57]:
In [58]:
a * Z
Out[58]:
In [59]:
b = torch.tensor([2.0, 1, 4, 3])
In [60]:
b
Out[60]:
In [61]:
X
Out[61]:
In [62]:
torch.mv(X, b)
Out[62]:
In [63]:
u = torch.tensor([3.0, -4.0])
In [64]:
torch.norm(u, p=2)
Out[64]:
In [65]:
torch.abs(u).sum()
Out[65]:
In [66]:
torch.norm(u, p=1)
Out[66]:
In [67]:
import numpy as np
In [68]:
torch.norm(u, p=np.inf)
Out[68]: