[go: up one dir, main page]

0% found this document useful (0 votes)
19 views7 pages

Fromnumpytopytorch

The document provides a comprehensive comparison between Numpy and PyTorch, detailing their respective data types, methods for creating arrays, numerical ranges, linear algebra operations, and various attributes. It also covers indexing, shape manipulation, item selection, calculations, arithmetic operations, and random number generation. Each section lists equivalent functions or methods in both libraries, facilitating easier transition and understanding for users familiar with one of the frameworks.

Uploaded by

Asma Mlayah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Fromnumpytopytorch

The document provides a comprehensive comparison between Numpy and PyTorch, detailing their respective data types, methods for creating arrays, numerical ranges, linear algebra operations, and various attributes. It also covers indexing, shape manipulation, item selection, calculations, arithmetic operations, and random number generation. Each section lists equivalent functions or methods in both libraries, facilitating easier transition and understanding for users familiar with one of the frameworks.

Uploaded by

Asma Mlayah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Types

Numpy PyTorch

np.ndarra
torch.Tensor
y

np.float3 torch.float32;
2 torch.float

np.float6 torch.float64;
4 torch.double

np.float1 torch.float16;
6 torch.half

np.int8 torch.int8

np.uint8 torch.uint8

np.int16 torch.int16; torch.short

np.int32 torch.int32; torch.int

np.int64 torch.int64; torch.long

Ones and zeros


Numpy PyTorch

np.empty((2,
torch.empty(2, 3)
3))

np.empty_like( torch.empty_like(
x) x)

np.eye torch.eye

np.identity torch.eye

np.ones torch.ones

np.ones_like torch.ones_like

np.zeros torch.zeros

np.zeros_like torch.zeros_like

From existing data


Numpy PyTorch

np.array([[1, 2], [3, 4]]) torch.tensor([[1, 2], [3, 4]])

np.array([3.2, 4.3],
dtype=np.float16) torch.tensor([3.2, 4.3],
np.float16([3.2, 4.3]) dtype=torch.float16)

x.copy() x.clone()

x.astype(np.float32) x.type(torch.float32); x.float()

np.fromfile(file) torch.tensor(torch.Storage(file))

np.frombuffer

np.fromfunction

np.fromiter

np.fromstring

np.load torch.load

np.loadtxt

np.concatenate torch.cat

Numerical ranges
Numpy PyTorch

np.arange(10) torch.arange(10)

np.arange(2, 3, torch.arange(2, 3,
0.1) 0.1)

np.linspace torch.linspace

np.logspace torch.logspace

Linear algebra
Numpy PyTorch

np.dot torch.dot # 1D arrays only


torch.mm # 2D arrays only
Numpy PyTorch

torch.mv # matrix-vector (2D x


1D)

np.matmul torch.matmul

np.tensord
torch.tensordot
ot

np.einsum torch.einsum

Building matrices
Numpy PyTorch

np.dia torch.dia
g g

np.tri torch.tri
l l

np.tri torch.tri
u u

Attributes
Numpy PyTorch

x.shape;
x.shape
x.size()

x.stride
x.stride()
s

x.ndim x.dim()

x.data x.data

x.size x.nelement()

x.dtype x.dtype

Indexing
Numpy PyTorch

x[0] x[0]

x[:, 0] x[:, 0]

x[indices] x[indices]

np.take(x, torch.take(x,
indices) torch.LongTensor(indices))

x[x != 0] x[x != 0]

Shape manipulation
Numpy PyTorch

x.reshape x.reshape; x.view

x.resize() x.resize_

x.resize_as_

x =
np.arange(6).reshape(3, 2, x = torch.arange(6).reshape(3, 2, 1)
1) x.permute(2, 0, 1); x.transpose(1,
x.transpose(2, 0, 1) # 2).transpose(0, 1) # 012 -> 021 -> 201
012 -> 201

x.flatten x.view(-1)

x.squeeze() x.squeeze()

x[:, None];
x[:, None]; x.unsqueeze(1)
np.expand_dims(x, 1)

Item selection and manipulation


Numpy PyTorch

np.put

x.put x.put_

x = np.array([1, 2, 3]) x = torch.tensor([1, 2, 3])


x.repeat(2) # [1, 1, 2, 2, x.repeat_interleave(2) # [1, 1, 2, 2, 3,
3, 3] 3]
Numpy PyTorch

x.repeat(2) # [1, 2, 3, 1, 2, 3]
x.repeat(2).reshape(2, -1).transpose(1,
0).reshape(-1)
# [1, 1, 2, 2, 3, 3]

np.tile(x, (3, 2)) x.repeat(3, 2)

x = np.array([[0, 1], [2, 3], x = torch.tensor([[0, 1], [2, 3], [4, 5]])
[4, 5]]) idxs = torch.tensor([0, 2])
idxs = np.array([0, 2]) x[idxs, torch.arange(x.shape[1])] # [0, 5]
np.choose(idxs, x) # [0, 5] torch.gather(x, 0, idxs[None, :])[0] # [0,
5]

np.sort sorted, indices = torch.sort(x, [dim])

np.argsort sorted, indices = torch.sort(x, [dim])

np.nonzero torch.nonzero

np.where torch.where

x[::-1] torch.flip(x, [0])

np.unique(x) torch.unique(x)

Calculation
Numpy PyTorch

x.min x.min

x.argmin x.argmin

x.max x.max

x.argmax x.argmax

x.clip x.clamp

x.round x.round

torch.floor(x);
np.floor(x)
x.floor()

torch.ceil(x);
np.ceil(x)
x.ceil()

x.trace x.trace
Numpy PyTorch

x.sum x.sum

x.sum(axis=
x.sum(0)
0)

x.cumsum x.cumsum

x.mean x.mean

x.std x.std

x.prod x.prod

x.cumprod x.cumprod

x.all x.all

x.any x.any

Arithmetic and comparison operations


Numpy PyTorch

np.less x.lt

np.less_equal x.le

np.greater x.gt

np.greater_equ
x.ge
al

np.equal x.eq

np.not_equal x.ne

Random numbers
Numpy PyTorch

torch.manual_se
np.random.seed
ed

np.random.permutation torch.randperm(
(5) 5)
Numerical operations
Numpy PyTorch

np.sig torch.sig
n n

np.sqr torch.sqr
t t

You might also like