torch零碎小知识

1.torch.rand()函数

均匀分布
torch.rand(*sizes, out=None) → Tensor

返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。

参数:

sizes (int...) - 整数序列,定义了输出张量的形状
out (Tensor, optinal) - 结果张量

print(torch.rand(1, 5)) # 1行5列

输出:

tensor([[0.0975, 0.9950, 0.9687, 0.8556, 0.3420]])


2.torch.randn()

torch.randn(*sizes, out=None) → Tensor

返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义。

参数:

sizes (int...) - 整数序列,定义了输出张量的形状
out (Tensor, optinal) - 结果张量

print(torch.randn(1,5))

输出:

tensor([[ 1.5800,  0.4972,  0.3574, -0.1394, -0.6761]])


参考链接:https://blog.csdn.net/wangwangstone/article/details/89815661

3.scatter函数,进行one-hot编码

scatter函数,中间参数表示修改位置的索引

https://www.cnblogs.com/dogecheng/p/11938009.html

4.pytorch 查看模型名字和参数

print('G模型参数名称')
for i in self.G.state_dict():
    print(i)
print('G模型参数值')
for i in self.G.named_parameters():
    print(i)

G模型:

self.fc = nn.Sequential(
            nn.Linear(self.input_size + self.class_num, 100),
            nn.BatchNorm1d(100),
            nn.Linear(100, self.out_size),
        )

G模型参数名称:数值表示网络的层号
fc.0.weight
fc.0.bias
fc.1.weight
fc.1.bias
fc.1.running_mean
fc.1.running_var
fc.1.num_batches_tracked
fc.2.weight
fc.2.bias

参数值:只有带有weight和bias层才有参数

原文地址:https://www.cnblogs.com/shuangcao/p/12864623.html