2018
import numpy as np
given_list = [24, 12, 57]
new_array = np.array(given_list)
print(type(new_array))
# <class 'numpy.ndarray'>
import numpy as np
given_list = [24, 12, 57]
new_array = np.array(given_list)
print(type(new_array))
# <class 'numpy.ndarray'>
given_list = [24, 12, 57]
new_array = np.array(given_list)
print([x+3 for x in given_list])
# [27, 15, 60]
print(new_array+3)
# [27 15 60]
first_array = np.random.rand(128, 5)
second_array = np.random.rand(5, 128)
print(np.matmul(first_array, second_array))
'''
[[1.15351208 1.95227908 1.96715651 ... 1.98488703 1.2217091 2.22688756]
[1.29874346 1.74803279 1.89340905 ... 2.07696858 1.9904079 2.20042014]
...
[0.82158841 1.07577147 1.75924153 ... 1.68843334 1.36875145 1.2564471 ]
[1.42693331 2.52156631 2.39800496 ... 2.47794813 2.10389287 2.72979265]]
'''
def square_python(num=100000):
squares = []
for i in range(1, num):
squares.append(i ** 2)
def square_numpy(num=100000):
squares = np.arange(1, num) ** 2
%%timeit
square_python()
# 10 loops, best of 3: 38.6 ms per loop
%%timeit
square_numpy()
# 1000 loops, best of 3: 314 µs per loop
shape = (2, 3)
print(np.ones(shape))
# [[1. 1. 1.]
# [1. 1. 1.]]
shape = (2, 3)
print(np.ones(shape))
# [[1. 1. 1.]
# [1. 1. 1.]]
shape = (2, 3)
#print(np.ones(shape))
print(torch.ones(shape))
# tensor([[1., 1., 1.],
# [1., 1., 1.]])
shape = (2, 3)
#print(np.ones(shape))
print(torch.ones(shape))
# tensor([[1., 1., 1.],
# [1., 1., 1.]])
shape = (3, 3)
x = 2 * np.ones(shape)
y = np.eye(shape[0])
print(x+y)
shape = (3, 3)
x = 2 * np.ones(shape)
y = np.eye(shape[0])
print(x+y)
x = 2 * torch.ones(shape)
y = torch.eye(shape[0])
print(x+y)
shape = (3, 3)
x = 2 * np.ones(shape)
y = np.eye(shape[0])
print(x+y)
x = 2 * torch.ones(shape)
y = torch.eye(shape[0])
print(x+y)
np.array([[1, 2], [3, 4]])
np.array([[1, 2], [3, 4]])
torch.tensor([[1, 2], [3, 4]])
shape = (3, 3)
x = 2 * np.ones(shape)
y = np.eye(shape[0])
y = np.add(x, y)
np.add(x, y, out=y)
shape = (3, 3)
x = 2 * np.ones(shape)
y = np.eye(shape[0])
y = np.add(x, y)
np.add(x, y, out=y)
shape = (3, 3)
x = 2 * np.ones(shape)
y = np.eye(shape[0])
y = np.add(x, y)
np.add(x, y, out=y)
shape = (3, 3)
x = 2 * torch.ones(shape)
y = torch.eye(shape[0])
y = torch.add(x, y)
torch.add(x, y, out=y)
shape = (3, 3)
x = 2 * torch.ones(shape)
y = torch.eye(shape[0])
y = torch.add(x, y)
torch.add(x, y, out=y)
x = np.arange(10)
print(x[1:7:2])
# [1 3 5]
y = np.arange(35).reshape(5,7)
print(y[1:5:2,::3])
# [[ 7 10 13]
# [21 24 27]]
x = torch.arange(10)
print(x[1:7:2])
# tensor([1, 3, 5])
y = torch.arange(35).reshape(5,7)
print(y[1:5:2,::3])
# tensor([[ 7, 10, 13],
# [21, 24, 27]])
x = np.arange(10,1,-1)
indexing_array = np.array([3,3,-3,8])
print(x[indexing_array])
# [7 7 4 2]
indexing_array = np.array([[1,1],[2,3]])
print(x[indexing_array])
# [[9 9]
# [8 7]]
x = torch.arange(10,1,-1)
indexing_array = torch.tensor([3,3,-3,8])
print(x[indexing_array])
# tensor([7, 7, 4, 2])
indexing_array = torch.tensor([[1,1],[2,3]])
print(x[indexing_array])
# tensor([[9, 9],
# [8, 7]])
Numpy | Torch |
---|---|
axis | dim |
copy | clone |
np.expand_dims(x, 1) | x.unsqueeze(1) |
tile | repeat |
np.sum(np_array, axis=1)
torch.sum(torch_array, dim=1)
A more complete comparison is available at
https://github.com/shagunsodhani/pytorch-for-numpy-users
x = np.linspace(start=10.0, stop=20, num=5)
print(x)
# [10. 12.5 15. 17.5 20. ]
x = np.linspace(start=10.0, stop=20, num=5)
print(x)
# [10. 12.5 15. 17.5 20. ]
x = torch.linspace(start=10, end=20, steps=5)
print(x)
# tensor([10.0000, 12.5000, 15.0000, 17.5000, 20.0000])
x = np.linspace(start=10.0, stop=20, num=5)
print(x)
# [10. 12.5 15. 17.5 20. ]
x = torch.linspace(start=10, end=20, steps=5)
print(x)
# tensor([10.0000, 12.5000, 15.0000, 17.5000, 20.0000])
x = np.linspace(start=10.0, stop=20, num=5)
print(x)
# [10. 12.5 15. 17.5 20. ]
x = torch.linspace(start=10, end=20, steps=5)
print(x)
# tensor([10.0000, 12.5000, 15.0000, 17.5000, 20.0000])
%%timeit
np.random.seed(1)
n = 10000
x = np.array(np.random.randn(n,n),
dtype = np.float32)
y = np.matmul(x, x)
# 1 loop, best of 3: 36.6 s per loop
%%timeit
torch.manual_seed(1)
n = 10000
device = torch.device('cuda:0')
x = torch.rand(n, n,
dtype=torch.float32,
device=device)
y = torch.matmul(x, x)
# 10 loops, best of 3: 797 ms per loop
torch_array = torch.from_numpy(numpy_array)
numpy_array = torch_array.numpy()
shape = (5, 3)
numpy_array = np.array(shape)
# Make a Numpy array
shape = (5, 3)
numpy_array = np.array(shape)
# Make a Numpy array
torch_array = torch.from_numpy(numpy_array)
# Convert it into a Torch tensor
shape = (5, 3)
numpy_array = np.array(shape)
# Make a Numpy array
torch_array = torch.from_numpy(numpy_array)
# Convert it into a Torch tensor
recreated_numpy_array = torch_array.numpy()
# Convert the Torch tensor into Numpy array
shape = (5, 3)
numpy_array = np.array(shape)
# Make a Numpy array
torch_array = torch.from_numpy(numpy_array)
# Convert it into a Torch tensor
recreated_numpy_array = torch_array.numpy()
# Convert the Torch tensor into Numpy array
if (recreated_numpy_array == numpy_array).all():
print("Numpy -> Torch -> Numpy")
# Existing Numpy logic
#
# Move data to GPU
# Use GPUs for costly operations
# Move data back to Numpy
# Existing Numpy logic
gpu_device = torch.device('cuda:0')
cpu_device = torch.device('cpu')
tensor_on_gpu = tensor.to(gpu_device)
tensor_on_cpu = tensor.to(cpu_device)
numpy_array = np.array([1, 2, 3])
torch_array = torch.from_numpy(numpy_array)
torch_array[0] = -100
print(numpy_array[0])
# -100
numpy_array = np.array([1, 2, 3])
torch_array = torch.Tensor(numpy_array)
torch_array[0] = -100
print(numpy_array[0])
# 1
numpy_array = np.array([1, 2, 3])
torch_array = torch.from_numpy(numpy_array).to(gpu_device)
torch_array[0] = -100
print(numpy_array[0])
# 1
torch_array = torch.tensor([1, 2, 3],
device = gpu_device)
numpy_array = torch_array.numpy()
# TypeError: can't convert CUDA tensor to
# numpy. Use Tensor.cpu() to copy the
# tensor to host memory first.
torch_array = torch.tensor([1, 2, 3],
device = gpu_device)
numpy_array = torch_array
.to(cpu_device)
.numpy()
def gradient(w, x, y):
"""Compute gradient for Linear Regression"""
y_estimate = x.dot(w).squeeze()
error = (y.squeeze() - y_estimate)
gradient = -(1.0/len(x)) * error.dot(x)
return gradient
## Credits: https://www.cs.toronto.edu/~frossard/post/linear_regression/
torch.nn.Linear(input_size, 1)
Shagun Sodhani