|
- import numpy as np
- import torch
- from torch import nn
- # from input_data import *
- from sklearn.preprocessing import MinMaxScaler
-
- # ConvLSTM Cell
- import random
-
- import numpy as np
- import torch
- from tqdm import tqdm
- from sklearn.metrics import mean_squared_error
- from torch import nn
- from torch.utils.data import Dataset, DataLoader
-
-
- mm = MinMaxScaler()
-
- #3dcnn 的输入(N, C, D, H ,W) ---> (N, C1, D1, H1, W1)
- #strid = 1 ---> kernel_size = input_dim + 1 - output_dim + 2*padding
-
- class ConvNet(nn.Module):
- def __init__(self,input_dim, hidden_dim, kernel_size1,padding1,kernel_size2,padding2, kernel_size3,padding3, kernel_size4,padding4):
- super(ConvNet, self).__init__()
- self.input_dim = input_dim
- self.hidden_dim = hidden_dim
- self.kernel_size1 = kernel_size1
- self.padding1 = padding1
-
- self.kernel_size2 = kernel_size2
- self.kernel_size3 = kernel_size3
- self.kernel_size4 = kernel_size4
- self.padding2 = padding2
- self.padding3 = padding3
- self.padding4 = padding4
- self.conv_dec = nn.Sequential(nn.Conv3d(in_channels=6,
- out_channels=1,
- kernel_size=kernel_size1,
- padding=self.padding1))
- # self.conv_dec1 = nn.Sequential(nn.Conv3d(in_channels=32,
- # out_channels=64,
- # kernel_size=kernel_size1,
- # padding=self.padding1))
- # self.conv_dec2 = nn.Sequential(nn.Conv3d(in_channels=64,
- # out_channels=1,
- # kernel_size=kernel_size1,
- # padding=self.padding1))
-
- self.conv1 = nn.Sequential(nn.Conv3d(in_channels=self.input_dim ,
- out_channels=16,
- kernel_size=kernel_size1,
- padding=self.padding1))
- self.relu = nn.ReLU()
- self.conv2 = nn.Sequential(nn.Conv3d(in_channels=16,
- out_channels=32,
- kernel_size=kernel_size2,
- padding=self.padding2))
- self.conv3 = nn.Sequential(nn.Conv3d(in_channels=32,
- out_channels=1,
- kernel_size=kernel_size3,
- padding=self.padding3,
- ))
- # self.conv4 = nn.Sequential(nn.Conv3d(in_channels=64,
- # out_channels=1,
- # kernel_size=kernel_size4,
- # padding=self.padding4,
- # ))
- # self.conv3 = nn.Sequential(nn.Conv3d(in_channels=32,
- # out_channels=64,
- # kernel_size=kernel_size3,
- # padding=self.padding3,
- # ))
- # self.conv4 = nn.Sequential(nn.Conv3d(in_channels=64,
- # out_channels=1,
- # kernel_size=kernel_size4,
- # padding=self.padding4,
- # ))
- # self.conv5 = nn.Sequential(nn.Conv3d(in_channels=128,
- # out_channels=1,
- # kernel_size=kernel_size4,
- # padding=self.padding4,
- # ))
- # self.fc = nn.Linear
-
- # 前馈网络过程
- def forward(self, x): #x 为(batch, channel, h, w)
-
- out = self.conv_dec(x)
- # print('out1.shape:{}'.format(out.shape))
- # out = self.conv_dec1(out)
- # print('out2.shape:{}'.format(out.shape))
- # out = self.conv_dec2(out)
- # print('out3.shape:{}'.format(out.shape))
- out = out.reshape(-1,10,1,40,200)
- out = self.conv1(out) #x 为(batch, channel, h, w)
- # out = self.relu(out)
- out = self.conv2(out)
- out = self.conv3(out)
- # out = self.conv4(out)
- # out = self.conv5(out)
- # out = self.relu(out)
- return out
-
-
-
- for seed in range(2023, 2024):
- for date_append in range(0, 15):
- 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
-
-
- # 设置随机数种子
- setup_seed(seed)
-
- # 需要 mld u v sss temp 降水 蒸发 混合层下的盐度
-
- data = np.load(r'/dataset/10day_for_14day_all_variables_deep8_pacific_10_19_SSS_learn.npz')
- print(data.files)
- # 2000-01-01 ---> 2019-12-31
- # surface_latent_heat_flux = data['surface_latent_heat_flux'][:] # (7305, 10, 40, 200)
- # surface_sensible_heat_flux = data['surface_sensible_heat_flux'][:] # (9851, 10, 40, 200)
- # surface_net_radiation = data['surface_net_radiation'][:] # (9851, 10, 40, 200)
- evaporation = data['evaporation'][:]
- total_precipitation = data['total_precipitation'][:]
- mld = data['mld'][:]
- # sst_surface = data['sst_surface'][:]
- sss_surface = data['sss_deep'][:]
- u_surface = data['u_deep'][:]
- v_surface = data['v_deep'][:]
- sss_surface_label = data['sss_deep_label'][:]
- # print(sst_surface)
-
- # print(sst_surface.shape)
- print(sss_surface.shape) #(3642, 10, 40, 200)
-
- print(sss_surface_label.shape) #(3638, 40, 200)
-
- dS_dt = data['dS_dt'][:]
- dS_dx = data['dS_dx'][:]
- dS_dy = data['dS_dy'][:]
-
-
-
- train_size = 2208
- valid_size = 2912 # 前20% 作为验证 剩下的20%的作为测试
- # surface_latent_heat_flux = surface_latent_heat_flux + surface_sensible_heat_flux + surface_net_radiation
-
- evaporation = evaporation.reshape(-1, 1, 10, 40, 200)
- evaporation = torch.Tensor(evaporation)
- evaporation_train = evaporation[0:train_size, :, :, :, :]
- evaporation_valid = evaporation[train_size:valid_size, :, :, :, :]
- # evaporation_test = evaporation[valid_size:,:,:,:,:]
-
- total_precipitation = total_precipitation.reshape(-1, 1, 10, 40, 200)
- total_precipitation = torch.Tensor(total_precipitation)
- total_precipitation_train = total_precipitation[0:train_size, :, :, :, :]
- total_precipitation_valid = total_precipitation[train_size:valid_size, :, :, :, :]
- # total_precipitation_test = total_precipitation[valid_size:,:,:,:,:]
-
- mld = mld.reshape(-1, 1, 10, 40, 200)
- mld = torch.Tensor(mld)
- mld_train = mld[0:train_size, :, :, :, :]
- mld_valid = mld[train_size:valid_size, :, :, :, :]
- # mld_test = mld[valid_size:,:,:,:,:]
-
- # sst_surface = sst_surface.reshape(-1, 1, 10, 40, 200)
- # sst_surface = torch.Tensor(sst_surface)
- # sst_surface_train = sst_surface[0:train_size, :, :, :, :]
- # sst_surface_valid = sst_surface[train_size:valid_size, :, :, :, :]
- # # sst_surface_test = sst_surface[valid_size:,:,:,:,:]
-
- sss_surface = sss_surface.reshape(-1, 1, 10, 40, 200)
- sss_surface = torch.Tensor(sss_surface)
- sss_surface_train = sss_surface[0:train_size, :, :, :, :]
- sss_surface_valid = sss_surface[train_size:valid_size, :, :, :, :]
- # sss_surface_test = sss_surface[valid_size:,:,:,:,:]
-
- u_surface = u_surface.reshape(-1, 1, 10, 40, 200)
- u_surface = torch.Tensor(u_surface)
- u_surface_train = u_surface[0:train_size, :, :, :, :]
- u_surface_valid = u_surface[train_size:valid_size, :, :, :, :]
- # u_surface_test = u_surface[valid_size:,:,:,:,:]
-
- v_surface = v_surface.reshape(-1, 1, 10, 40, 200)
- v_surface = torch.Tensor(v_surface)
- v_surface_train = v_surface[0:train_size, :, :, :, :]
- v_surface_valid = v_surface[train_size:valid_size, :, :, :, :]
- # # v_surface_test = v_surface[valid_size:,:,:,:,:]
-
- dS_dt = dS_dt.transpose(1,0,2,3)
- dS_dt = dS_dt.reshape(-1, 1, 10, 40, 200)
- dS_dt = torch.Tensor(dS_dt)
- dS_dt_train = dS_dt[0:train_size, :, :, :]
- dS_dt_valid = dS_dt[train_size:valid_size, :, :, :]
-
- dS_dx = dS_dx.transpose(1,2,0,3)
- dS_dx = dS_dx.reshape(-1, 1, 10, 40, 200)
- dS_dx = torch.Tensor(dS_dx)
- dS_dx_train = dS_dx[0:train_size, :, :, :]
- dS_dx_valid = dS_dx[train_size:valid_size, :, :, :]
-
- dS_dy = dS_dy.transpose(1,2,3,0)
- dS_dy = dS_dy.reshape(-1, 1, 10, 40, 200)
- dS_dy = torch.Tensor(dS_dy)
- dS_dy_train = dS_dy[0:train_size, :, :, :]
- dS_dy_valid = dS_dy[train_size:valid_size, :, :, :]
-
-
- train_data = torch.cat((evaporation_train, total_precipitation_train, sss_surface_train, mld_train, u_surface_train, v_surface_train,
- dS_dt_train, dS_dx_train, dS_dy_train
- ), dim=1) # train_data.shape:torch.Size([5920, 10, 16, 40, 200])
-
- valid_data = torch.cat((evaporation_valid, total_precipitation_valid, sss_surface_valid, mld_valid, u_surface_valid, v_surface_valid,
- dS_dt_valid, dS_dx_valid, dS_dy_valid
- ), dim=1)
-
-
- print(train_data.shape)
- print(valid_data.shape)
-
-
- sss_train_label = sss_surface_label[10 :train_size + 10 , date_append, :, :]
- sss_valid_label = sss_surface_label[train_size + 10 : valid_size + 10, date_append, :, :]
-
-
- sss_train_label = sss_train_label.reshape(-1, 1, 40, 200)
- sss_valid_label = sss_valid_label.reshape(-1, 1, 40, 200)
-
- train_label = sss_train_label
- valid_label = sss_valid_label
- print(train_label.shape)
-
-
- # 构建数据管道
- 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(train_data, train_label)
- trainloader = DataLoader(trainset, batch_size=batch_size1, shuffle=True, drop_last=False, pin_memory=True,
- num_workers=4)
-
- validset = MyDataset(valid_data, valid_label)
- validloader = DataLoader(validset, batch_size=batch_size2, shuffle=True, drop_last=False, pin_memory=True,
- num_workers=4)
-
- model_weights1 = '/model/epo200_lay3_lr0.001_e{}_CNN_3D_hidd16_{}day_model_weights_SSS_deep8.pth'.format(seed, date_append)
-
- model = ConvNet(input_dim=10, hidden_dim=1, kernel_size1=(3, 3, 3), padding1=(1, 1, 1), kernel_size2=(3, 3, 3),
- padding2=(1, 1, 1), kernel_size3=(3, 3, 3),padding3=(1, 1, 1), kernel_size4=(3, 3, 3),padding4=(1, 1, 1)).cuda()
-
- criterion = nn.MSELoss()
- # 定义优化器
- optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
-
- epochs = 200
- train_losses, valid_losses = [], []
- # best_loss = 2
- best_score = float('inf')
- best_score1 = float('inf')
-
- pred_val = np.zeros((704, 1, 40, 200))
-
- 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
- loss1 = 0
- for data, label in tqdm(trainloader):
- # data, label = data
- data = data.cuda()
- label = label.cuda()
- optimizer.zero_grad()
-
- data_train = data[:,:6,:,:,:]
- # data = data.permute(0,2,1,3,4)
- out = model(data_train)
-
- out = out.reshape(-1, 1, 40, 200)
- loss = criterion(out, label)
-
- losses += loss
-
- loss.backward()
- optimizer.step()
- train_loss = losses / len(trainloader)
- train_losses.append(train_loss)
-
- print('Training Loss: {:.10f}'.format((train_loss)))
-
- # model.eval()
- losses = 0
- with torch.no_grad():
- for i, data in tqdm(enumerate(validloader)):
- data, label = data
- data = data.cuda()
- label = label.cuda()
- optimizer.zero_grad()
- data_train = data[:,:6,:,:,:]
- # data = data.permute(0,2,1,3,4)
- out = model(data_train)
- out = out.reshape(-1, 1, 40, 200)
- loss = criterion(out, label)
-
- losses += float(loss)
-
- out1 = out.detach().cpu().numpy()
- pred_val[i * batch_size2:(i + 1) * batch_size2] = np.array(out1)
-
- valid_loss = losses / len(validloader)
- valid_losses.append(valid_loss)
-
- valid_label1 = valid_label.reshape(-1, 1)
- preds1 = pred_val.reshape(-1, 1)
-
- s = rmse(valid_label1, preds1)
- sores.append(s)
- print('Score: {:.3f}'.format(s))
-
- 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(),
- '/model/CNN_3D_lr0.001_model_200_layer3_hidd16_{}day_e{}_SSS_deep8.pt'.format(date_append, seed))
-
- print(sores)
- print(best_score)
- print(s)
|