Skip to content

Instantly share code, notes, and snippets.

@SchattenGenie
Created July 20, 2020 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SchattenGenie/f73604e7702154c7f435247690670597 to your computer and use it in GitHub Desktop.
Save SchattenGenie/f73604e7702154c7f435247690670597 to your computer and use it in GitHub Desktop.
import os
from sys import argv
import sys
import numpy as np
import yaml
import torch
from torch import nn
import torch.nn.functional as F
import pandas
from shutil import copyfile, copy2
device = torch.device('cpu')
baseline_neurons = 50000
label_class_correspondence = {'Electron': 0, 'Ghost': 1, 'Kaon': 2, 'Muon': 3, 'Pion': 4, 'Proton': 5}
class_label_correspondence = {0: 'Electron', 1: 'Ghost', 2: 'Kaon', 3: 'Muon', 4: 'Pion', 5: 'Proton'}
def get_score(logloss):
k = -1. / 0.9
b = 1.2 / 0.9
score = b + k * logloss
score = max(score, 0)
score = min(score, 1)
return score
from sparse_model import SparseNet
from sparse_vd import LinearSVDO
def calc_score(input_file="test.csv.gz", solution_file="test_solution.csv.gz", model_weights="model_weights.pt"):
test = pandas.read_csv(input_file)
features = list(set(test.columns) - {'Label', 'Class', 'ID'})
features = sorted(features)
model = SparseNet(input_dim=len(features), device=device).to(device)
model.load_state_dict(torch.load(model_weights, map_location=torch.device('cpu')))
model.eval()
X = torch.tensor(test[features].values).float()
preds = model(X)
solution_pandas = pandas.read_csv(solution_file, index_col=None, compression='gzip')
arr = solution_pandas.values
columns = list(solution_pandas.columns)
labels = list(map(
lambda x: label_class_correspondence[x],
list(map(lambda x: columns[x], arr.argmax(1)))
))
labels = torch.tensor(labels).long()
nllloss = torch.nn.NLLLoss()
score = nllloss(preds, labels)
effecive_number_parameters = 0
total_number_parameters = 0
for module in model.children():
if isinstance(module, LinearSVDO):
effecive_number_parameters += module.count_parameters()[0]
total_number_parameters += module.count_parameters()[1]
else:
for param in module.parameters():
effecive_number_parameters += param.numel()
total_number_parameters += param.numel()
final_score = get_score(score.detach().numpy()) * np.log(1 + baseline_neurons / effecive_number_parameters)
return final_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment