![](https://img-blog.csdnimg.cn/b458ff8a2af042618f99241007f19058.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
PyTorch学习笔记
PyTorch学习笔记
ICDAT_GO
行到水穷处,坐看云起时
展开
-
PyTorch学习笔记(五):自动微分机制
1. 利用backward方法求导数loss.backward(),无返回值梯度信息在需要求导的变量的属性中:w1.grad、w2.grad# f(x) = a*x^2 + b*x + c的导数x = torch.tensor(0.0, requires_grad=True) # x需要被求导a = torch.tensor(1.0)b = torch.tensor(-2.0)c = torch.tensor(1.0)y = a*torch.pow(x,2) + b*x + c y.原创 2021-12-02 08:31:30 · 1204 阅读 · 0 评论 -
torch.where()
torch.where(condition, x, y) → TensorReturn a tensor of elements selected from either x or y, depending on condition.condition = torch.tensor([[0.6, 0.7], [0.8, 0.4]])print(condition)x = torch.zeros(2,2)print(x)y = torch.ones(2,2)print(y)# 输出ten.原创 2021-11-30 09:33:24 · 704 阅读 · 0 评论 -
PyTorch学习笔记(四):Tensor合并与分割
cata = torch.rand(4,32,8)b = torch.rand(5,32,8)print(torch.cat([a, b], dim=0).shape) # torch.Size([9, 32, 8])stack: create new dima = torch.rand(32,8)b = torch.rand(32,8)print(torch.stack([a, b], dim=0).shape) # torch.Size([2, 32, 8])split:原创 2021-11-30 09:20:58 · 1418 阅读 · 0 评论 -
PyTorch学习笔记(三):Tensor变换
view/reshapea = torch.rand(4,1,28,28)print(a.shape) # torch.Size([4, 1, 28, 28])print(a.view(4, 28*28).shape) # torch.Size([4, 784])print(a.reshape(4, 28*28).shape) # torch.Size([4, 784])print(a.view(4, -1).shape) #原创 2021-11-30 09:02:09 · 512 阅读 · 0 评论 -
PyTorch学习笔记(二):索引与切片
Indexing: dim 0 firsta = torch.rand(4,3,28,28)print(a.dim()) print(a.shape)# 输出4torch.Size([4, 3, 28, 28])print(a[0].shape) # torch.Size([3, 28, 28])print(a[0, 0].shape) # torch.Size([28, 28])print(a[0, 0, 0].shape) # torch.S原创 2021-11-29 21:49:42 · 280 阅读 · 0 评论 -
PyTorch学习笔记(一):创建Tensor
1、Tensor的维度和尺寸不同类型的数据可以用不同维度(dimension)的张量来表示,标量为0维张量,向量为1维张量,矩阵为2维以上的张量;可以简单地总结为:有几层中括号,就是多少维的张量;可以使用shape属性或者size()方法查看张量在每一维的长度;标量:# 标量(0维张量)scalar = torch.tensor(1)print(scalar)print(scalar.dim()) print(scalar.size())print(scalar.shape)# 输出te原创 2021-11-29 21:28:43 · 240 阅读 · 0 评论 -
nn.Parameter()
class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.embedding = nn.Embedding(5, 10) self.dense_1 = nn.Linear(10, 5, bias=True) self.dense_2 = nn.Linear(5, 1, bias=True) def原创 2021-11-29 20:46:30 · 1405 阅读 · 0 评论 -
torch.gather()
torch.gather(input, dim, index) → TensorGathers values along an axis specified by dim.For a 3-D tensor the output is specified by:out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0out[i][j][k] = input[i][index[i][j][k]][k] # if dim.原创 2021-11-23 08:46:30 · 1077 阅读 · 0 评论