1-- torch.nn.Sequential( )函数
作用:通过顺序序列创建神经网络结构
示例:调用Block函数时,将顺序执行括号内的Function。
import torch
x = torch.randn(1, 1, 64, 64)
Block = torch.nn.Sequential(
torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, stride = 1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 3, stride = 1),
torch.nn.ReLU()
)
result = Block(x)
print(result)
上述代码等同于:
import torch
Conv1 = torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, stride = 1)
Conv2 = torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 3, stride = 1),
Relu = torch.nn.ReLU()
x = torch.randn(1, 1, 64, 64)
print(x)
x = Conv1(x)
x = Relu(x)
x = Conv2(x)
result = Relu(x)
print(result)
############################
2--torch.flatten()函数
作用:按维度进行拼接;torch.flatten(input, start_dim = 0, end_dim = -1);input为输入tensor数据,start_dim为拼接的起始维度,end_dim为拼接的终止维度。
示例:
import torch
x = torch.randn(2, 3, 3)
print(x)
result1 = torch.flatten(x, start_dim = 0, end_dim = 2)
print(result1)
result2 = torch.flatten(x, start_dim = 0, end_dim = 1)
print(result2)
result3 = torch.flatten(x, start_dim = 1, end_dim = 2)
print(result3)
结果:
tensor([[[ 0.3546, -0.8551, 2.3490],
[-0.0920, 0.0773, -0.4556],
[-1.6943, 1.4517, -0.0767]],
[[-0.6950, 0.4382, -1.2691],
[-0.0252, -0.4980, -0.5994],
[-0.2581, -0.2544, -0.6787]]]) #X
tensor([ 0.3546, -0.8551, 2.3490, -0.0920, 0.0773, -0.4556, -1.6943, 1.4517,
-0.0767, -0.6950, 0.4382, -1.2691, -0.0252, -0.4980, -0.5994, -0.2581,
-0.2544, -0.6787]) #result1
tensor([[ 0.3546, -0.8551, 2.3490],
[-0.0920, 0.0773, -0.4556],
[-1.6943, 1.4517, -0.0767],
[-0.6950, 0.4382, -1.2691],
[-0.0252, -0.4980, -0.5994],
[-0.2581, -0.2544, -0.6787]]) #result2
tensor([[ 0.3546, -0.8551, 2.3490, -0.0920, 0.0773, -0.4556, -1.6943, 1.4517,
-0.0767],
[-0.6950, 0.4382, -1.2691, -0.0252, -0.4980, -0.5994, -0.2581, -0.2544,
-0.6787]]) #result3
|