<jupyter_start><jupyter_text>Tensor basics - Vector (1-D) operations Dr. <NAME>, CA 94536 April 2019<jupyter_code>import torch
import numpy as np<jupyter_output><empty_output><jupyter_text>Vector (tensor) addition<jupyter_code>u = torch.Tensor([1,0])
v = torch.Tensor([0,1])
u
v
w = u+v
w<jupyter_output><empty_output><jupyter_text>Vector multiplication with a scalar<jupyter_code>y = torch.Tensor([[1],[2]])
y
z= 1.5*y
z<jupyter_output><empty_output><jupyter_text>Linear combination<jupyter_code>u = torch.Tensor([1,2])
v = torch.Tensor([3,-1])<jupyter_output><empty_output><jupyter_text>$$2\times\begin{bmatrix}1 & 2\end{bmatrix}+3\times\begin{bmatrix}3 & -1\end{bmatrix}$$ = $$\begin{bmatrix}11 & -1\end{bmatrix}$$<jupyter_code>w = 2*u+3*v
w<jupyter_output><empty_output><jupyter_text>Product (element-wise) of two tensors<jupyter_code>u = torch.Tensor([[1],[3]])
v = torch.Tensor([[2],[4]])
u
v<jupyter_output><empty_output><jupyter_text>$$ u=\begin{bmatrix}1 \\ 3 \end{bmatrix}, \ \ v=\begin{bmatrix}2 \\ 4 \end{bmatrix} $$$$ u*v = \begin{bmatrix}1\times 2 \\ 3\times 4 \end{bmatrix}= \begin{bmatrix}2 \\ 12 \end{bmatrix}$$<jupyter_code>z = u*v
z<jupyter_output><empty_output><jupyter_text>DOT product using `dot` method<jupyter_code>u = torch.Tensor([1,2])
v = torch.Tensor([3,1])
u
v
torch.dot(u,v)<jupyter_output><empty_output><jupyter_text>Matrix multiplication between tensors.Remember to use reshape the tensors using the `reshape()` method to ensure proper matrix multiplication<jupyter_code>u = torch.Tensor([1,2])
v = torch.Tensor([[3],[1]])
u
u = u.reshape(2,1)
v = v.reshape(1,2)
u
u.shape
v.shape
z = torch.mm(u,v)
z<jupyter_output><empty_output><jupyter_text>Adding a scalar to a tensor (also known as broadcasting)<jupyter_code>u = torch.Tensor([1,2,3,4])
u
z=u+10
z<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1796143250688705, 'fraction_numerical': 0.03636363636363636, 'mean_word_length': 7.606635071090047, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '320764', 'license': 'MIT', 'n_tokens_mistral': 792, 'n_tokens_neox': 744, 'n_words': 125, 'path': 'Tensor basics - vector (1-D) operations.ipynb', 'repo_name': 'tirthajyoti/PyTorch_Machine_Learning', 'size': '1834'}