-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from NCAR/ggantos
Running attention_net_validation_loss
- Loading branch information
Showing
22 changed files
with
2,542 additions
and
29,779 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
*.out | ||
*.o | ||
*.out.* | ||
batch**.sh | ||
*.o* | ||
.idea | ||
holodecml.egg-info/ | ||
data/ | ||
models/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
path_data: "/glade/p/cisl/aiml/ai4ess_hackathon/holodec/" | ||
path_save: "/glade/p/cisl/aiml/ggantos/holodec/optuna/test/" | ||
model_name: "attn" | ||
num_particles: "large" | ||
random_seed: 328942 | ||
output_cols: ["x", "y", "z", "d", "hid"] | ||
scaler_out: "MinMaxScaler" | ||
num_z_bins: False | ||
subset: 0.1 | ||
mass: False | ||
metric: "mae" | ||
noisy_sd: 0.1 | ||
attention_network: | ||
activation: "relu" | ||
output_num: 5 | ||
train: | ||
epochs: 100 | ||
batch_size: 64 | ||
verbose: 1 | ||
callbacks: | ||
EarlyStopping: | ||
monitor: "val_loss" | ||
patience: 3 | ||
ReduceLROnPlateau: | ||
monitor: "val_loss" | ||
factor: 0.2 | ||
patience: 1 | ||
min_lr: 0.0000001 | ||
mode: "auto" | ||
CSVLogger: | ||
filename: "/glade/p/cisl/aiml/ggantos/holodec/optuna/test/training.txt" | ||
separator: " " | ||
append: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
log: | ||
save_path: "/glade/p/cisl/aiml/ggantos/holodec/optuna/test/log.txt" | ||
|
||
slurm: | ||
jobs: 20 | ||
kernel: "ncar_pylib ncar_20200417" | ||
batch: | ||
account: "NAML0001" | ||
gres: "gpu:v100:1" | ||
mem: "256G" | ||
n: 8 | ||
t: "12:00:00" | ||
J: "hyper_opt" | ||
o: "hyper_opt.out" | ||
e: "hyper_opt.err" | ||
|
||
optuna: | ||
name: "holodec_optimization.db" | ||
reload: 0 | ||
objective: "/glade/work/ggantos/holodec-ml/scripts/ggantos/objective.py" | ||
direction: "minimize" | ||
metric: "val_loss" | ||
n_trials: 20 | ||
gpu: True | ||
save_path: "/glade/p/cisl/aiml/ggantos/holodec/optuna/test/" | ||
sampler: | ||
type: "TPESampler" | ||
parameters: | ||
attention_neurons: | ||
type: "int" | ||
settings: | ||
name: "attention_neurons" | ||
low: 50 | ||
high: 150 | ||
hidden_layers: | ||
type: "int" | ||
settings: | ||
name: "hidden_layers" | ||
low: 1 | ||
high: 4 | ||
hidden_neurons: | ||
type: "int" | ||
settings: | ||
name: "hidden_neurons" | ||
low: 50 | ||
high: 150 | ||
min_filters: | ||
type: "int" | ||
settings: | ||
name: "min_filters" | ||
low: 8 | ||
high: 24 | ||
learning_rate: | ||
type: "float" | ||
settings: | ||
name: "learning_rate" | ||
low: 0.00001 | ||
high: 0.01 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
path_data: "/glade/p/cisl/aiml/ai4ess_hackathon/holodec/ft_rad_bidis_z/" | ||
path_save: "/glade/p/cisl/aiml/ggantos/holodec/ft_rad_bidis_models/z/" | ||
model_name: "cnn" | ||
random_seed: 328942 | ||
input_variable: 'input_image' | ||
label_variable: 'histogram' | ||
metric: "mae" | ||
conv2d_network: | ||
filters: [4, 8, 16] | ||
kernel_sizes: [5, 5, 5] | ||
conv2d_activation: "relu" | ||
pool_sizes: [0, 0, 0] | ||
dense_sizes: [64, 32, 16] | ||
dense_activation: "elu" | ||
lr: 0.001 | ||
optimizer: "adam" | ||
loss: "categorical_crossentropy" | ||
batch_size: 256 | ||
metrics: ["TP","FP","TN","FN"] | ||
epochs: 20 | ||
verbose: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,91 @@ | ||
import logging | ||
import tensorflow as tf | ||
from typing import List, Dict | ||
import tensorflow.keras.backend as K | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class SymmetricCrossEntropy: | ||
|
||
def __init__(self, a: float = 1.0, b: float = 1.0) -> None: | ||
self.a = a | ||
self.b = b | ||
|
||
def __call__(self, *args, **kwargs) -> float: | ||
bce = tf.keras.losses.CategoricalCrossentropy() | ||
kld = tf.keras.losses.KLDivergence() | ||
return self.a * bce(*args, **kwargs) + self.b * kld(*args, **kwargs) | ||
|
||
|
||
def rmse(y_true, y_pred): | ||
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1)) | ||
|
||
|
||
def wmse(y_true, y_pred): | ||
return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1)) | ||
|
||
|
||
def R2(y_true, y_pred): | ||
""" Is actually 1 - R2 | ||
""" | ||
SS_res = K.sum(K.square(y_true - y_pred)) | ||
SS_res = K.sum(K.square(y_true - y_pred)) | ||
SS_tot = K.sum(K.square(y_true - K.mean(y_true))) | ||
return SS_res/(SS_tot + K.epsilon()) | ||
return SS_res / (SS_tot + K.epsilon()) | ||
|
||
|
||
def keras_mse(y_true, y_pred): | ||
return K.mean(K.square(y_pred - y_true)) | ||
|
||
|
||
def attention_net_loss(y_true, y_pred): | ||
def noisy_true_particle_loss(y_true, y_pred): | ||
# y_true and y_pred will have shape (batch_size x max_num_particles x 5) | ||
loss_real = tf.reduce_mean(tf.abs(y_true[y_true[:, :, -1] > 0] - y_pred[y_true[:, :, -1] > 0])) | ||
loss_bce = binary_crossentropy(y_true[:,:,-1], | ||
y_pred[:,:,-1]) | ||
loss_bce = tf.keras.losses.binary_crossentropy(tf.reshape(y_true[:, :, -1],[-1]), | ||
tf.reshape(y_pred[:, :, -1],[-1])) | ||
loss_total = loss_real + loss_bce | ||
return loss_total | ||
|
||
def random_particle_distance_loss(y_true, y_pred): | ||
loss_xy = tf.zeros((), dtype=tf.float32) | ||
loss_z = tf.zeros((), dtype=tf.float32) | ||
loss_d = tf.zeros((), dtype=tf.float32) | ||
|
||
for h in range(tf.shape(y_pred)[0]): | ||
y_pred_h = y_pred[h] | ||
print("y_pred_h.shape", y_pred_h.get_shape()) | ||
y_true_h = y_true[h] | ||
print("y_true_h.shape", y_true_h.shape) | ||
real_idx = tf.argmin(y_true_h[:, -1], axis=0) | ||
if real_idx == 0: | ||
real_idx = tf.cast(tf.shape(y_true_h)[0], dtype=tf.int64) | ||
print("real_idx.shape", real_idx.get_shape()) | ||
y_true_h = y_true_h[:real_idx] | ||
print("y_true_h.shape", y_true_h.get_shape()) | ||
|
||
dist_x = (y_pred_h[:, 0:1] - tf.transpose(y_true_h)[0:1, :]) ** 2 | ||
dist_y = (y_pred_h[:, 1:2] - tf.transpose(y_true_h)[1:2, :]) ** 2 | ||
dist_xy = dist_x + dist_y | ||
print(f"dist_xy.shape: {dist_xy.shape}") | ||
loss_xy_h = tf.math.reduce_sum(tf.math.reduce_min(dist_xy, axis=1)) | ||
loss_xy = loss_xy + loss_xy_h | ||
|
||
# determine index of true particle closest to each predicted particle | ||
max_idx = tf.cast(tf.math.argmin(dist_xy, axis=1), dtype=tf.int32) | ||
max_idx_2d = tf.stack((tf.range(tf.shape(dist_xy)[0]), max_idx), axis=-1) | ||
|
||
loss_z_h = (y_pred_h[:, 2:3] - tf.transpose(y_true_h)[2:3, :]) ** 2 | ||
loss_z_h = tf.math.reduce_sum(tf.gather_nd(loss_z_h, max_idx_2d)) | ||
loss_z = loss_z + loss_z_h | ||
|
||
loss_d_h = (y_pred_h[:, 3:4] - tf.transpose(y_true_h)[3:4, :]) ** 2 | ||
loss_d_h = tf.math.reduce_sum(tf.gather_nd(loss_d_h, max_idx_2d)) | ||
loss_d = loss_d + loss_d_h | ||
|
||
loss_xy = loss_xy/tf.cast(tf.shape(y_pred)[0], dtype=tf.float32) | ||
loss_z = loss_z/tf.cast(tf.shape(y_pred)[0], dtype=tf.float32) | ||
loss_d = loss_d/tf.cast(tf.shape(y_pred)[0], dtype=tf.float32) | ||
|
||
valid_error = loss_xy + loss_z + loss_d | ||
print(f"ERROR SHAPE: {valid_error.shape}") | ||
|
||
return valid_error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.