PyTorch基础与简单神经网络构建
1. PyTorch基础操作与内部结构
1.1 基础操作
如果你熟悉所有这些基本操作,就可以开始构建网络了。PyTorch 还有许多其他重要操作,以下是一些常见操作的示例代码:
import torch
# 创建随机张量
x = torch.rand(2,3,4)
x_with_2n3_dimension = x[1, :, :]
scalar_x = x[1,1,1] # first value from each dimension
# numpy 风格的切片
x = torch.rand(2,3)
print(x[:, 1:]) # skipping first column
print(x[:-1, :]) # skipping last row
# 转置
x = torch.rand(2,3)
print(x.t()) # size 3x2
# 拼接和堆叠
x = torch.rand(2,3)
concat = torch.cat((x,x))
print(concat) # Concatenates 2 tensors on zeroth dimension
x = torch.rand(2,3)
concat = torch.cat((x,x), dim=1)
print(concat) # Concatenates 2 tensors on first dimension
x = torch.rand(2,3)
stacked = torch.stack((x,x),
超级会员免费看
订阅专栏 解锁全文
8万+

被折叠的 条评论
为什么被折叠?



