Commit 5afdcbe6 authored by Marco Cristoforetti's avatar Marco Cristoforetti
Browse files

script

parent c94a8df4
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -11,6 +11,11 @@ import torch.utils.data as utils_data
import torch.nn.functional as F
import datetime
from sklearn.metrics import confusion_matrix, matthews_corrcoef
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
np.set_printoptions(suppress=True, precision=3)
torch.manual_seed(21894)
np.random.seed(21894)
......@@ -150,35 +155,29 @@ np.random.shuffle(ixs_valid_test)
ixs_valid = ixs_valid_test[::2]
ixs_test = ixs_valid_test[1::2]
dst_min = data_out[:last_train].min(axis=1).values.flatten()
ixs_tr1 = np.where((mm_scaler_out.inverse_transform(data_out_scaled[:last_train].clone()).min(axis=1)[0]).numpy()<-20)[0]
ixs_tr2 = np.where((mm_scaler_out.inverse_transform(data_out_scaled[:last_train].clone()).min(axis=1)[0]).numpy()>=-20)[0]
bins = [dst_min.min() - 10] + list(np.arange(-300, dst_min.max() + 10, 10))
h, b = np.histogram(dst_min, bins=bins)
if len(np.argwhere(h == 0)) > 0:
bins = np.delete(bins, np.argwhere(h == 0)[0] + 1)
h, b = np.histogram(dst_min, bins=bins)
w = h.max()/h
def fix_weight(dst_v):
pos = np.argwhere(np.abs(b - dst_v) == np.abs((b - dst_v)).min())[0,0]
if dst_v - b[pos] < 0:
pos = pos-1
# return w[pos]/h.max()
return np.sqrt(w[pos]/h.max())
BATCH_SIZE=256
fix_weight_v = np.vectorize(fix_weight)
weights = fix_weight_v(dst_min)
np.random.shuffle(ixs_tr2)
ixs_tr2a = ixs_tr2[:10000]
ixs_tr = list(ixs_tr1) + list(ixs_tr2a)
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, num_samples= len(dst_min))
aa = data_out_c[ixs_tr]
weights_c = torch.tensor([len(aa[aa==0])/len(aa[aa==0]), len(aa[aa==0])/len(aa[aa==1]), len(aa[aa==0])/len(aa[aa==2]), len(aa[aa==0])/len(aa[aa==3])]).to(device)#.sqrt()
weights = data_out_c[ixs_tr].clone().max(axis=1)[0]
wix = []
for wi in range(4):
wix.append(torch.where(weights == wi * 1.))
for wi in range(4):
weights[wix[wi]] = weights_c[wi].to(float)
BATCH_SIZE=256
dataset_tr = Dataset(data_in_scaled[:last_train], data_out_scaled[:last_train], data_out_c[:last_train], weights)
dataset_tr = Dataset(data_in_scaled[ixs_tr], data_out_scaled[ixs_tr], data_out_c[ixs_tr], weights)
# data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=False, sampler = sampler)
data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=True)
class DSTnet(nn.Module):
def __init__(self, nvars, nhidden_i, nhidden_o, n_out_i, before, after):
super().__init__()
......@@ -199,9 +198,9 @@ class DSTnet(nn.Module):
self.linear_o_3 = nn.Linear(self.nhidden_o, self.nhidden_o // 2)
self.linear_o_4_c = nn.Linear(self.nhidden_o // 2, self.after * 4)
self.linear_o_4_r = nn.Linear(4, 16)
self.linear_o_4b_r = nn.Linear(4, 4)
self.linear_o_5_r = nn.Linear(16, 1)
self.linear_o_4_r = nn.Linear(self.nhidden_o // 2, self.after)
# self.linear_o_4b_r = nn.Linear(8, 8)
# self.linear_o_5_r = nn.Linear(8, 1)
def init_hidden(self, batch_size):
......@@ -225,22 +224,15 @@ class DSTnet(nn.Module):
x = F.relu(self.linear_o_3(x))
x = F.dropout(x, 0.2, training=self.training)
x1 = self.linear_o_4_c(x)
x2 = F.relu(self.linear_o_4_r(x1.view(-1, 4)))
# x2 = F.relu(self.linear_o_4b_r(x2))
x2 = self.linear_o_5_r(x2)
x2 = x2.reshape(x0.size(0), self.after)
x1 = x1.reshape(x0.size(0) * self.after, 4)
x2 = self.linear_o_4_r(x)
return x2, x1
aa = data_out_c[:last_train]
weights_c = torch.tensor([len(aa[aa==0])/len(aa[aa==0]), len(aa[aa==1])/len(aa[aa==1]), len(aa[aa==0])/len(aa[aa==2]), len(aa[aa==0])/len(aa[aa==3])]).to(device).sqrt()
loss_f = nn.L1Loss()
loss_mse = nn.MSELoss(reduction='none')
loss_fc= nn.CrossEntropyLoss()
# loss_fc= nn.CrossEntropyLoss(weight = weights_c)
# loss_fc= nn.CrossEntropyLoss()
loss_fc= nn.CrossEntropyLoss(weight = weights_c)
nhidden_i = 2
nhidden_o = 96
......@@ -253,7 +245,7 @@ print(dst_net)
num_epochs = 2000
lr = 1e-4
optimizer = torch.optim.Adam(dst_net.parameters(), lr=lr)#, weight_decay=1e-5)
optimizer = torch.optim.Adam(dst_net.parameters(), lr=lr, weight_decay=1e-5)
history_tr = np.zeros((num_epochs, 3))
history_valid = np.zeros((num_epochs, 3))
......@@ -276,8 +268,7 @@ for epoch in range(num_epochs):
loss_r = loss_f(out_r, y_r)
loss_c = loss_fc(out_c, y_c)
# loss = (loss_r * w).mean() + 10 * loss_c
loss = (loss_r).mean() + loss_c
loss = (loss_r * w).mean() + loss_c
loss.backward()
optimizer.step()
......@@ -305,32 +296,80 @@ for epoch in range(num_epochs):
epoch_time = time.time() - start_time
if (epoch % 10 == 0):
print('Epoch %d time = %.2f, tr_rmse = %0.5f, val_rmse = %0.5f, ts_rmse = %0.5f, tr_c = %.5f, val_c = %.5f, ts_c = %.5f' %
(epoch, epoch_time, loss_tr, loss_val, loss_ts, loss_c_tr, loss_c_val, loss_c_ts))
print('Epoch %d time = %.2f, tr_rmse = %0.5f, valid_rmse = %.5f, ts_rmse = %.5f, loss_c_tr = %0.5f, loss_c_val = %.5f, loss_c_ts = %.5f' %
(epoch, epoch_time, loss_tr, loss_val, loss_ts, loss_c_tr, loss_c_val, loss_c_ts))
if (epoch % 10 == 0):
out_r, out_c = dst_net(data_in_scaled[:last_train].to(device).float())
out_cc = F.softmax(out_c, dim=1)
out_cc = out_cc.detach().cpu().numpy()
tp = 11
print(confusion_matrix(data_out_c[:last_train][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp])/(confusion_matrix(data_out_c[:last_train][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp]).sum(axis=1)[:, None]))
out_r, out_c = dst_net(data_in_scaled[last_train:].to(device).float())
out_cc = F.softmax(out_c, dim=1)
out_cc = out_cc.detach().cpu().numpy()
tp = 11
print(confusion_matrix(data_out_c[last_train:][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp])/(confusion_matrix(data_out_c[last_train:][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp]).sum(axis=1)[:, None]))
dst_levels = [-20,-50,-100]
torch.save(dst_net.state_dict(), os.path.join('/home/mcristofo/DST/models','dst_regr_class_nsc.pth'))
truth = data_out[last_train:].cpu().detach().numpy().copy()
out = mm_scaler_out.inverse_transform(out_r.cpu().clone()).detach().numpy()
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_tr_regr_class_nsc.txt'), history_tr)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_valid_regr_class_nsc.txt'), history_valid)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_ts_regr_class_nsc.txt'), history_ts)
for ll in range(12):
print(ll, np.sqrt(mean_squared_error(truth[:,ll], out[:,ll])))
truth[np.where(truth >= dst_levels[0])] = 0
truth[np.where((truth < dst_levels[0]) & (truth >= dst_levels[1]))] = 1
truth[np.where((truth < dst_levels[1]) & (truth >= dst_levels[2]))] = 2
truth[np.where((truth < dst_levels[2]))] = 3
out[np.where(out >= dst_levels[0])] = 0
out[np.where((out < dst_levels[0]) & (out >= dst_levels[1]))] = 1
out[np.where((out < dst_levels[1]) & (out >= dst_levels[2]))] = 2
out[np.where((out < dst_levels[2]))] = 3
print(confusion_matrix(truth[:,11], out[:,11])/confusion_matrix(truth[:,11], out[:,11]).sum(axis=1)[:, None])
np.random.shuffle(ixs_tr2)
ixs_tr2a = ixs_tr2[:10000]
ixs_tr = list(ixs_tr1) + list(ixs_tr2a)
aa = data_out_c[ixs_tr]
weights_c = torch.tensor([len(aa[aa==0])/len(aa[aa==0]), len(aa[aa==0])/len(aa[aa==1]), len(aa[aa==0])/len(aa[aa==2]), len(aa[aa==0])/len(aa[aa==3])]).to(device)#.sqrt()
weights = data_out_c[ixs_tr].clone().max(axis=1)[0]
wix = []
for wi in range(4):
wix.append(torch.where(weights == wi * 1.))
for wi in range(4):
weights[wix[wi]] = weights_c[wi].to(float)
dataset_tr = Dataset(data_in_scaled[ixs_tr], data_out_scaled[ixs_tr], data_out_c[ixs_tr], weights)
# data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=False, sampler = sampler)
data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=True)
torch.save(dst_net.state_dict(), os.path.join('/home/mcristofo/DST/models','dst_regr_class.pth'))
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_tr_regr_class.txt'), history_tr)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_valid_regr_class.txt'), history_valid)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_ts_regr_class.txt'), history_ts)
dst_net.eval()
out_r, out_c = dst_net(data_in_scaled[last_train:].to(device).float())
out_cc = F.softmax(out_c, dim=1)
out_cc = out_cc.detach().cpu().numpy()
from sklearn.metrics import confusion_matrix, matthews_corrcoef
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
np.set_printoptions(suppress=True, precision=3)
tp = 11
out_cc = F.softmax(out_c, dim=1)
out_cc = out_cc.detach().cpu().numpy()
print(confusion_matrix(data_out_c[last_train:][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp])/(confusion_matrix(data_out_c[last_train:][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp]).sum(axis=1)[:, None]))
dst_levels = [-20,-50,-100]
truth = data_out[last_train:].cpu().detach().numpy().copy()
out = mm_scaler_out.inverse_transform(out_r.cpu().clone()).detach().numpy()
......
......@@ -12,6 +12,9 @@ import torch.nn.functional as F
import datetime
from sklearn.metrics import confusion_matrix, matthews_corrcoef
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
np.set_printoptions(suppress=True, precision=3)
torch.manual_seed(21894)
np.random.seed(21894)
......@@ -154,39 +157,27 @@ ixs_test = ixs_valid_test[1::2]
ixs_tr1 = np.where((mm_scaler_out.inverse_transform(data_out_scaled[:last_train].clone()).min(axis=1)[0]).numpy()<-20)[0]
ixs_tr2 = np.where((mm_scaler_out.inverse_transform(data_out_scaled[:last_train].clone()).min(axis=1)[0]).numpy()>=-20)[0]
np.random.shuffle(ixs_tr2)
ixs_tr2 = ixs_tr2[:10000]
ixs_tr = list(ixs_tr1) + list(ixs_tr2)
dst_min = data_out[ixs_tr].min(axis=1).values.flatten()
bins = [dst_min.min() - 10] + list(np.arange(-300, dst_min.max() + 10, 10))
h, b = np.histogram(dst_min, bins=bins)
if len(np.argwhere(h == 0)) > 0:
bins = np.delete(bins, np.argwhere(h == 0)[0] + 1)
h, b = np.histogram(dst_min, bins=bins)
w = h.max()/h
def fix_weight(dst_v):
pos = np.argwhere(np.abs(b - dst_v) == np.abs((b - dst_v)).min())[0,0]
if dst_v - b[pos] < 0:
pos = pos-1
# return w[pos]/h.max()
return np.sqrt(w[pos]/h.max())
fix_weight_v = np.vectorize(fix_weight)
weights = fix_weight_v(dst_min)
BATCH_SIZE=256
sampler = torch.utils.data.sampler.WeightedRandomSampler(weights, num_samples= len(dst_min))
np.random.shuffle(ixs_tr2)
ixs_tr2a = ixs_tr2[:10000]
ixs_tr = list(ixs_tr1) + list(ixs_tr2a)
aa = data_out_c[ixs_tr]
weights_c = torch.tensor([len(aa[aa==0])/len(aa[aa==0]), len(aa[aa==0])/len(aa[aa==1]), len(aa[aa==0])/len(aa[aa==2]), len(aa[aa==0])/len(aa[aa==3])]).to(device)#.sqrt()
BATCH_SIZE=256
weights = data_out_c[ixs_tr].clone().max(axis=1)[0]
wix = []
for wi in range(4):
wix.append(torch.where(weights == wi * 1.))
for wi in range(4):
weights[wix[wi]] = weights_c[wi].to(float)
dataset_tr = Dataset(data_in_scaled[ixs_tr], data_out_scaled[ixs_tr], data_out_c[ixs_tr], weights)
# data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=False, sampler = sampler)
data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=True)
class DSTnet(nn.Module):
def __init__(self, nvars, nhidden_i, nhidden_o, n_out_i, before, after):
super().__init__()
......@@ -207,9 +198,9 @@ class DSTnet(nn.Module):
self.linear_o_3 = nn.Linear(self.nhidden_o, self.nhidden_o // 2)
self.linear_o_4_c = nn.Linear(self.nhidden_o // 2, self.after * 4)
self.linear_o_4_r = nn.Linear(4, 16)
self.linear_o_4b_r = nn.Linear(16, 16)
self.linear_o_5_r = nn.Linear(16, 1)
self.linear_o_4_r = nn.Linear(self.nhidden_o // 2, self.after)
# self.linear_o_4b_r = nn.Linear(8, 8)
# self.linear_o_5_r = nn.Linear(8, 1)
def init_hidden(self, batch_size):
......@@ -233,44 +224,33 @@ class DSTnet(nn.Module):
x = F.relu(self.linear_o_3(x))
x = F.dropout(x, 0.2, training=self.training)
x1 = self.linear_o_4_c(x)
x2 = F.relu(self.linear_o_4_r(x1.view(-1, 4)))
x2 = F.relu(self.linear_o_4b_r(x2))
x2 = self.linear_o_5_r(x2)
x2 = x2.reshape(x0.size(0), self.after)
x1 = x1.reshape(x0.size(0) * self.after, 4)
x2 = self.linear_o_4_r(x)
return x2, x1
aa = data_out_c[ixs_tr]
weights_c = torch.tensor([len(aa[aa==1])/len(aa[aa==0]), len(aa[aa==1])/len(aa[aa==1]), len(aa[aa==0])/len(aa[aa==2]), len(aa[aa==0])/len(aa[aa==3])]).to(device)#.sqrt()
loss_f = nn.L1Loss()
loss_mse = nn.MSELoss(reduction='none')
#loss_fc= nn.CrossEntropyLoss()
# loss_fc= nn.CrossEntropyLoss()
loss_fc= nn.CrossEntropyLoss(weight = weights_c)
nhidden_i = 2
#nhidden_o = 96
#n_out_i = 8
nhidden_o = 96
n_out_i = 12
n_out_i = 8
before = BEFORE
nvars = data_in_scaled.shape[-1]
dst_net = DSTnet(nvars, nhidden_i, nhidden_o, n_out_i, before, AFTER).to(device)
print(dst_net)
num_epochs = 10000
lr = 1e-5
num_epochs = 2000
lr = 1e-4
optimizer = torch.optim.Adam(dst_net.parameters(), lr=lr, weight_decay=1e-5)
history_tr = np.zeros((num_epochs, 3))
history_valid = np.zeros((num_epochs, 3))
history_ts = np.zeros((num_epochs, 3))
np.set_printoptions(suppress=True, precision=3)
for epoch in range(num_epochs):
start_time = time.time()
......@@ -288,8 +268,7 @@ for epoch in range(num_epochs):
loss_r = loss_f(out_r, y_r)
loss_c = loss_fc(out_c, y_c)
# loss = (loss_r * w).mean() + 10 * loss_c
loss = (loss_r).mean() + loss_c
loss = (loss_r * w).mean() + loss_c
loss.backward()
optimizer.step()
......@@ -316,24 +295,69 @@ for epoch in range(num_epochs):
history_ts[epoch] = [loss_ts, loss_mae_ts, loss_c_ts]
epoch_time = time.time() - start_time
if (epoch % 100 == 0):
print('Epoch %d time = %.2f, tr_rmse = %0.5f, val_rmse = %0.5f, ts_rmse = %0.5f, tr_c = %.5f, val_c = %.5f, ts_c = %.5f' %
(epoch, epoch_time, loss_tr, loss_val, loss_ts, loss_c_tr, loss_c_val, loss_c_ts))
if (epoch % 10 == 0):
print('Epoch %d time = %.2f, tr_rmse = %0.5f, valid_rmse = %.5f, ts_rmse = %.5f, loss_c_tr = %0.5f, loss_c_val = %.5f, loss_c_ts = %.5f' %
(epoch, epoch_time, loss_tr, loss_val, loss_ts, loss_c_tr, loss_c_val, loss_c_ts))
if (epoch % 100 == 0):
out_r, out_c = dst_net(data_in_scaled[last_train:].to(device).float())
if (epoch % 10 == 0):
out_r, out_c = dst_net(data_in_scaled[:last_train].to(device).float())
out_cc = F.softmax(out_c, dim=1)
out_cc = out_cc.detach().cpu().numpy()
tp = 11
print(confusion_matrix(data_out_c[:last_train][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp])/(confusion_matrix(data_out_c[:last_train][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp]).sum(axis=1)[:, None]))
out_r, out_c = dst_net(data_in_scaled[last_train:].to(device).float())
out_cc = F.softmax(out_c, dim=1)
out_cc = out_cc.detach().cpu().numpy()
tp = 11
print(confusion_matrix(data_out_c[last_train:][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp])/(confusion_matrix(data_out_c[last_train:][:,tp].cpu().detach().numpy(), out_cc.argmax(axis=1).reshape(-1,12)[:,tp]).sum(axis=1)[:, None]))
dst_levels = [-20,-50,-100]
truth = data_out[last_train:].cpu().detach().numpy().copy()
out = mm_scaler_out.inverse_transform(out_r.cpu().clone()).detach().numpy()
for ll in range(12):
print(ll, np.sqrt(mean_squared_error(truth[:,ll], out[:,ll])))
truth[np.where(truth >= dst_levels[0])] = 0
truth[np.where((truth < dst_levels[0]) & (truth >= dst_levels[1]))] = 1
truth[np.where((truth < dst_levels[1]) & (truth >= dst_levels[2]))] = 2
truth[np.where((truth < dst_levels[2]))] = 3
out[np.where(out >= dst_levels[0])] = 0
out[np.where((out < dst_levels[0]) & (out >= dst_levels[1]))] = 1
out[np.where((out < dst_levels[1]) & (out >= dst_levels[2]))] = 2
out[np.where((out < dst_levels[2]))] = 3
print(confusion_matrix(truth[:,11], out[:,11])/confusion_matrix(truth[:,11], out[:,11]).sum(axis=1)[:, None])
np.random.shuffle(ixs_tr2)
ixs_tr2a = ixs_tr2[:10000]
ixs_tr = list(ixs_tr1) + list(ixs_tr2a)
aa = data_out_c[ixs_tr]
weights_c = torch.tensor([len(aa[aa==0])/len(aa[aa==0]), len(aa[aa==0])/len(aa[aa==1]), len(aa[aa==0])/len(aa[aa==2]), len(aa[aa==0])/len(aa[aa==3])]).to(device)#.sqrt()
weights = data_out_c[ixs_tr].clone().max(axis=1)[0]
wix = []
for wi in range(4):
wix.append(torch.where(weights == wi * 1.))
for wi in range(4):
weights[wix[wi]] = weights_c[wi].to(float)
dataset_tr = Dataset(data_in_scaled[ixs_tr], data_out_scaled[ixs_tr], data_out_c[ixs_tr], weights)
# data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=False, sampler = sampler)
data_loader_tr = utils_data.DataLoader(dataset_tr, batch_size=BATCH_SIZE, num_workers = 4, shuffle=True)
torch.save(dst_net.state_dict(), os.path.join('/home/mcristofo/DST/models','dst_regr_class_nsc.pth'))
torch.save(dst_net.state_dict(), os.path.join('/home/mcristofo/DST/models','dst_regr_class.pth'))
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_tr_regr_class_nsc.txt'), history_tr)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_valid_regr_class_nsc.txt'), history_valid)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_ts_regr_class_nsc.txt'), history_ts)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_tr_regr_class.txt'), history_tr)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_valid_regr_class.txt'), history_valid)
np.savetxt(os.path.join('/home/mcristofo/DST/hist','history_ts_regr_class.txt'), history_ts)
dst_net.eval()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment