常用网络层
一 卷积层
import os
import torch
import random
import numpy as np
import torchvision
import torch.nn as nn
from torchvision import transforms
from matplotlib import pyplot as plt
from PIL import Image
from tools.common_tools import transform_invert, set_seedset_seed(1)
path_img = os.path.join(os.path.dirname(os.path.abspath(__file__)), "lena.png")
img = Image.open(path_img).convert('RGB')
img_transform = transforms.Compose([transforms.ToTensor()])
img_tensor = img_transform(img)
img_tensor.unsqueeze_(dim=0)
flag = 0
if flag:maxpool_layer = nn.MaxPool2d((2, 2), stride=(2, 2)) img_pool = maxpool_layer(img_tensor)
flag = 0
if flag:avgpoollayer = nn.AvgPool2d((2, 2), stride=(2, 2)) img_pool = avgpoollayer(img_tensor)
flag = 0
if flag:img_tensor = torch.ones((1, 1, 4, 4))avgpool_layer = nn.AvgPool2d((2, 2), stride=(2, 2), divisor_override=3)img_pool = avgpool_layer(img_tensor)print("raw_img:\n{}\npooling_img:\n{}".format(img_tensor, img_pool))
flag = 0
if flag:img_tensor = torch.randint(high=5, size=(1, 1, 4, 4), dtype=torch.float)maxpool_layer = nn.MaxPool2d((2, 2), stride=(2, 2), return_indices=True)img_pool, indices = maxpool_layer(img_tensor)img_reconstruct = torch.randn_like(img_pool, dtype=torch.float)maxunpool_layer = nn.MaxUnpool2d((2, 2), stride=(2, 2))img_unpool = maxunpool_layer(img_reconstruct, indices)print("raw_img:\n{}\nimg_pool:\n{}".format(img_tensor, img_pool))print("img_reconstruct:\n{}\nimg_unpool:\n{}".format(img_reconstruct, img_unpool))
flag = 1
if flag:inputs = torch.tensor([[1., 2, 3]])linear_layer = nn.Linear(3, 4)linear_layer.weight.data = torch.tensor([[1., 1., 1.],[2., 2., 2.],[3., 3., 3.],[4., 4., 4.]])linear_layer.bias.data.fill_(0.5)output = linear_layer(inputs)print(inputs, inputs.shape)print(linear_layer.weight.data, linear_layer.weight.data.shape)print(output, output.shape)