Skip to content
Snippets Groups Projects
Commit a7f7fa3a authored by Yandi's avatar Yandi
Browse files

[Submitting]

parent aaf309df
No related branches found
No related tags found
1 merge request!1Master into main
...@@ -28,6 +28,7 @@ LinearRegression: ...@@ -28,6 +28,7 @@ LinearRegression:
# Bias in {True, False} # Bias in {True, False}
Bias: True Bias: True
#Name of directory containing logs
LogDir: ./logs/
...@@ -19,11 +19,19 @@ import datetime ...@@ -19,11 +19,19 @@ import datetime
# External imports # External imports
import tqdm import tqdm
import torch import torch
import torch.nn as nn
# Local imports # Local imports
import bindataset as dataset import bindataset as dataset
import dataloader
def create_submission(model, transform): def dummy_model(X):
# X is a (B, T, N) tensor
# As a dummy model, say, we average all the environmental measures
# Divided by a magic number
return X[:, :, 4:].mean(dim=2) / 26 # This is (B, T)
def create_submission(model, transform, device):
step_days = 10 step_days = 10
batch_size = 1024 batch_size = 1024
# We make chunks of num_days consecutive samples; As our dummy predictor # We make chunks of num_days consecutive samples; As our dummy predictor
...@@ -33,9 +41,6 @@ def create_submission(model, transform): ...@@ -33,9 +41,6 @@ def create_submission(model, transform):
num_days = 365 num_days = 365
num_workers = 7 num_workers = 7
use_cuda = torch.cuda.is_available()
device = torch.device("cuda") if use_cuda else torch.device("cpu")
# Build the dataloaders # Build the dataloaders
logging.info("Building the dataloader") logging.info("Building the dataloader")
...@@ -63,11 +68,10 @@ def create_submission(model, transform): ...@@ -63,11 +68,10 @@ def create_submission(model, transform):
# days of the same location then followed by consecutive days of the # days of the same location then followed by consecutive days of the
# next location and so on # next location and so on
chunk_size = batch_size * num_days chunk_size = batch_size * num_days
with torch.no_grad(): with torch.no_grad():
for X in tqdm.tqdm(test_loader): for X in tqdm.tqdm(test_loader):
X.to(device) X = X.to(device)
print(X.shape)
############################################# #############################################
# This is where you inject your knowledge # This is where you inject your knowledge
# About your model # About your model
...@@ -137,4 +141,34 @@ def create_submission(model, transform): ...@@ -137,4 +141,34 @@ def create_submission(model, transform):
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s") logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(message)s")
test() use_cuda = torch.cuda.is_available()
\ No newline at end of file if use_cuda :
device = torch.device('cuda')
else :
device = toch.device('cpu')
model_path = "logs/LinearRegression_5/best_model.pt"
model = nn.Sequential(
nn.Linear(14,8,False),
nn.ReLU(),
nn.Linear(8, 35, True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(),
nn.Linear(35,1, True),
nn.ReLU()
)
model = model.to(device)
model.load_state_dict(torch.load(model_path))
#create_submission(model, dataloader.transform_remove_space_time(), device)
create_submission(model, None, device)
\ No newline at end of file
...@@ -200,7 +200,7 @@ if __name__ == "__main__": ...@@ -200,7 +200,7 @@ if __name__ == "__main__":
num_days = num_days, num_days = num_days,
batch_size = batch_size, batch_size = batch_size,
num_workers = num_workers, num_workers = num_workers,
pin_memory = False, pin_memory = True,
valid_ratio = valid_ratio, valid_ratio = valid_ratio,
overwrite_index=True, overwrite_index=True,
max_num_samples=max_num_samples, max_num_samples=max_num_samples,
...@@ -241,6 +241,8 @@ if __name__ == "__main__": ...@@ -241,6 +241,8 @@ if __name__ == "__main__":
print(check_max(tensor, index)) print(check_max(tensor, index))
print("="*30) print("="*30)
print(X.shape)
check_info(X,0) #latitude check_info(X,0) #latitude
check_info(X,1) #longitude check_info(X,1) #longitude
check_info(X,2) #depth check_info(X,2) #depth
......
#Internal imports
import dataloader import dataloader
import model import model
import test import test
from train import train from train import train
import yaml
import losses import losses
import optimizers import optimizers
import create_submission
import utils
#External imports
import yaml
import torch import torch
import logging import logging
import torch.optim import torch.optim
import torch.nn as nn import torch.nn as nn
import create_submission import os
def optimizer(cfg, model): def optimizer(cfg, network):
result = {"Adam" : torch.optim.Adam(model.parameters())} result = {"Adam" : torch.optim.Adam(network.parameters())}
return result[cfg["Optimizer"]] return result[cfg["Optimizer"]]
if __name__ == "__main__": if __name__ == "__main__":
...@@ -47,39 +52,57 @@ if __name__ == "__main__": ...@@ -47,39 +52,57 @@ if __name__ == "__main__":
else : else :
device = toch.device('cpu') device = toch.device('cpu')
#model = model.build_model(cfg, 18) #network = network.build_network(cfg, 18)
model = nn.Sequential( network = nn.Sequential(
nn.Linear(14,8,False), nn.Linear(14,8,False),
nn.ReLU(), nn.ReLU(),
nn.Linear(8, 8, True), nn.Linear(8, 35, True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(),
nn.Linear(35,35,True),
nn.ReLU(), nn.ReLU(),
nn.Linear(8,35,True), nn.Linear(35,35,True),
nn.ReLU(), nn.ReLU(),
nn.Linear(35,1, True) nn.Linear(35,1, True),
nn.ReLU()
) )
model = model.to(device)
for param in list(model.parameters()): def init_xavier(module):
if type(module)==nn.Linear:
nn.init.xavier_uniform_(module.weight)
network = network.to(device)
"""
for param in list(network.parameters()):
param = 1 param = 1
"""
f_loss = losses.RMSLELoss() f_loss = losses.RMSLELoss()
optimizer = optimizer(cfg, model) optimizer = optimizer(cfg, network)
#optimizer = torch.optim.Adam((model.parameters()), lr = 10000)
logdir = utils.create_unique_logpath(cfg["LogDir"], cfg["Model"]["Name"])
network_checkpoint = model.ModelCheckpoint(logdir + "/best_model.pt", network)
for t in range(cfg["Training"]["Epochs"]): for t in range(cfg["Training"]["Epochs"]):
torch.autograd.set_detect_anomaly(True) torch.autograd.set_detect_anomaly(True)
print("Epoch {}".format(t)) print("Epoch {}".format(t))
train(model, train_loader, f_loss, optimizer, device) train(network, train_loader, f_loss, optimizer, device)
#print(list(model.parameters())[0].grad) #print(list(network.parameters())[0].grad)
val_loss = test.test(model, valid_loader, f_loss, device) val_loss = test.test(network, valid_loader, f_loss, device)
network_checkpoint.update(val_loss)
print(" Validation : Loss : {:.4f}".format(val_loss)) print(" Validation : Loss : {:.4f}".format(val_loss))
create_submission.create_submission(model, None)
create_submission.create_submission(network, None)
""" """
logdir = generate_unique_logpath(top_logdir, "linear") logdir = generate_unique_logpath(top_logdir, "linear")
print("Logging to {}".format(logdir)) print("Logging to {}".format(logdir))
......
...@@ -20,6 +20,17 @@ class LinearRegression(nn.Module): ...@@ -20,6 +20,17 @@ class LinearRegression(nn.Module):
def build_model(cfg, input_size): def build_model(cfg, input_size):
return eval(f"{cfg['Model']['Name']}(cfg, input_size)") return eval(f"{cfg['Model']['Name']}(cfg, input_size)")
class ModelCheckpoint:
def __init__(self, filepath, model):
self.min_loss = None
self.filepath = filepath
self.model = model
def update(self, loss):
if (self.min_loss is None) or (loss < self.min_loss):
print("Saving a better model")
torch.save(self.model.state_dict(), self.filepath)
self.min_loss = loss
if __name__== "__main__": if __name__== "__main__":
import yaml import yaml
......
Id,Predicted
...@@ -9,3 +9,9 @@ def generate_unique_logpath(logdir, raw_run_name): ...@@ -9,3 +9,9 @@ def generate_unique_logpath(logdir, raw_run_name):
return log_path return log_path
i = i + 1 i = i + 1
def create_unique_logpath(top_logdir, raw_run_name):
if not os.path.exists(top_logdir):
os.mkdir(top_logdir)
logdir = generate_unique_logpath(top_logdir, raw_run_name)
os.mkdir(logdir)
return logdir
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment