Skip to content

Commit

Permalink
fixes from code review
Browse files Browse the repository at this point in the history
minor changes to syntax, commenting, formatting, etc
  • Loading branch information
dpaiton committed Sep 8, 2020
1 parent bbe7e83 commit 287c704
Show file tree
Hide file tree
Showing 34 changed files with 105 additions and 394 deletions.
4 changes: 1 addition & 3 deletions adversarial_analysis.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import os
import sys

ROOT_DIR = os.getcwd()
while 'DeepSparseCoding' in ROOT_DIR:
ROOT_DIR = os.path.dirname(ROOT_DIR)
ROOT_DIR = os.path.dirname(os.getcwd())
if ROOT_DIR not in sys.path: sys.path.append(ROOT_DIR)

import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_optimizer(self, optimizer_params, trainable_variables):
lr=optimizer_params.weight_lr,
weight_decay=optimizer_params.weight_decay)
else:
assert False, ('optimizer name must be "sgd" or "adam", not %s'%(optimizer_name))
assert False, (f'optimizer name must be "sgd" or "adam", not {optimizer_name}')
return optimizer

def setup_optimizer(self):
Expand Down
2 changes: 0 additions & 2 deletions models/mlp_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np

import torch
#import torch.nn.functional as F

from DeepSparseCoding.models.base_model import BaseModel
from DeepSparseCoding.modules.mlp_module import MlpModule
Expand All @@ -15,7 +14,6 @@ def setup(self, params, logger=None):
def get_total_loss(self, input_tuple):
input_tensor, input_label = input_tuple
pred = self.forward(input_tensor)
#return F.nll_loss(pred, input_label)
self.loss_fn = torch.nn.CrossEntropyLoss()
return self.loss_fn(pred, input_label)

Expand Down
4 changes: 2 additions & 2 deletions modules/activations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def activation_picker(activation_function):
return F.leaky_relu
if activation_function == 'lca_threshold':
return lca_threshold
assert False, ('Activation function ' + activation_function + ' is not supported.')
assert False, (f'Activation function {activation_function} is not supported.')

def lca_threshold(u_in, thresh_type, rectify, sparse_threshold):
u_zeros = torch.zeros_like(u_in)
Expand All @@ -38,5 +38,5 @@ def lca_threshold(u_in, thresh_type, rectify, sparse_threshold):
u_in,
u_zeros))
else:
assert False, ('Parameter thresh_type must be "soft" or "hard", not '+thresh_type)
assert False, (f'Parameter thresh_type must be "soft" or "hard", not {thresh_type}')
return a_out
1 change: 0 additions & 1 deletion modules/mlp_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def preprocess_data(self, input_tensor):
def forward(self, x):
for dropout, act_func, layer in zip(self.dropout, self.act_funcs, self.layers):
x = dropout(act_func(layer(x)))
#x = F.log_softmax(x, dim=1)
return x

def get_encodings(self, input_tensor):
Expand Down
55 changes: 55 additions & 0 deletions params/base_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@


class BaseParams(object):
"""
all models
batch_size [int] number of images in a training batch
data_dir [str] location of dataset folders
device [str] which device to run on
dtype [torch dtype] dtype for network variables
eps [float] small value to avoid division by zero
lib_root_dir [str] system location of this library directory
log_to_file [bool] if set, log to file, else log to stderr
model_name [str] name for model (can be anything)
model_type [str] type of model (must be among the list returned by utils/loaders.get_module_list())
num_epochs [int] how many epochs to use for training
num_pixels [int] total number of pixels in the input image
out_dir [str] base directory for all model outputs
optimizer [object] empty object with the following properties:
optimizer.name [str] which optimization algorithm to use
can be "sgd" (default) or "adam"
optimizer.lr_annealing_milestone_frac [list of floats] fraction of num_epochs to anneal learning rate
optimizer.lr_decay_rate [float] amount to anneal learning rate by
rand_seed [int] seed to be given to np.random.RandomState
rand_state [int] random state to be used for all random functions to allow for reproducible results
renormalize_weights [bool] if set, l2 normalize weights after each update
rescale_data_to_one [bool] if set, rescale input data to be between 0 and 1, per example
version [str] model version for output
shuffle_data [bool] if set, shuffle loader data before delivering batches
standardize_data [bool] if set, z-score data to have mean=0 and standard deviation=1 using numpy operators
train_logs_per_epoch [int or None] how often to send updates to the logfile
workspace_dir [str] system directory that is the parent to the primary repository directory
mlp
activation_functions [list of str] strings correspond to activation functions for layers.
len must equal the len of layer_types
strings must be one of those listed in modules/activations.activation_picker()
dropout_rate [list of floats] specifies dropout probability of a value being set to zero or None per layer
len must be equal to the len of layer_types
layer_types [list of str] weight connectivity type, either "conv" or "fc"
len must be equal to the len of layer_channels - 1
layer_channels [list of int] number of outputs per layer, including the input layer
lca
dt [float] discrete global time constant for neuron dynamics
lca update rule is multiplied by dt/tau
num_latent [int] number of lca latent units
num_steps [int] number of lca inference steps to take
rectify_a [bool] if set, rectify the layer 1 neuron activity
sparse_mult [float] multiplyer placed in front of the sparsity loss term
tau [float] LCA time constant
lca update rule (step_size) is multiplied by dt/tau
thresh_type [str] specifying LCA threshold function; can be "hard" or "soft"
lca; mlp
weight_decay [float] multiplier to use on top of weight decay loss term
weight_lr [float] learning rate for all weight updates
"""

def __init__(self):
self.set_params()
self.compute_helper_params()
Expand Down
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DeepSparseCoding
foolbox>=3.1.1
h5py>=2.8.0
imageio>=2.4.1
ipython>=7.1.1
Expand All @@ -11,7 +12,8 @@ scikit-image>=0.14.1
scikit-learn>=0.20.0
scipy>=1.1.0
seaborn>=0.9.0
tensorboard==2.2
tensorflow-gpu==2.1
tensorflow-gpu==1.15.2
tensorflow-estimator==1.15.1
tensorboard==1.15
tensorflow-probability==0.8.0
tensorflow-compression
tensorflow-probability
2 changes: 1 addition & 1 deletion tests/test_data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_standardize(self):
places=num_tolerance_decimals,
msg=err_msg)

def rescale_data_to_one(self):
def test_rescale_data_to_one(self):
num_tolerance_decimals = 7
unflat_shape = [8, 4, 4, 3]
flat_shape = [8, 4*4*3]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setUp(self):

def test_model_loading(self):
for model_type in self.model_list:
model_type = ''.join(model_type.split("_")[:-1]) # remove '_model' at the end
model_type = ''.join(model_type.split('_')[:-1]) # remove '_model' at the end
model = loaders.load_model(model_type)
params = loaders.load_params(self.test_params_file, key=model_type+'_params')
train_loader, val_loader, test_loader, data_params = datasets.load_dataset(params)
Expand All @@ -31,7 +31,7 @@ def test_model_loading(self):
### TODO - more basic test to compute gradients per model###
#def test_gradients(self):
# for model_type in self.model_list:
# model_type = ''.join(model_type.split("_")[:-1]) # remove '_model' at the end
# model_type = ''.join(model_type.split('_')[:-1]) # remove '_model' at the end
# model = loaders.load_model(model_type)

def test_lca_ensemble_gradients(self):
Expand Down
4 changes: 2 additions & 2 deletions tf1x/analyze_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import numpy as np
import tensorflow as tf

root_path = os.path.dirname(os.path.dirname(os.getcwd()))
if root_path not in sys.path: sys.path.append(root_path)
ROOT_DIR = os.path.dirname(os.path.dirname(os.getcwd()))
if ROOT_DIR not in sys.path: sys.path.append(ROOT_DIR)

from DeepSparseCoding.tf1x.utils.logger import Logger
import DeepSparseCoding.tf1x.utils.data_processing as dp
Expand Down
95 changes: 0 additions & 95 deletions tf1x/encode_image.py

This file was deleted.

Loading

0 comments on commit 287c704

Please sign in to comment.