自主设计网站企业网站建设方案书

当前位置: 首页 > news >正文

自主设计网站,企业网站建设方案书,建设银行昆山分行网站,中国风网站模板html一.tensor创建
1.张量的定义 张量在形式上就是多维数组#xff0c;例如标量就是0维张量#xff0c;向量就是一维张量#xff0c;矩阵就是二维张量#xff0c;而三维张量就可以想象RGB图片#xff0c;每个channel是一个二维的矩阵#xff0c;共有三个channel#xff0…一.tensor创建
1.张量的定义 张量在形式上就是多维数组例如标量就是0维张量向量就是一维张量矩阵就是二维张量而三维张量就可以想象RGB图片每个channel是一个二维的矩阵共有三个channel还可以考虑更多。 在代码中创建张量Tensor数据类型时除了封装张量本身的数据data外还会附加张量的一些性质和操作例如数据的梯度grad创建tensor的函数grad_fun是求导的关键是否为叶子节点is_leaf是否需要梯度require_grad。 2.张量的创建 2.1 tensor直接创建 torch.tensor([1])   
arr  np.ones((3, 3)) t  torch.tensor(arr, devicecuda) # t torch.tensor(arr) 值得注意的点 1tensor括号里的数据可以是list以“()”表示也可以是数组以[]表示也可以是numpy即先用numpy创建一个numpy然后直接导入如下 2注意数据类型有时候需要在数字后面加“.”表示float因为求导时候需要float类型 3可以添加device’cuda’获得加速。 2.2 from_numpy从numpy中创建 arr  np.array([[1, 2, 3], [4, 5, 6]]) t  torch.from_numpy(arr) # arr[0, 0] 0 t[0, 0]  -1 值得注意的点 1这个创建的tensor和原来的numpy共享内存也即是说修改tensor就会修改原来的numpy。 2.3 从数字中创建

通过torch.zeros创建张量

out_t  torch.tensor([1]) #ttorch.zeros((3,3)) t  torch.zeros((3, 3), outout_t) 值得注意的点 1也可以先创建一个tensor然后在zeros函数的out接收创建的zeros二者的size可以不一样创建完成后二者一致。 2还可以torch.ones 3全1张量还可以用full函数 t  torch.full((3, 3), 1) 注意试验一下是不是还可以创建全“2”张量 4还可以利用torch.zeros_like()torch.ones_like()torch.full_like创建和input张量类似于size,只不过是用一个真实的张量表示一致的全0/1张量。 5torch.eye()创建单位对角矩阵 2.4 等差均分创建 t  torch.arange(2, 10, 2) 1创建等差数列张量后面为等差值默认为1. # t torch.linspace(2, 10, 5) t  torch.linspace(2, 10, 6) 1在[start,end]中均分n等份这时会出现小数。 2还可以等log创建torch.logspace() 2.5 依据概率创建

通过torch.normal创建正态分布张量 # mean张量 std: 张量 # mean torch.arange(1, 5, dtypetorch.float) # std torch.arange(1, 5, dtypetorch.float) # t_normal torch.normal(mean, std) # mean标量 std: 标量 # t_normal torch.normal(0., 1., size(4,)) # mean张量 std: 标量 mean  torch.arange(1, 5, dtypetorch.float) std  1 t_normal  torch.normal(mean, std)

1注意meanstd可以是标量和张量的组合共四种模式。 2torch.randn(),torch.randn_like()创建标准正态分布张量 3torch.rand(),torch.rand_like()创建[0,1]均匀分布。 4torch.randint(low,high),torch.randint_like(low,high)创建[low,high)均匀分布。 5torch.randperm(n)创建从0到n-1的随机排列张量 6torch.bernoulli(input)创建以input为概率值的伯努利分布张量。 3.张量的操作 3.1张量拼接 torch.cat(tensors,dim) t  torch.ones((2, 3)) t_0  torch.cat([t, t], dim0) t_1  torch.cat([t, t, t], dim1) 1是在原来的维度上进行拼接 torch.stack(tensor,dim) t  torch.ones((2, 3)) t_stack  torch.stack([t, t, t], dim0) 1是在新创建的维度上进行拼接如果维度小于现存的维度则创建该维度后后面的递推。比如t现在维度是2*3拼接后则是3*2*3其中后两维的2*3是原来的t。 3.2 张量切分 torch.chunk(input,chunk,dim) a  torch.ones((2, 7))  # 7 list_of_tensors  torch.chunk(a, dim1, chunks3)   # 3 for idx, t in enumerate(list_of_tensors): (1)在维度dim上进行chunk均分如果不能整除最后一份为余数。 torch.split(input,int/list,dim) t  torch.ones((2, 5)) list_of_tensors  torch.split(t, [2, 1, 1], dim1)  # [2 , 1, 2] for idx, t in enumerate(list_of_tensors): # list_of_tensors torch.split(t, [2, 1, 2], dim1) # for idx, t in enumerate(list_of_tensors): (1)为int时和chunk功能类似 2为list时可以按照设定值切分但总和要与input维度上值一致。 3.3 张量索引 torch.index_select(input,dim,select) t  torch.randint(0, 9, size(3, 3)) idx  torch.tensor([0, 2], dtypetorch.long)    # float t_select  torch.index_select(t, dim0, indexidx) (1)在dim维度上按照select索引数值。 torch.maksed_select(input,mask) t  torch.randint(0, 9, size(3, 3)) mask  t.le(5)  # ge is mean greater than or equal/   gt: greater than  le  lt t_select  torch.masked_select(t, mask) mask是和input同大小的布尔类型张量按照TRUE返回一维张量。 3.4张量变形 torch.reshape(input,shape) t  torch.randperm(8) t_reshape  torch.reshape(t, (-1, 2, 2))    # -1 t[0]  1024 3.5 张量维度交换 torch.transpose(input,dim1,dim2) # torch.transpose t  torch.rand((2, 3, 4)) t_transpose  torch.transpose(t, dim01, dim12)    # c*h*w     h*w*c (1)维度变换之后数据是如何变化的 2torch.t()二维张量矩阵转置 3.6 张量压缩 torch.sequeeze(input,dim) t  torch.rand((1, 2, 3, 1)) t_sq  torch.squeeze(t) t_0  torch.squeeze(t, dim0) t_1  torch.squeeze(t, dim1) 1默认压缩所有为1的维度也可以指定维度若指定维度不为1则不会压缩。 2torch.unsequeeze()扩展维度的值。 4.张量的数学运算 t_0  torch.randn((3, 3)) t_1  torch.ones_like(t_0) t_add  torch.add(t_0, 10, t_1) torch.add可同时执行乘法运算。 5.计算图 计算图是用来描述运算的有向无环图包括结点node和边edge。结点表示数据如向量矩阵张量等边表示运算如加减乘除卷积等。 import torch w  torch.tensor([1.], requires_gradTrue) x  torch.tensor([2.], requires_gradTrue) a  torch.add(w, x)     # retain_grad() b  torch.add(w, 1) y  torch.mul(a, b) y.backward() print(w.grad) # 查看叶子结点

print(is_leaf:\n, w.is_leaf, x.is_leaf, a.is_leaf, b.is_leaf, y.is_leaf) # 查看梯度

print(gradient:\n, w.grad, x.grad, a.grad, b.grad, y.grad) # 查看 grad_fn

print(grad_fn:\n, w.grad_fn, x.grad_fn, a.grad_fn, b.grad_fn, y.grad_fn) 6.自动求导 torch.autograd.backward(tensors,retain_graph) w  torch.tensor([1.], requires_gradTrue) x  torch.tensor([2.], requires_gradTrue) a  torch.add(w, x) b  torch.add(w, 1) y  torch.mul(a, b) y.backward(retain_graphTrue) # print(w.grad) y.backward() w  torch.tensor([1.], requires_gradTrue) x  torch.tensor([2.], requires_gradTrue) a  torch.add(w, x)     # retain_grad() b  torch.add(w, 1) y0  torch.mul(a, b)    # y0 (xw) * (w1) y1  torch.add(a, b)    # y1 (xw) (w1)    dy1/dw 2 loss  torch.cat([y0, y1], dim0)       # [y0, y1] grad_tensors  torch.tensor([1., 2.]) loss.backward(gradientgrad_tensors)    # gradient 传入 torch.autograd.backward()中的grad_tensors torch.autograd.grad(outputs,inputs,retain_graph) x  torch.tensor([3.], requires_gradTrue) y  torch.pow(x, 2)     # y x**2 grad_1  torch.autograd.grad(y, x, create_graphTrue)   # grad_1 dy/dx 2x 2 * 3 6 grad_2  torch.autograd.grad(grad_1[0], x)              # grad_2 d(dy/dx)/dx d(2x)/dx 2 autograd.grad()会返回梯度张量供保存。