|
- import torch
- from torch import nn, einsum
- import torch.nn.functional as F
- from sklearn.metrics import mean_squared_error
- from torch.utils.data import Dataset, DataLoader
- import random
- import numpy as np
- from einops import rearrange, repeat
- from einops.layers.torch import Rearrange
- from torch.nn import Flatten
- from tqdm import tqdm
-
-
- class DoubleConvDS(nn.Module):
- """(convolution => [BN] => ReLU) * 2"""
-
- def __init__(self, in_channels, out_channels, mid_channels=None, kernels_per_layer=1):
- super().__init__()
- if not mid_channels:
- mid_channels = out_channels
- self.double_conv = nn.Sequential(
- DepthwiseSeparableConv(in_channels, mid_channels, kernel_size=3, kernels_per_layer=kernels_per_layer, padding=1),
- nn.BatchNorm2d(mid_channels),
- nn.ReLU(inplace=True),
- DepthwiseSeparableConv(mid_channels, out_channels, kernel_size=3, kernels_per_layer=kernels_per_layer, padding=1),
- nn.BatchNorm2d(out_channels),
- nn.ReLU(inplace=True)
- )
-
- def forward(self, x):
- return self.double_conv(x)
-
-
- class DoubleConv(nn.Sequential):
- def __init__(self, in_channels, out_channels, mid_channels=None):
- if mid_channels is None:
- mid_channels = out_channels
- super(DoubleConv, self).__init__(
- nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1, bias=False),
- nn.BatchNorm2d(mid_channels),
- nn.ReLU(inplace=True),
- nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1, bias=False),
- nn.BatchNorm2d(out_channels),
- nn.ReLU(inplace=True)
- )
-
- class Down(nn.Sequential):
- def __init__(self, in_channels, out_channels):
- super(Down, self).__init__(
- nn.MaxPool2d(2, stride=2),
- DoubleConvDS(in_channels, out_channels)
- )
-
- class Up(nn.Module):
- def __init__(self, in_channels, out_channels, bilinear=True):
- super(Up, self).__init__()
- if bilinear:
- self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
- self.conv = DoubleConvDS(in_channels, out_channels, in_channels // 2)
- else:
- self.up = nn.ConvTranspose2d(in_channels, in_channels // 2, kernel_size=2, stride=2)
- self.conv = DoubleConvDS(in_channels, out_channels)
-
- def forward(self, x1: torch.Tensor, x2: torch.Tensor) -> torch.Tensor:
- x1 = self.up(x1)
- # [N, C, H, W]
- diff_y = x2.size()[2] - x1.size()[2]
- diff_x = x2.size()[3] - x1.size()[3]
-
- # padding_left, padding_right, padding_top, padding_bottom
- x1 = F.pad(x1, [diff_x // 2, diff_x - diff_x // 2,
- diff_y // 2, diff_y - diff_y // 2])
-
- x = torch.cat([x2, x1], dim=1)
- x = self.conv(x)
- return x
-
- class OutConv(nn.Sequential):
- def __init__(self, in_channels, num_classes):
- super(OutConv, self).__init__(
- nn.Conv2d(in_channels, num_classes, kernel_size=1)
- )
-
- class ChannelAttention(nn.Module):
- def __init__(self, input_channels, reduction_ratio=16):
- super(ChannelAttention, self).__init__()
- self.input_channels = input_channels
- self.avg_pool = nn.AdaptiveAvgPool2d(1)
- self.max_pool = nn.AdaptiveMaxPool2d(1)
- # https://github.com/luuuyi/CBAM.PyTorch/blob/master/model/resnet_cbam.py
- # uses Convolutions instead of Linear
- self.MLP = nn.Sequential(
- Flatten(),
- nn.Linear(input_channels, input_channels // reduction_ratio),
- nn.ReLU(),
- nn.Linear(input_channels // reduction_ratio, input_channels)
- )
-
- def forward(self, x):
- # Take the input and apply average and max pooling
- avg_values = self.avg_pool(x)
- max_values = self.max_pool(x)
- out = self.MLP(avg_values) + self.MLP(max_values)
- scale = x * torch.sigmoid(out).unsqueeze(2).unsqueeze(3).expand_as(x)
- return scale
-
-
- class SpatialAttention(nn.Module):
- def __init__(self, kernel_size=7):
- super(SpatialAttention, self).__init__()
- assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
- padding = 3 if kernel_size == 7 else 1
- self.conv = nn.Conv2d(2, 1, kernel_size=kernel_size, padding=padding, bias=False)
- self.bn = nn.BatchNorm2d(1)
-
- def forward(self, x):
- avg_out = torch.mean(x, dim=1, keepdim=True)
- max_out, _ = torch.max(x, dim=1, keepdim=True)
- out = torch.cat([avg_out, max_out], dim=1)
- out = self.conv(out)
- out = self.bn(out)
- scale = x * torch.sigmoid(out)
- return scale
-
-
- class CBAM(nn.Module):
- def __init__(self, input_channels, reduction_ratio=16, kernel_size=7):
- super(CBAM, self).__init__()
- self.channel_att = ChannelAttention(input_channels, reduction_ratio=reduction_ratio)
- self.spatial_att = SpatialAttention(kernel_size=kernel_size)
-
- def forward(self, x):
- out = self.channel_att(x)
- out = self.spatial_att(out)
- return out
-
- #深度可分离卷积
- class DepthwiseSeparableConv(nn.Module):
- def __init__(self, in_channels, output_channels, kernel_size, padding=0, kernels_per_layer=1):
- super(DepthwiseSeparableConv, self).__init__()
- # In Tensorflow DepthwiseConv2D has depth_multiplier instead of kernels_per_layer
- self.depthwise = nn.Conv2d(in_channels, in_channels * kernels_per_layer, kernel_size=kernel_size,
- padding=padding,
- groups=in_channels)
- self.pointwise = nn.Conv2d(in_channels * kernels_per_layer, output_channels, kernel_size=1)
-
- def forward(self, x):
- x = self.depthwise(x)
- x = self.pointwise(x)
- return x
-
-
- class SmaAt_UNet(nn.Module):
- def __init__(self, n_channels = 175, n_classes = 69, kernels_per_layer=2, bilinear=True, reduction_ratio=16):
- super(SmaAt_UNet, self).__init__()
- self.n_channels = n_channels
- self.n_classes = n_classes
- kernels_per_layer = kernels_per_layer
- self.bilinear = bilinear
- reduction_ratio = reduction_ratio
-
- self.inc = DoubleConvDS(self.n_channels, 64)
- self.cbam1 = CBAM(64, reduction_ratio=reduction_ratio)
- self.down1 = Down(64, 128)
- self.cbam2 = CBAM(128, reduction_ratio=reduction_ratio)
- self.down2 = Down(128, 256)
- self.cbam3 = CBAM(256, reduction_ratio=reduction_ratio)
- self.down3 = Down(256, 512)
- self.cbam4 = CBAM(512, reduction_ratio=reduction_ratio)
- factor = 2 if self.bilinear else 1
- self.down4 = Down(512, 1024 // factor)
- self.cbam5 = CBAM(1024 // factor, reduction_ratio=reduction_ratio)
- self.up1 = Up(1024, 512 // factor, self.bilinear)
- self.up2 = Up(512, 256 // factor, self.bilinear)
- self.up3 = Up(256, 128 // factor, self.bilinear)
- self.up4 = Up(128, 64, self.bilinear)
-
- self.outc = OutConv(64, self.n_classes)
-
- def forward(self, x):
- b, c, t1, h, w = x.size()
- x = x.reshape(b, t1 * c, h, w)
- x1 = self.inc(x)
- x1Att = self.cbam1(x1)
- x2 = self.down1(x1)
- x2Att = self.cbam2(x2)
- x3 = self.down2(x2)
- x3Att = self.cbam3(x3)
- x4 = self.down3(x3)
- x4Att = self.cbam4(x4)
- x5 = self.down4(x4)
- x5Att = self.cbam5(x5)
- x = self.up1(x5Att, x4Att)
- x = self.up2(x, x3Att)
- x = self.up3(x, x2Att)
- x = self.up4(x, x1Att)
- logits = self.outc(x)
- logits = logits.reshape(b, 69, h, w)
- return logits
-
-
- # 判断t是否是元组,如果是,直接返回t;如果不是,则将t复制为元组(t, t)再返回
- # 用来处理当给出的图像尺寸或块尺寸是int类型(如224)时,直接返回为同值元组(如(224, 224))
- def pair(t):
- return t if isinstance(t, tuple) else (t, t)
-
-
- # PreNorm
- class PreNorm(nn.Module):
- def __init__(self, dim, fn):
- super().__init__()
- self.norm = nn.LayerNorm(dim)
- self.fn = fn
- def forward(self, x, **kwargs):
- return self.fn(self.norm(x), **kwargs)
-
-
-
- # Feedforward
-
- class FeedForward(nn.Module):
- def __init__(self, dim, hidden_dim, dropout=0.):
- super().__init__()
- self.net = nn.Sequential(
- nn.Linear(dim, hidden_dim),
- nn.GELU(),
- nn.Dropout(dropout),
- nn.Linear(hidden_dim, dim),
- nn.Dropout(dropout)
- )
- def forward(self, x):
- return self.net(x)
-
-
- #attention
-
- class Attention(nn.Module):
- def __init__(self, dim, heads=8, dim_head=64, dropout=0.):
- super().__init__()
- inner_dim = dim_head * heads
- project_out = not (heads == 1 and dim_head == dim)
-
- self.heads = heads
- self.scale = dim_head ** -0.5
-
- self.attend = nn.Softmax(dim=-1)
- self.to_qkv = nn.Linear(dim, inner_dim * 3, bias=False)
-
- self.to_out = nn.Sequential(
- nn.Linear(inner_dim, dim),
- nn.Dropout(dropout),
- ) if project_out else nn.Identity()
-
- def forward(self, x):
- b, n, _, h = *x.shape, self.heads
- qkv = self.to_qkv(x).chunk(3, dim=-1) # (b, n(65), dim*3) ---> 3 * (b, n, dim)
- q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=h), qkv) # q, k, v (b, h, n, dim_head(64))
-
- dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale
-
- attn = self.attend(dots)
-
- out = einsum('b h i j, b h j d -> b h i d', attn, v)
- out = rearrange(out, 'b h n d -> b n (h d)')
- return self.to_out(out)
-
-
- # Transformet
-
- class Transformer(nn.Module):
- def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout=0.):
- super().__init__()
- self.layers = nn.ModuleList([])
- for _ in range(depth):
- self.layers.append(nn.ModuleList([
- PreNorm(dim, Attention(dim, heads=heads, dim_head=dim_head, dropout=dropout)),
- PreNorm(dim, FeedForward(dim, mlp_dim, dropout=dropout))
- ]))
-
- def forward(self, x):
- for attn, ff in self.layers:
- x = attn(x) + x
- x = ff(x) + x
- return x
-
-
- #VIT
- class ViT(nn.Module):
- def __init__(self, *, image_size, patch_size, num_classes, dim, depth, heads, mlp_dim, pool='cls', channels=3, dim_head=64, dropout=0., emb_dropout=0.):
- super().__init__()
- image_height, image_width = pair(image_size) # 8 8
- patch_height, patch_width = pair(patch_size) # 32 32
-
- assert image_height % patch_height ==0 and image_width % patch_width == 0
-
- num_patches = (image_height // patch_height) * (image_width // patch_width)
-
- patch_dim = channels * patch_height * patch_width
- assert pool in {'cls', 'mean'}
-
- # self.to_patch_embedding = nn.Sequential(
- # Rearrange('b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1=patch_height, p2=patch_width),
- # nn.Linear(patch_dim, dim)
- # )
-
- self.pos_embedding = nn.Parameter(torch.randn(1, num_patches+1, dim))
- self.cls_token = nn.Parameter(torch.randn(1, 1, dim)) # nn.Parameter()定义可学习参数
- self.dropout = nn.Dropout(emb_dropout)
-
- self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim, dropout)
-
- self.pool = pool
- self.to_latent = nn.Identity()
-
- self.mlp_head = nn.Sequential(
- nn.LayerNorm(dim),
- nn.Linear(dim, num_classes)
- )
- self.lst = nn.LayerNorm(dim)
-
- self.unet = SmaAt_UNet()
-
- def forward(self, x):
- x = self.unet(x)
- # print(x.shape)
- b, c, h, w = x.size()
- x = x.reshape(b, c, h*w)
- # print(x.shape)
- # x = self.to_patch_embedding(img) # b c (h p1) (w p2) -> b (h w) (p1 p2 c) -> b (h w) dim ##(16,64,3072) ---> (16,64,1024)
- b, n, _ = x.shape # b表示batchSize, n表示每个块的空间分辨率, _表示一个块内有多少个值 #batch=16 n=64 _=1024
-
- cls_tokens = repeat(self.cls_token, '() n d -> b n d', b=b) # self.cls_token: (1, 1, dim) -> cls_tokens: (batchSize, 1, dim)
- # print('cls_tokens.shape:{}'.format(cls_tokens.shape)) #([16, 1, 1024])
- x = torch.cat((cls_tokens, x), dim=1) # 将cls_token拼接到patch token中去 (b, 65, dim)
- # print('x1.shape:{}'.format(x.shape)) #torch.Size([16, 65, 1024])
- x += self.pos_embedding[:, :(n+1)] # 加位置嵌入(直接加) (b, 65, dim)
- # print('x2.shape:{}'.format(x.shape)) #torch.Size([16, 65, 1024])
- x = self.dropout(x)
- # print('x3.shape:{}'.format(x.shape)) #torch.Size([16, 65, 1024])
-
- x = self.transformer(x) # (b, 65, dim)
- # print('x4.shape:{}'.format(x.shape)) #torch.Size([16, 65, 1024])
-
- # x = x.mean(dim=1) if self.pool == 'mean' else x[:, 0] # (b, dim)
- # print('x.shape:{}'.format(x.shape)) #torch.Size([16, 65, 1024])
-
- x = self.to_latent(x) # Identity (b, dim)
- # print(x.shape)
- # x = self.lst(x)
- # print(x)
- # return self.mlp_head(x) # (b, num_classes)
- return x
-
-
-
-
-
-
- def setup_seed(seed):
- torch.manual_seed(seed)
- torch.cuda.manual_seed_all(seed)
- np.random.seed(seed)
- random.seed(seed)
- torch.backends.cudnn.deterministic = True
-
- for ii in range(1,6):
- for epoch111 in range(100,600,100):
- # 设置随机数种子
- setup_seed(ii)
-
- data = np.load('/dataset/7day_for_Nday_data_openi_09_17_atlantic_area_last.npz')
- print(data.files) # ['hycom_temp', 'slfh', 'sshf', 'ssr', 'str', 'mld', 'analysis_temp', 'u', 'v', 'T_d', 'u_d', 'v_d', 'xx', 'yy']
-
- hycom_temp = data['hycom_temp'][:] # (3281, 7, 7, 41, 201)
- slfh = data['slfh'][:] # (3281, 7, 41, 201)
- sshf = data['sshf'][:] # (3281, 7, 41, 201)
- ssr = data['ssr'][:] # (3281, 7, 41, 201)
- str = data['str'][:] # (3281, 7, 41, 201)
- mld = data['mld'][:] # (3281, 7, 41, 201)
- analysis_temp = data['analysis_temp'][:] # (3281, 7, 7, 41, 201)
- u = data['u'][:] # (3281, 7, 7, 41, 201)
- v = data['v'][:] # (3281, 7, 7, 41, 201)
- T_d = data['T_d'][:] # (3281, 7, 41, 201)
- u_d = data['u_d'][:] # (3281, 7, 41, 201)
- v_d = data['v_d'][:] # (3281, 7, 41, 201)
- xx = data['xx'][:] # (3281, 7, 41, 201)
- yy = data['yy'][:] # (3281, 7, 41, 201)
-
-
- analysis_temp = analysis_temp.transpose(0, 2, 1, 3, 4)
-
- print(analysis_temp.shape) #(3275, 7, 10, 41, 81)
-
- print(hycom_temp.shape) # (3281, 7, 7, 41, 81)
-
-
- train_size = 1952 # 前60%
- valid_size = 2624 # 中间20% 作为验证 剩下的20%的作为测试
-
- Q_net = (slfh + sshf + ssr + str)/86400
-
- # print(Q_net)
- hycom_temp = hycom_temp.transpose(0, 2, 1, 3, 4)
- hycom_temp = hycom_temp.reshape(-1,7,7,41,81)
- hycom_temp = torch.Tensor(hycom_temp)
- hycom_temp_train = hycom_temp[0:train_size,:,:,:,:]
- hycom_temp_valid = hycom_temp[train_size:valid_size,:,:,:,:]
- hycom_temp_test = hycom_temp[valid_size:,:,:,:,:]
-
-
- # slfh = slfh.reshape(-1,1,1,7,41,201)
- # slfh = torch.Tensor(slfh)
- # slfh_train = slfh[0:train_size,:,:,:,:]
- # slfh_valid = slfh[train_size:valid_size,:,:,:,:]
- # slfh_test = slfh[valid_size:,:,:,:,:]
- #
- # sshf = sshf.reshape(-1,1,1,7,41,201)
- # sshf = torch.Tensor(sshf)
- # sshf_train = sshf[0:train_size,:,:,:,:]
- # sshf_valid = sshf[train_size:valid_size,:,:,:,:]
- # sshf_test = sshf[valid_size:,:,:,:,:]
- #
- #
- # ssr = ssr.reshape(-1,1,1,7,41,201)
- # ssr = torch.Tensor(ssr)
- # ssr_train = ssr[0:train_size,:,:,:,:]
- # ssr_valid = ssr[train_size:valid_size,:,:,:,:]
- # ssr_test = ssr[valid_size:,:,:,:,:]
- #
- #
- # str = str.reshape(-1,1,1,7,41,201)
- # str = torch.Tensor(str)
- # str_train = str[0:train_size,:,:,:,:]
- # str_valid = str[train_size:valid_size,:,:,:,:]
- # str_test = str[valid_size:,:,:,:,:]
-
- Q_net = Q_net.reshape(-1,1,7,41,81)
- Q_net = torch.Tensor(Q_net)
- Q_net_train = Q_net[0:train_size,:,:,:,:]
- Q_net_valid = Q_net[train_size:valid_size,:,:,:,:]
- Q_net_test = Q_net[valid_size:,:,:,:,:]
-
-
- mld = mld.reshape(-1,1,7,41,81)
- mld = torch.Tensor(mld)
- mld_train = mld[0:train_size,:,:,:,:]
- mld_valid = mld[train_size:valid_size,:,:,:,:]
- mld_test = mld[valid_size:,:,:,:,:]
-
-
- analysis_temp = analysis_temp.reshape(-1,7,10,41,81)
- analysis_temp = torch.Tensor(analysis_temp)
- # analysis_temp_train = analysis_temp[0:train_size,:,:,:,:]
- # analysis_temp_valid = analysis_temp[train_size:valid_size,:,:,:,:]
- # analysis_temp_test = analysis_temp[valid_size:,:,:,:,:]
-
- u = u.transpose(0, 2, 1, 3, 4)
- u = u.reshape(-1,7,7,41,81)
- u = torch.Tensor(u)
- u_train = u[0:train_size,:,:,:,:]
- u_valid = u[train_size:valid_size,:,:,:,:]
- u_test = u[valid_size:,:,:,:,:]
-
- v = v.transpose(0, 2, 1, 3, 4)
- v = v.reshape(-1,7,7,41,81)
- v = torch.Tensor(v)
- v_train = v[0:train_size,:,:,:,:]
- v_valid = v[train_size:valid_size,:,:,:,:]
- v_test = v[valid_size:,:,:,:,:]
-
-
- T_d = T_d.reshape(-1,1,7,41,81)
- T_d = torch.Tensor(T_d)
- T_d_train = T_d[0:train_size,:,:,:,:]
- T_d_valid = T_d[train_size:valid_size,:,:,:,:]
- T_d_test = T_d[valid_size:,:,:,:,:]
-
-
- u_d = u_d.reshape(-1,1,7,41,81)
- u_d = torch.Tensor(u_d)
- u_d_train = u_d[0:train_size,:,:,:,:]
- u_d_valid = u_d[train_size:valid_size,:,:,:,:]
- u_d_test = u_d[valid_size:,:,:,:,:]
-
-
- v_d = v_d.reshape(-1,1,7,41,81)
- v_d = torch.Tensor(v_d)
- v_d_train = v_d[0:train_size,:,:,:,:]
- v_d_valid = v_d[train_size:valid_size,:,:,:,:]
- v_d_test = v_d[valid_size:,:,:,:,:]
-
-
- xx = xx.reshape(-1,1,7,41,81)
- xx = torch.Tensor(xx)
- xx_train = xx[0:train_size,:,:,:,:]
- xx_valid = xx[train_size:valid_size,:,:,:,:]
- xx_test = xx[valid_size:,:,:,:,:]
-
-
- yy = yy.reshape(-1,1,7,41,81)
- yy = torch.Tensor(yy)
- yy_train = yy[0:train_size,:,:,:,:]
- yy_valid = yy[train_size:valid_size,:,:,:,:]
- yy_test = yy[valid_size:,:,:,:,:]
-
- print('hycom_temp_train.shape:{}'.format(hycom_temp_train.shape))
- print('Q_net_train.shape:{}'.format(Q_net_train.shape))
- print('mld_train.shape:{}'.format(mld_train.shape))
- print('u_train.shape:{}'.format(u_train.shape))
- print('v_train.shape:{}'.format(v_train.shape))
- print('T_d_train.shape:{}'.format(T_d_train.shape))
- print('v_d_train.shape:{}'.format(v_d_train.shape))
-
-
- train_data = torch.cat((hycom_temp_train, Q_net_train, mld_train, u_train, v_train, T_d_train, v_d_train), dim=1) # train_data.shape:torch.Size([1952, 25, 7, 41, 81]) # 数据 第一维时间 第二维深度和变量 第三维7dat seq 4 5lat lon
-
-
- print('train_data.shape:{}'.format(train_data.shape))
-
- valid_data = torch.cat((hycom_temp_valid, Q_net_valid, mld_valid, u_valid, v_valid, T_d_valid, v_d_valid), dim=1) # valid_data.shape:torch.Size([672, 7, 32, 41, 81])
- print('valid_data.shape:{}'.format(valid_data.shape)) #([326, 3, 12, 15])
-
- test_data = torch.cat((hycom_temp_test, Q_net_test, mld_test, u_test, v_test, T_d_test, v_d_test), dim=1) #
- print('test_data.shape:{}'.format(test_data.shape)) #([326, 3, 12, 15])
-
- train_label = analysis_temp[7:train_size + 7,:,:,:] # train_label.shape:torch.Size([1952, 7, 7, 41, 81])
-
- valid_label = analysis_temp[train_size + 7:valid_size + 7,:,:,:] # valid_label.shape:torch.Size([672, 7, 7, 41, 81])
-
-
- test_label = analysis_temp[valid_size + 7: 3646,:,:,:] # test_label.shape:torch.Size([650, 7, 7, 41, 81])
-
-
- print('test_label.shape:{}'.format(test_label.shape)) # test_label.shape:(63, 1, 6, 27)
-
- train_label = torch.Tensor(train_label)
- valid_label = torch.Tensor(valid_label)
- test_label = torch.Tensor(test_label)
- print('train_label.shape:{}'.format(train_label.shape)) #train_label.shape:torch.Size([224, 1, 6, 27])
- test_label11 = test_label
- valid_label11 = valid_label
-
- #构建数据管道
- class MyDataset(Dataset):
- def __init__(self, data, label):
- self.data = torch.Tensor(data)
- self.label = torch.Tensor(label)
-
- def __len__(self):
- return len(self.label)
-
- def __getitem__(self, idx):
- return self.data[idx], self.label[idx]
-
-
- batch_size1 = 32
- batch_size2 = 32
- batch_size3 = 3000
- # trainset = MyDataset(input_data, sst)
- # trainset = MyDataset(train_data,train_label)
- # trainloader = DataLoader(trainset, batch_size=batch_size, shuffle=True)
- # batch_size = 1826
- trainset = MyDataset(train_data, train_label)
- trainloader = DataLoader(trainset, batch_size=batch_size1, shuffle=True, drop_last=False,pin_memory=True, num_workers=0)
-
- validset = MyDataset(valid_data, valid_label)
- validloader = DataLoader(validset, batch_size=batch_size2, shuffle=True, drop_last=False,pin_memory=True, num_workers=0)
-
- testset = MyDataset(test_data, test_label)
- testloader = DataLoader(testset, batch_size=batch_size3, shuffle=False, drop_last=False,pin_memory=True, num_workers=0)
-
- # print('Qnet_train.shape:{}'.format(sst1_train.shape))
-
- model_weights1 = '/vit_unet_camb_wm_epo{}_lr1e-3_lay3_e{}_black_ahead_1_day_model_weights.pth'.format(epoch111,ii)
-
- print('-----------------------train_black_att_3dcnn_convlstm--------------------------')
- # 输入特征数
- input_dim = 1
- # 隐藏层节点数
- hidden_dim = (64,64,64)
- # 卷积核大小
- kernel_size = (3, 3)
-
- model = ViT(
- image_size = 288,
- patch_size = 32,
- num_classes = 1000,
- dim = 3321,
- depth = 8, #1
- heads = 8, #2
- mlp_dim = 2048,
- dropout = 0.1,
- emb_dropout = 0.1
- )
- # model = model.cuda()
- # out = model(a) #torch.Size([1826, 12, 15])
- # print(out.shape)
- criterion = nn.MSELoss()
- # 定义优化器
- optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
-
- epochs = 1000
- train_losses, valid_losses = [], []
- # best_loss = 2
- best_score = float('inf')
- best_score1 = float('inf')
- preds = np.zeros((1952,1,6,27))
- # preds = np.expand_dims(preds, axis=1)#preds.reshape(100,1,12,15)
- # preds = np.zeros((18000,1))
- print(preds.shape)
- sores = []
- def rmse(y_true, y_preds):
- return np.sqrt(mean_squared_error(y_pred = y_preds, y_true = y_true))
-
- for epoch in range(epochs):
- print('Epoch: {}/{}'.format(epoch + 1, epochs))
- # print(var_y)
- #模型训练
- model.train()
- losses = 0
- for data, label in tqdm(trainloader):
- data = data
- label = label
- label = label.reshape(-1,1,6,27)
- data = data.reshape(-1, 3, 7, 6, 27)
- optimizer.zero_grad()
- # print('data.shape:{}'.format(data.shape)) #torch.Size([160, 5, 1, 12, 15])
- out = model(data)
- print('out.shape:{}'.format(out.shape)) #out.shape:torch.Size([32, 2, 12, 15])
- print('label.shape:{}'.format(label.shape)) # out.shape:torch.Size([32, 2, 12, 15])
- loss = criterion(out, label)
- # losses +=loss.cpu().detach().numpy
- losses += loss
- # 反向传播
- loss.backward()
- optimizer.step()
- train_loss = losses / len(trainloader)
- train_losses.append(train_loss)
- print('Training Loss: {:.3f}'.format(train_loss))
-
- # # 模型验证
- # model.eval()
- losses = 0
-
- with torch.no_grad():
- for i, data in tqdm(enumerate(validloader)):
- # print('i:{}'.format(i))
- data, labels = data
- data = data
- label = label
- optimizer.zero_grad()
- data = data.reshape(-1, 7, 7, 6, 27)
- pred = model(data)
-
- # print('pred_shape:{}'.format(pred.shape)) #torch.Size([2, 1, 12, 15])
- loss = criterion(pred, label)
- losses += loss
- pred = pred.reshape(-1,1,6,27)
- preds[i * batch_size2:(i + 1) * batch_size2] = pred.cpu()
- # print(preds.shape)
- # preds[i * batch_size2:(i + 1) * batch_size2] = np.array(tmp)
- valid_loss = losses / len(validloader)
- valid_losses.append(valid_loss)
- print('Validation Loss: {:.3f}'.format(valid_loss))
-
- valid_label1 = valid_label.reshape(-1,1)
- preds1 = preds.reshape(-1,1)
- # print('valid_label1.shape:{}'.format(valid_label1.shape))#(18000,1)
- # print('preds.shape:{}0'.format(preds.shape)) #(360,1)
- s = rmse(valid_label1,preds1)
- sores.append(s)
- print('Score: {:.3f}'.format(s))
- # 保存最佳模型权重
- # if s < best_score: #求s的最小值 ---》最大值反过来 inf符号也要反过来
- # best_score = s
- # checkpoint = {'best_score': s,
- # 'state_dict': model.state_dict()}
- # torch.save(checkpoint, model_weights) # if valid_loss < best_loss:
- # best_loss = valid_loss
- # torch.save(model.state_dict(), './model_epo300_5e-4__hidden64_lay3e2_14day.pt')
- if valid_loss < best_score1: # 求s的最小值 ---》最大值反过来 inf符号也要反过来
- best_score1 = valid_loss
- checkpoint = {'best_score': valid_loss,
- 'state_dict': model.state_dict()}
- torch.save(checkpoint, model_weights1) # if valid_loss < best_loss:
- best_loss = valid_loss
- torch.save(model.state_dict(),
- '/vit_unet_camb_ds_wm_model_epo{}_lr1e-3_layer3_ahead_10_day_e{}.pt'.format(epoch111,ii))
-
- print(sores)
- print('best_score1:{}'.format(best_score1))
- print('s:{}'.format(s))
- print(valid_losses)
-
- print('valid_losses.:{}'.format(valid_losses))
|