text
stringlengths
1
93.6k
def check_folder(log_dir):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
return log_dir
def str2bool(x):
return x.lower() in ('true')
def pytorch_xavier_weight_factor(gain=0.02, uniform=False) :
if uniform :
factor = gain * gain
mode = 'FAN_AVG'
else :
factor = (gain * gain) / 1.3
mode = 'FAN_AVG'
return factor, mode, uniform
def pytorch_kaiming_weight_factor(a=0.0, activation_function='relu', uniform=False) :
if activation_function == 'relu' :
gain = np.sqrt(2.0)
elif activation_function == 'leaky_relu' :
gain = np.sqrt(2.0 / (1 + a ** 2))
elif activation_function =='tanh' :
gain = 5.0 / 3
else :
gain = 1.0
if uniform :
factor = gain * gain
mode = 'FAN_IN'
else :
factor = (gain * gain) / 1.3
mode = 'FAN_IN'
return factor, mode, uniform
# <FILESEP>
"""
Created on 24 July 2016
@author: Thomas Antony
"""
import copy
import inspect
import functools
import collections
__version__ = '0.0.5.3'
def validate_task(original_task):
"""
Validates task and adds default values for missing options using the
following steps.
1. If there is no input list specified or if it is None, the input spec is
assumed to be ['*'].
2. If there are not outputs specified, or if the output spec is None or an
empty list, the output spec is assumed to be ['*'].
3. If the input or output spec is not iterable, they are converted into
single element tuples. If they are any iterable, they are converted into
tuples.
4. The task['fn'] option must be callable.
5. If number of outputs is more than one, task['fn'] must be a generator
function.
6. Generator functions are not supported for output spec of '*'.
Returns new task with updated options
"""
task = original_task._asdict()
# Default values for inputs and outputs
if 'inputs' not in task or task['inputs'] is None:
task['inputs'] = ['*']
# Outputs list cannot be empty
if ('outputs' not in task or
task['outputs'] is None or
len(task['outputs']) == 0):
task['outputs'] = ['*']
# Convert to tuples (even for single values)
if not hasattr(task['inputs'], '__iter__') or isinstance(task['inputs'], str):
task['inputs'] = (task['inputs'],)
else:
task['inputs'] = tuple(task['inputs'])
if not hasattr(task['outputs'], '__iter__') or isinstance(task['outputs'], str):
task['outputs'] = (task['outputs'],)
else:
task['outputs'] = tuple(task['outputs'])
if not callable(task['fn']):