torch使用

我目前经常的引用:

1
2
3
import torch as th
import torch.nn as nn
import torch.nn.functional as F

torch

torch张量初始化时require_grad=False,如果需要梯度要自己设置

1
2
3
4
5
6
7
8
9
10
11
12
a = th.eye(n, m=None) # 对角线为1其他为0,注意这里的对角线指将非方阵补齐为方阵后的对角线
a = th.from_numpy(ndarray) # 返回的张量tensor和numpy的ndarray共享同一内存空间,修改一个会导致另一个也被修改,返回的张量不能改变大小
a = th.linspace(start, end, steps=100) # 返回一个1维张量,包含在start和end上均匀间隔的steps个点
a = th.logspace(start, end, steps=100) # 返回一个1维张量,包含在区间10^start和10^end上以对数刻度均匀间隔的steps个点
a = th.ones(*sizes)
a = th.rand(*sizes) # 返回一个张量,包含了从区间(0, 1)的均匀分布中抽取的一组随机数,形状由可变参数sizes定义
a = th.rand(*sizes) # 标准正态分布
a.reshape(dim0, dim1, dim2) # view和它功能相同但需要注意连续,reshape可全面替换view
a = th.arange(start, end, step=1) # 返回一个1维张量,长度为((end-start)/step),以step为步长的一组序列值
a = th.cat([eg1,eg2,eg3], 0) # torch.cat(inputs, dimension=0), 连接
a = th.chunk(tensor, chunks, dim=0) # chunks:数量, 分块

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023 J-sycamore