1.对于图片(NxCxHxW)的2D卷积与反卷积
卷积
input1 = torch.randn(32, 3, 12, 12)
ngf = 8
downsample = nn.Conv2d(3, ngf*2, 4, stride=1, padding=0)
h = downsample(input1)
print(h.size())
>>>torch.Size([32, 16, 9, 9])
反卷积
input1 = torch.randn(32, 3, 12, 12)
ngf = 8
upsample = nn.ConvTranspose2d(ngf*2, ngf, 4, stride=1, padding=0)
output = upsample(h, output_size=input1.size())
print(output.size())
>>>torch.Size([32, 8, 12, 12])
归一化
m = nn.BatchNorm2d(50)
input = torch.randn(20, 50, 35, 45)
output = m(input)
print(output.size())
>>>torch.Size([20, 50, 35, 45])
BatchNorm2d(n)中的参数n与tensor(N x C x H x W)中的C必须相同
2.GeneratorVideo中的get_gru_initial_state函数
a = Variable(torch.FloatTensor(num_samples,dim_z_motion).normal_())
a = torch.FloatTensor(5,3).normal_()
a
>>>tensor([[-1.3049, 0.0021, 0.1509],
[ 0.3527, 0.5780, 0.0437],
[-0.2406, -0.8861, 0.1983],
[ 1.0273, 1.0479, -0.1404],
[ 0.5898, 0.0900, 0.1426]])
FloatTensor函数返回一个tensor张量,第一个参数为数组的个数,第二个参数为每个数组的维度。此处即为采样的个数和要构建的gru单元的motion的维度。
*normal_(mean=0, std=1, , generator=None) 函数则是根据参数mean和std从正态分布中取值
故此处函数的作用是返回一个num_sample X dim_z_motion 维度,从正态分布中取值的tensor作为gru的初始值
Variable 将tensor封装之后可以调用.backward()函数进行梯度计算
3.sample_z_categ
首先根据所要分配给类别的向量数,构建随机整数的向量作为类别初始值
构建一个全零向量,维度为采样视频数 x 类别向量数
根据初始类别和全零向量矩阵构建每个video的one_hot矩阵
为了给video中每帧图片类别值,将video的每行one_hot复制n份,n为video_length.
返回所有帧的类别、所有视频的类别
实验示例
a= np.random.randint(6, size=8)
b = np.zeros((8, 6), dtype=np.float32)
b[np.arange(8),a] = 1
c = np.repeat(b, 3, axis=0)
print(a)
print(b)
print(c)
[0 2 0 3 5 3 1 0]
[[1. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 1.]
[0. 0. 0. 1. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]]
[[1. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 1.]
[0. 0. 0. 0. 0. 1.]
[0. 0. 0. 0. 0. 1.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0.]]
4.cat(tensor,dim)函数
可看作split函数的反向
将若干个数组拼合成一个
dim选择要合并的维度