<jupyter_start><jupyter_text>Tutorial Chapter 1 - Tensor  Tensor Init<jupyter_code>import torch
import numpy as np
A=torch.empty((5,5),dtype=torch.long)
B=torch.randn((5,5),dtype=torch.float)
C=torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
D=np.zeros((5,5),dtype=np.uint8)
D=torch.tensor(D)
print(A)
print(B)
print(C)
print(D)
E=A+B
E[:]
#可索引 但是共享内存 修改一个都会变<jupyter_output><empty_output><jupyter_text>张量操作|函数|功能||:---:|:---:||Tensor(*sizes)|基础构造函数||tensor(data,)|类似np.array的构造函数||ones(*sizes)|全1Tensor||zeros(*sizes)|全0Tensor||eye(*sizes)|对角线为1，其他为0||arange(s,e,step)|从s到e，步长为step||linspace(s,e,steps)|从s到e，均匀切分成steps份||rand/randn(*sizes)|均匀/标准分布||normal(mean,std)/uniform(from,to)|正态分布/均匀分布||randperm(m)|随机排列|The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions$ d, d+1, \dots, d+kd,d+1,…,d+k $that satisfy the following contiguity-like condition that $\forall i = 0, \dots, k-1∀i=0,…,k−1 ,$$$\text{stride}[i] = \text{stride}[i+1] \times \text{size}[i+1]$$Otherwise, contiguous() needs to be called before the tensor can be viewed. See also: reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.<jupyter_code>A=torch.randn((5,5))
print(A)
B=A.view(25)
print(B)
print("max value index is ",int(B.argmax(0).float().item()))<jupyter_output>tensor([[-1.0922,  0.0418,  0.4470,  0.1606,  1.3236],
        [ 1.9200,  1.3230,  1.4505,  2.0114,  0.0912],
        [ 1.7515, -0.3208,  3.1449, -1.5550,  0.4567],
        [-0.4851,  0.9575, -0.6078,  0.0127, -0.7880],
        [ 0.1331,  1.0679,  0.0655,  1.1302,  0.9566]])
tensor([-1.0922,  0.0418,  0.4470,  0.1606,  1.3236,  1.9200,  1.3230,  1.4505,
         2.0114,  0.0912,  1.7515, -0.3208,  3.1449, -1.5550,  0.4567, -0.4851,
         0.9575, -0.6078,  0.0127, -0.7880,  0.1331,  1.0679,  0.0655,  1.1302,
         0.9566])
max value index is  12<jupyter_text>张量运算构造：|函数|功能||:---:|:---:||Tensor(*sizes)|基础构造函数||tensor(data,)|类似np.array的构造函数||ones(*sizes)|全1Tensor||zeros(*sizes)|全0Tensor||eye(*sizes)|对角线为1，其他为0||arange(s,e,step)|从s到e，步长为step||linspace(s,e,steps)|从s到e，均匀切分成steps份||rand/randn(*sizes)|均匀/标准分布||normal(mean,std)/uniform(from,to)|正态分布/均匀分布||randperm(m)|随机排列|线性代数操作：| 函数	|功能||:---:|:---:||trace|	对角线元素之和(矩阵的迹)||diag|	对角线元素||triu/tril	|矩阵的上三角/下三角，可指定偏移量||mm/bmm	|矩阵乘法，batch的矩阵乘法||addmm/addbmm/addmv/addr/baddbmm..|	矩阵运算||t|转置||dot/cross|	内积/外积||inverse	|求逆矩阵||svd	|奇异值分解|  自动梯度autograd包能够根据输入和前向传播过程自动构建计算图，并执行反向传播<jupyter_code>import torch
from torch.autograd import Variable
x = Variable(torch.randn(3), requires_grad=True)
y = Variable(torch.randn(3), requires_grad=True)
z = Variable(torch.randn(3), requires_grad=True)
print(x)
print(y)
print(z)

t = x + y
l = t.dot(z)<jupyter_output>tensor([-0.1762,  0.2599, -1.7669], requires_grad=True)
tensor([ 0.3748, -1.2066,  0.2440], requires_grad=True)
tensor([-0.9962,  1.1738, -0.2715], requires_grad=True)<jupyter_text>![formular](https://www.zhihu.com/equation?tex=l+%3D+%28x%2By%29%5ETz%2C+dl%2Fdx+%3D+dl%2Fdy+%3D+z%2C+dl%2Fdz%3Dx%2By%3Dt%2C+dl%2Fdt%3Dz)<jupyter_code>l.backward(retain_graph=True)
print(x.grad)
print(y.grad) # x.grad = y.grad = z
print(z)

print(z.grad) # z.grad = t = x + y
print(t)
x.grad.data.zero_()
y.grad.data.zero_()
z.grad.data.zero_()

t.backward(z) #dl/dt=z dl/z=dt
print(x.grad)
print(y.grad)<jupyter_output>tensor([-0.9962,  1.1738, -0.2715])
tensor([-0.9962,  1.1738, -0.2715])
{'fraction_non_alphanumeric': 0.20584257506086015, 'fraction_numerical': 0.10440898025426021, 'mean_word_length': 5.364888123924269, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '423432', 'license': 'Apache-2.0', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1945, 'n_words': 270, 'path': 'Resources/WikiResources/DiveIntoPytorch.ipynb', 'repo_name': 'OOXXXXOO/WSNet', 'size': '3697'}