Skip to content

Commit

Permalink
flak8e correcting src/
Browse files Browse the repository at this point in the history
  • Loading branch information
beckynevin committed Jul 16, 2024
1 parent 09159b8 commit 763182a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 121 deletions.
10 changes: 5 additions & 5 deletions src/analyze/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def load_checkpoint(
file_name += ".pt"
elif model_name[0:2] == "DE":
file_name = (
str(path)
+ f"{model_name}_{prescription}_{inject_type}_{data_dim}"
str(path) +
f"{model_name}_{prescription}_{inject_type}_{data_dim}"
f"_noise_{noise}_beta_{BETA}_nmodel_{nmodel}_epoch_{epoch}.pt"
)
#import os
#print('cwd', os.getcwd())
#print(os.listdir(path))
# import os
# print('cwd', os.getcwd())
# print(os.listdir(path))
checkpoint = torch.load(file_name, map_location=device)
return checkpoint

Expand Down
2 changes: 1 addition & 1 deletion src/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def simulate_data_2d(self,
loc=0, scale=sigma))
elif inject_type == "feature":
noisy_image = image + np.random.normal(
loc=0, scale=sigma, size=(image_size, image_size))
loc=0, scale=sigma, size=(image_size, image_size))
image_array[i, :, :] = noisy_image
total_brightness.append(np.sum(image))
# we'll need the noisy image summed if we want to
Expand Down
52 changes: 25 additions & 27 deletions src/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def forward(self, x):
beta = nn.functional.softplus(x[:, 3])
return torch.stack((gamma, nu, alpha, beta), dim=1)


class ConvLayers(nn.Module):
def __init__(self):
super(ConvLayers, self).__init__()
Expand All @@ -83,7 +84,8 @@ def __init__(self):
def forward(self, x):
# print('input shape', x.shape)
if x.dim() == 3: # Check if the input is of shape (batchsize, 32, 32)
x = x.unsqueeze(1) # Add channel dimension, becomes (batchsize, 1, 32, 32)
# Add channel dimension, becomes (batchsize, 1, 32, 32)
x = x.unsqueeze(1)
# print('shape after potential unsqeeze', x.shape)
x = nn.functional.relu(self.conv1(x))
# print('shape after conv1', x.shape)
Expand All @@ -104,10 +106,7 @@ def forward(self, x):
return x


def model_setup_DER(loss_type,
DEVICE,
n_hidden=64,
data_type="0D"):
def model_setup_DER(loss_type, DEVICE, n_hidden=64, data_type="0D"):
# initialize the model from scratch
if loss_type == "SDER":
Layer = SDERLayer
Expand All @@ -120,18 +119,21 @@ def model_setup_DER(loss_type,
if data_type == "2D":
# Define the convolutional layers
conv_layers = ConvLayers()

# Initialize the rest of the model
model = torch.nn.Sequential(
conv_layers,
Model(n_hidden=n_hidden, n_input=405, n_output=4), # Adjust input size according to the flattened output size
Layer()
Model(
n_hidden=n_hidden, n_input=405, n_output=4
), # Adjust input size according to the flattened output size
Layer(),
)
elif data_type == "0D":
# from https://github.com/pasteurlabs/unreasonable_effective_der
# /blob/main/x3_indepth.ipynb
model = torch.nn.Sequential(Model(
n_hidden=n_hidden, n_input=3, n_output=4), Layer())
model = torch.nn.Sequential(
Model(n_hidden=n_hidden, n_input=3, n_output=4), Layer()
)
model = model.to(DEVICE)
return model, lossFn

Expand All @@ -148,20 +150,16 @@ def forward(self, x):
return torch.stack((mu, var), dim=1)


def model_setup_DE(loss_type,
DEVICE,
n_hidden=64,
data_type="0D"):
def model_setup_DE(loss_type, DEVICE, n_hidden=64, data_type="0D"):
# initialize the model from scratch
if loss_type == "no_var_loss":
# model = de_no_var().to(DEVICE)
lossFn = torch.nn.MSELoss(reduction="mean")
if loss_type == "var_loss":
# model = de_var().to(DEVICE)
Layer = MuVarLayer
lossFn = torch.nn.GaussianNLLLoss(full=False,
eps=1e-06,
reduction="mean")
lossFn = torch.nn.GaussianNLLLoss(
full=False, eps=1e-06, reduction="mean")
if loss_type == "bnll_loss":
# model = de_var().to(DEVICE)
Layer = MuVarLayer
Expand All @@ -172,14 +170,17 @@ def model_setup_DE(loss_type,
# Initialize the rest of the model
model = torch.nn.Sequential(
conv_layers,
Model(n_hidden=n_hidden, n_input=405, n_output=2), # Adjust input size according to the flattened output size
Layer()
Model(
n_hidden=n_hidden, n_input=405, n_output=2
), # Adjust input size according to the flattened output size
Layer(),
)
elif data_type == "0D":
# from https://github.com/pasteurlabs/unreasonable_effective_der
# /blob/main/x3_indepth.ipynb
model = torch.nn.Sequential(Model(
n_hidden=n_hidden, n_input=3, n_output=2), Layer())
model = torch.nn.Sequential(
Model(n_hidden=n_hidden, n_input=3, n_output=2), Layer()
)
model = model.to(DEVICE)
return model, lossFn

Expand All @@ -189,10 +190,7 @@ def model_setup_DE(loss_type,


class Model(nn.Module):
def __init__(self,
n_output=4,
n_hidden=64,
n_input=3):
def __init__(self, n_output=4, n_hidden=64, n_input=3):
super().__init__()
self.model = nn.Sequential(
nn.Linear(n_input, n_hidden),
Expand Down Expand Up @@ -245,8 +243,8 @@ def loss_sder(y, y_pred, coeff):
)
u_ep = 1 / np.sqrt(nu.detach().numpy())

return torch.mean(torch.log(var) +
(1.0 + coeff * nu) * error**2 / var), u_al, u_ep
return torch.mean(torch.log(var) + (1.0 + coeff * nu) * error**2 / var), \
u_al, u_ep


# from martius lab
Expand Down
43 changes: 20 additions & 23 deletions src/scripts/DeepEvidentialRegression.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ def parse_args():

# data info
parser.add_argument(
"--data_path",
"-d", default=DefaultsDER["data"]["data_path"])
"--data_path", "-d", default=DefaultsDER["data"]["data_path"])
parser.add_argument(
"--data_dimension",
"-dd", default=DefaultsDER["data"]["data_dimension"]
"--data_dimension", "-dd",
default=DefaultsDER["data"]["data_dimension"]
)
parser.add_argument(
"--data_prescription",
"-dp", default=DefaultsDER["data"]["data_prescription"]
"--data_prescription", "-dp",
default=DefaultsDER["data"]["data_prescription"]
)
parser.add_argument(
"--data_injection",
"-di", default=DefaultsDER["data"]["data_injection"]
"--data_injection", "-di",
default=DefaultsDER["data"]["data_injection"]
)
parser.add_argument(
"--data_engine",
Expand Down Expand Up @@ -254,7 +253,7 @@ def parse_args():
"randomseed": args.randomseed,
"batchsize": args.batchsize,
"generatedata": args.generatedata,
"normalize": args.normalize
"normalize": args.normalize,
},
# "plots": {key: {} for key in args.plots},
# "metrics": {key: {} for key in args.metrics},
Expand All @@ -281,7 +280,7 @@ def parse_args():
injection = config.get_item("data", "data_injection", "DER")
if config.get_item("data", "generatedata", "DER", raise_exception=False):
# generate the df
print('generating the data')
print("generating the data")
data = DataPreparation()
if dim == "0D":
data.sample_params_from_prior(size_df)
Expand All @@ -298,17 +297,14 @@ def parse_args():
# Convert lists to tensors
df[key] = torch.tensor(value)
elif dim == "2D":
print('2D data')
data.sample_params_from_prior(size_df,
low=[1, 1, -1.5],
high=[10, 10, 1.5],
n_params=3,
seed=42)
print("2D data")
data.sample_params_from_prior(
size_df, low=[1, 1, -1.5], high=[10, 10, 1.5], n_params=3,
seed=42
)
model_inputs, model_outputs = data.simulate_data_2d(
size_df,
data.params,
image_size=32,
inject_type=injection)
size_df, data.params, image_size=32, inject_type=injection
)
else:
loader = MyDataLoader()
if dim == "0D":
Expand All @@ -332,18 +328,19 @@ def parse_args():
model_outputs = np.reshape(df["output"].numpy(), (len_df * len_x))
model_inputs = np.array([xs_array, ms_array, bs_array]).T
model_inputs, model_outputs = DataPreparation.normalize(
model_inputs, model_outputs, norm)
model_inputs, model_outputs, norm
)
x_train, x_val, y_train, y_val = DataPreparation.train_val_split(
model_inputs, model_outputs, val_proportion=val_prop, random_state=rs
)
'''
"""
import matplotlib.pyplot as plt
plt.clf()
plt.imshow(x_train[0,:,:])
plt.title(y_train[0])
plt.colorbar()
plt.show()
'''
"""
trainData = TensorDataset(torch.Tensor(x_train), torch.Tensor(y_train))
trainDataLoader = DataLoader(
trainData, batch_size=BATCH_SIZE, shuffle=True)
Expand Down
12 changes: 6 additions & 6 deletions src/scripts/ExperimentalExpectedSigmaTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def beta_type(value):
model_inputs, model_outputs, val_proportion=0.1,
random_state=41
)

chk = chk_module.load_checkpoint(
model,
prescription,
Expand All @@ -269,14 +269,14 @@ def beta_type(value):
COEFF=COEFF,
loss=loss_type,
)

# first, define the model at this epoch
DERmodel.load_state_dict(chk.get("model_state_dict"))
# checkpoint['model_state_dict'])
DERmodel.eval()
# now run on the x_test
y_pred = DERmodel(torch.Tensor(x_test)).detach().numpy()

print(x_test)
print(x_test[:, 1])
if typei == "predictive":
Expand All @@ -298,17 +298,17 @@ def beta_type(value):
plt.show()

plt.clf()
_, bins = np.histogram(sub, bins=50)#, range=[-20, 20])
_, bins = np.histogram(sub, bins=50) # , range=[-20, 20])
plt.hist(sub, bins=bins, alpha=0.5, label=label, color="#610345")
'''
"""
plt.hist(
np.sqrt(y_pred[:, 1]),
bins=bins,
alpha=0.5,
label="predicted sigma",
color="#9EB25D",
)
'''
"""

plt.axvline(x=np.mean(sub), color="black", ls="--")
plt.axvline(x=np.std(sub), color="black", ls="--")
Expand Down
Loading

0 comments on commit 763182a

Please sign in to comment.