<jupyter_start><jupyter_text>Numpy is the de-facto numerial computation library, so loading data from numpy is a basic skill for machine learning.<jupyter_code>import torch
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = torch.from_numpy(x)
print(y)<jupyter_output>tensor([[1, 2],
        [3, 4]])<jupyter_text>Recover numpy array from torch tensor<jupyter_code>z = y.numpy()
print(z)<jupyter_output>[[1 2]
 [3 4]]<jupyter_text>Numpy and torch tensors have many similarities.  Scalar<jupyter_code>a = np.array(1)
b = torch.tensor(1)
print(a, type(a))
print(b, type(b))<jupyter_output>1 <class 'numpy.ndarray'>
tensor(1) <class 'torch.Tensor'><jupyter_text>numpy scalar arithmetics<jupyter_code>a = np.array(1)
b = np.array(2)
print(a+b, b-a, a*b, a/b)<jupyter_output>3 1 2 0.5<jupyter_text>tensor scalar arithmetics<jupyter_code>a = torch.tensor(1)
b = torch.tensor(2)
print(a+b, b-a, a*b, a/b)<jupyter_output>tensor(3) tensor(1) tensor(2) tensor(0.5000)<jupyter_text>Matrix Matrix initialization<jupyter_code>a = np.zeros((3, 3))
b = np.ones((3, 3))
print(a, '\n', b)
a = torch.zeros((3, 3))
b = torch.ones((3, 3))
print(a, '\n', b)<jupyter_output>[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]] 
 [[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]) 
 tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])<jupyter_text>Random matrices<jupyter_code>np.random.seed(42)
a = np.random.randn(3, 3)
torch.manual_seed(42)
a = torch.randn(3, 3)<jupyter_output><empty_output><jupyter_text>Matrix arithmetics<jupyter_code>a = np.random.randn(2, 2)
b = np.random.randn(2, 2)
print(a + b, np.add(a, b))
print(a - b, np.subtract(a, b))
print(a.dot(b), np.dot(a, b))
print(a / b, np.divide(a, b))
print(np.transpose(a))
print(np.vstack([a, b]))
print(np.hstack([a, b]))
print(a.reshape(1, 4))
a = torch.randn(2, 2)
b = torch.randn(2, 2)
print(a+b, torch.add(a, b))
print(a-b, torch.sub(a, b))
print(a/b, torch.div(a, b))
print(torch.mm(a, b))
print(torch.t(a))
print(torch.cat((a, b)))
print(torch.cat((a, b), dim=1))
print(torch.vstack([a, b]))
print(torch.hstack([a, b]))
print(a.reshape(1, 4))<jupyter_output>tensor([[-0.9965, -0.2786],
        [ 0.1373,  3.1603]]) tensor([[-0.9965, -0.2786],
        [ 0.1373,  3.1603]])
tensor([[-0.3907,  2.2720],
        [ 1.0888, -1.6076]]) tensor([[-0.3907,  2.2720],
        [ 1.0888, -1.6076]])
tensor([[ 2.2896, -0.7815],
        [-1.2886,  0.3257]]) tensor([[ 2.2896, -0.7815],
        [-1.2886,  0.3257]])
tensor([[-0.2641,  3.2605],
        [-0.5551,  1.0690]])
tensor([[-0.6936,  0.6131],
        [ 0.9967,  0.7764]])
tensor([[-0.6936,  0.9967],
        [ 0.6131,  0.7764],
        [-0.3029, -1.2753],
        [-0.4758,  2.3839]])
tensor([[-0.6936,  0.9967, -0.3029, -1.2753],
        [ 0.6131,  0.7764, -0.4758,  2.3839]])
tensor([[-0.6936,  0.9967],
        [ 0.6131,  0.7764],
        [-0.3029, -1.2753],
        [-0.4758,  2.3839]])
tensor([[-0.6936,  0.9967, -0.3029, -1.2753],
        [ 0.6131,  0.7764, -0.4758,  2.3839]])
tensor([[-0.6936,  0.9967,  0.6131,  0.7764]])
{'fraction_non_alphanumeric': 0.24178327367393426, 'fraction_numerical': 0.14253172795314026, 'mean_word_length': 3.3479490806223478, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '584048', 'license': 'MIT', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1492, 'n_words': 276, 'path': 'tutorials/01-basics/pytorch_basics/3-torch-and-numpy.ipynb', 'repo_name': 'pkuwwt/pytorch-tutorial', 'size': '3073'}