1 import numpy as np 2 3 features = np.array([[1,2,3],[4,5,6],[7,8,9]]) 4 components = [True,True,True] 5 features = features[:, components] 6 print(features) 7 fea = np.array([[1,2,3],[4,5,6],[7,8,9]]) 8 components = [True,False,True] 9 fea = fea[:, components] 10 print(fea)
结果:
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1 3]
[4 6]
[7 9]]
2.生成均值为0,方差为1的离散正态分布
1 import torch 2 3 batch_size = 5 4 embedding_dim = 2 5 mean = torch.zeros(batch_size, embedding_dim) 6 print(mean) 7 std = mean + 1 8 print(std) 9 fakez = torch.normal(mean=mean, std=std) # 500*128,均值为0,方差为1的离散正态分布 10 print(fakez)
orch.randn(*sizes, out=None) → Tensor
返回一个张量,包含了从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数。
torch.rand(*sizes, out=None) → Tensor
返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。
3.torch中的repeat函数:之后的维度,对应维度相乘
1 alpha = torch.rand( 3, 1, 1)
2 print(alpha)
3 alpha = alpha.repeat(1, 2, 3)
4 print(alpha)
结果:
tensor([[[0.0029]],
[[0.0730]],
[[0.7339]]])
tensor([[[0.0029, 0.0029, 0.0029],
[0.0029, 0.0029, 0.0029]],
[[0.0730, 0.0730, 0.0730],
[0.0730, 0.0730, 0.0730]],
[[0.7339, 0.7339, 0.7339],
[0.7339, 0.7339, 0.7339]]])
将1改成2:
alpha = torch.rand( 3, 1, 1) print(alpha) alpha = alpha.repeat(2, 2, 3) print(alpha)
结果:6个两行三列
tensor([[[0.8134]],
[[0.6626]],
[[0.5064]]])
tensor([[[0.8134, 0.8134, 0.8134],
[0.8134, 0.8134, 0.8134]],
[[0.6626, 0.6626, 0.6626],
[0.6626, 0.6626, 0.6626]],
[[0.5064, 0.5064, 0.5064],
[0.5064, 0.5064, 0.5064]],
[[0.8134, 0.8134, 0.8134],
[0.8134, 0.8134, 0.8134]],
[[0.6626, 0.6626, 0.6626],
[0.6626, 0.6626, 0.6626]],
[[0.5064, 0.5064, 0.5064],
[0.5064, 0.5064, 0.5064]]])