<jupyter_start><jupyter_text>![img](images/Tensor.jpg)<jupyter_code>import torch
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1D Tensor<jupyter_code>x = torch.FloatTensor(5)
x<jupyter_output><empty_output><jupyter_text>2D Tensor<jupyter_code>from sklearn.datasets import load_iris

iris = load_iris()
iris.data.shape
iris.feature_names
iris_tensor = torch.from_numpy(iris.data)
iris_tensor.size()<jupyter_output><empty_output><jupyter_text>3D Tensor<jupyter_code>from PIL import Image

panda = np.array(Image.open('images/panda.jpg'))
panda_tensor = torch.from_numpy(panda)
panda_tensor.size()
plt.imshow(panda)
plt.imshow(panda_tensor[:,:,0].numpy());
plt.imshow(panda_tensor[200:500,100:500,:].numpy());
diagonal = torch.eye(3,3)
diagonal<jupyter_output><empty_output><jupyter_text>4D Tensor<jupyter_code>from glob import glob

# Read images from disk
cats = glob('images/cat*.jpg')

cat_imgs = np.array([np.array(Image.open(cat).resize((224,224))) for cat in cats])
print(cat_imgs.shape)

cat_imgs = cat_imgs.reshape(-1,224,224,3)
cat_tensors = torch.from_numpy(cat_imgs)
print(cat_tensors.size())<jupyter_output>(5, 224, 224, 3)
torch.Size([5, 224, 224, 3])<jupyter_text>Auto Gradient<jupyter_code>from torch.autograd import Variable

x = Variable(torch.ones(2,2), requires_grad=True)
x
y = x.mean()
y
y.backward()   # compute the gradients
x.grad
y.backward()
x.grad
y.backward()
x.grad
y.backward()
x.grad
print(x.grad_fn)
print(y.grad_fn)
x.data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1464516129032258, 'fraction_numerical': 0.03354838709677419, 'mean_word_length': 8.343373493975903, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '253319', 'license': 'MIT', 'n_tokens_mistral': 682, 'n_tokens_neox': 644, 'n_words': 95, 'path': 'Deep_Learning_with_PyTorch/02_Building_Blocks_Tensor.ipynb', 'repo_name': 'woosa7/pytorch', 'size': '1550'}