text:
from __future__ import print_function, division
import matplotlib
import logging
from sys import stdout
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!
from neuralnilm import (Net, RealApplianceSource,
                        BLSTMLayer, DimshuffleLayer,
                        BidirectionalRecurrentLayer)
from neuralnilm.source import standardise, discretize, fdiff, power_and_fdiff
from neuralnilm.experiment import run_experiment, init_experiment
from neuralnilm.net import TrainingError
from neuralnilm.objectives import scaled_cost

from lasagne.nonlinearities import sigmoid, rectify, tanh
from lasagne.objectives import crossentropy, mse
from lasagne.init import Uniform, Normal
from lasagne.layers import (LSTMLayer, DenseLayer, Conv1DLayer,
                            ReshapeLayer, FeaturePoolLayer, RecurrentLayer)
from lasagne.updates import nesterov_momentum
from functools import partial
import os
import __main__
from copy import deepcopy
from math import sqrt
import numpy as np
import theano.tensor as T

NAME = os.path.splitext(os.path.split(__main__.__file__)[1])[0]
PATH = "/homes/dk3810/workspace/python/neuralnilm/figures"
SAVE_PLOT_INTERVAL = 500
GRADIENT_STEPS = 100

source_dict = dict(
    filename='/data/dk3810/ukdale.h5',
    appliances=[
        ['fridge freezer', 'fridge', 'freezer'],
        'hair straighteners',
        'television',
        'dish washer',
        ['washer dryer', 'washing machine']
    ],
    max_appliance_powers=[300, 500, 200, 2500, 2400],
    on_power_thresholds=[5] * 5,
    max_input_power=5900,
    min_on_durations=[60, 60, 60, 1800, 1800],
    min_off_durations=[12, 12, 12, 1800, 600],
    window=("2013-06-01", "2014-07-01"),
    seq_length=1500,
    output_one_appliance=False,
    boolean_targets=False,
    train_buildings=[1],
    validation_buildings=[1],
    skip_probability=0.7,
    n_seq_per_batch=10,
    subsample_target=3,
    include_diff=False,
    clip_appliance_power=True,
    target_is_prediction=False,
    standardise_input=True,
    standardise_targets=True,
    input_padding=0,
    lag=0,
    input_stats={'mean': np.array([ 0.05526326], dtype=np.float32),
                 'std': np.array([ 0.12636775], dtype=np.float32)},
    target_stats={
        'mean': np.array([ 0.04066789,  0.01881946,
                           0.24639061,  0.17608672,  0.10273963],
                         dtype=np.float32),
        'std': np.array([ 0.11449792,  0.07338708,
                       0.26608968,  0.33463112,  0.21250485],
                     dtype=np.float32)}
)

def change_learning_rate(net, epoch):
    net.updates = partial(nesterov_momentum, learning_rate=0.001)
    net.compile()

def change_subsample(net, epoch):
    net.source.subsample_target = 3
    net.generate_validation_data_and_set_shapes()

net_dict = dict(
    save_plot_interval=SAVE_PLOT_INTERVAL,
    loss_function=scaled_cost,
    updates=partial(nesterov_momentum, learning_rate=0.01),
    do_save_activations=True,
    epoch_callbacks={501: change_learning_rate}
)

def exp_a(name):
    #  avg valid cost =  0.5296852589
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_b(name):
    # 1 layer, pool after RNN
    #  avg valid cost =  0.8358715773
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_c(name):
    # 2 layers, pool in between
    #  avg valid cost =  0.5183933973
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=(1/sqrt(N))),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_d(name):
    # 3 layers, pool after first layer
    #  avg valid cost =  0.5396855474
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=(1/sqrt(N))),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=(1/sqrt(N))),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_e(name):
    # layerwise pre-training
    # avg valid cost =  0.6081719398
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy['subsample_target'] = 1
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net_dict_copy['layer_changes'] = {
        501: {
            'remove_from': -3,
            'new_layers': [
                {
                    'type': BidirectionalRecurrentLayer,
                    'num_units': N,
                    'gradient_steps': GRADIENT_STEPS,
                    'W_in_to_hid': Normal(std=1/sqrt(N)),
                    'nonlinearity': tanh
                },
                {
                    'type': DenseLayer,
                    'num_units': source.n_outputs,
                    'nonlinearity': None,
                    'W': Normal(std=(1/sqrt(N)))
                }
            ]
        },
        1001: {
            'remove_from': -3,
            'callback': change_subsample,
            'new_layers': [
                {
                    'type': FeaturePoolLayer,
                    'ds': 3, # number of feature maps to be pooled together
                    'axis': 1, # pool over the time axis
                    'pool_function': T.max
                },
                {
                    'type': BidirectionalRecurrentLayer,
                    'num_units': N,
                    'gradient_steps': GRADIENT_STEPS,
                    'W_in_to_hid': Normal(std=1/sqrt(N)),
                    'nonlinearity': tanh
                },
                {
                    'type': DenseLayer,
                    'num_units': source.n_outputs,
                    'nonlinearity': None,
                    'W': Normal(std=(1/sqrt(N)))
                }
            ]
        }
    }
    net = Net(**net_dict_copy)
    return net

def exp_f(name):
    # layerwise pre-training (pre-train with pool)
    # need to re-run
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy['subsample_target'] = 1
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net_dict_copy['layer_changes'] = {
        501: {
            'remove_from': -3,
            'callback': change_subsample,
            'new_layers': [
                {
                    'type': BidirectionalRecurrentLayer,
                    'num_units': N,
                    'gradient_steps': GRADIENT_STEPS,
                    'W_in_to_hid': Normal(std=1/sqrt(N)),
                    'nonlinearity': tanh
                },
                {
                    'type': DenseLayer,
                    'num_units': source.n_outputs,
                    'nonlinearity': None,
                    'W': Normal(std=(1/sqrt(N)))
                },
                {
                    'type': FeaturePoolLayer,
                    'ds': 3, # number of feature maps to be pooled together
                    'axis': 1, # pool over the time axis
                    'pool_function': T.max
                }
            ]
        },
        1001: {
            'remove_from': -3,
            'new_layers': [
                {
                    'type': BidirectionalRecurrentLayer,
                    'num_units': N,
                    'gradient_steps': GRADIENT_STEPS,
                    'W_in_to_hid': Normal(std=1/sqrt(N)),
                    'nonlinearity': tanh
                },
                {
                    'type': DenseLayer,
                    'num_units': source.n_outputs,
                    'nonlinearity': None,
                    'W': Normal(std=(1/sqrt(N)))
                }
            ]
        }
    }
    net = Net(**net_dict_copy)
    return net

def exp_g(name):
    # sigmoid
    #  avg valid cost =  1.5114594698
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': sigmoid
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': sigmoid
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': sigmoid
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_h(name):
    # ReLU
    # doesn't train: training error is 0
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': rectify
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': rectify
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': rectify
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_i(name):
    # 2 x dense layers at end (both with 5 units)
    # avg valid cost =  0.5794851780
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(source.n_outputs)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_j(name):
    # 2 x dense layers at end (penultiate with  50 units)
    #  avg valid cost =  0.5457109213
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': N,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_k(name):
    # RNN output
    # doesn't train (0 error)
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': source.n_outputs,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': None
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_l(name):
    # RNN output, 2 layers
    # NaNs
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': source.n_outputs,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': None
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': source.n_outputs,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(source.n_outputs)),
            'nonlinearity': None
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_m(name):
    # Conv AND pool, with 50 filters, filter_length=10
    # avg valid cost =  0.4936202168
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=9
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 50,
            'filter_length': 10,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_n(name):
    # Conv AND pool, with 10 filters
    # avg valid cost =  0.4842122793 (run on 277)
    # avg valid cost =  0.5845696926 (run on 278)
    # need to re-run with correct initialisations
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=2
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 10,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_o(name):
    # 2 lots of conv, then pool
    # need to re-run with correct initialisations
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=4
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 32,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 16,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_p(name):
    # 2 lots of conv, then pool
    # a re-run of O but with correct initialisations
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=4
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 32,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(32)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 16,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(50))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(16)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_q(name):
    # Conv AND pool, with 10 filters
    # avg valid cost =  0.4842122793 (run on 277)
    # avg valid cost =  0.5845696926 (run on 278)
    #  re-run of P but with correct initialisations
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=2
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': 10,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(10)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    net.load_params(iteration=1139)
    return net

def exp_r(name):
    # 2 lots of conv, then pool
    # a re-run of O but with 50 filters and correct init
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=4
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': N,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': N,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_s(name):
    # Conv AND pool, with 50 filters
    # avg valid cost =  0.4842122793 (run on 277)
    # avg valid cost =  0.5845696926 (run on 278)
    #  re-run of P but with correct initialisations and 50 filters
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy.update(dict(
        subsample_target=3,
        input_padding=2
    ))
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1)  # (batch, features, time)
        },
        {
            'type': Conv1DLayer, # convolve over the time axis
            'num_filters': N,
            'filter_length': 3,
            'stride': 1,
            'nonlinearity': tanh,
            'W': Normal(std=1/sqrt(N))
        },
        {
            'type': DimshuffleLayer,
            'pattern': (0, 2, 1) # back to (batch, time, features)
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_t(name):
    # a but with no random seed
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source,
        seed=None
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_u(name):
    # a but with no random seed
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source,
        seed=None
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_v(name):
    # a
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_w(name):
    # 2x2x pool
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source,
        downsample_target=4
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 2, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 2, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_x(name):
    # 3x2x pool
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source,
        downsample_target=6
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 2, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_y(name):
    # 5-way RNN as penultimate layer
    source_dict_copy = deepcopy(source_dict)
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': FeaturePoolLayer,
            'ds': 3, # number of feature maps to be pooled together
            'axis': 1, # pool over the time axis
            'pool_function': T.max
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': 5,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1/sqrt(N)),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(5)))
        }
    ]
    net = Net(**net_dict_copy)
    return net

def exp_z(name):
    # layerwise pre-training
    # avg valid cost =  0.6081719398
    source_dict_copy = deepcopy(source_dict)
    source_dict_copy['subsample_target'] = 1
    source = RealApplianceSource(**source_dict_copy)
    net_dict_copy = deepcopy(net_dict)
    net_dict_copy.update(dict(
        experiment_name=name,
        source=source,
        epoch_callbacks={4501: change_learning_rate}
    ))
    N = 50
    net_dict_copy['layers_config'] = [
        {
            'type': BidirectionalRecurrentLayer,
            'num_units': N,
            'gradient_steps': GRADIENT_STEPS,
            'W_in_to_hid': Normal(std=1.),
            'nonlinearity': tanh
        },
        {
            'type': DenseLayer,
            'num_units': source.n_outputs,
            'nonlinearity': None,
            'W': Normal(std=(1/sqrt(N)))
        }
    ]
    net_dict_copy['layer_changes'] = {
        2001: {
            'remove_from': -3,
            'new_layers': [
                {
                    'type': BidirectionalRecurrentLayer,
                    'num_units': N,
                    'gradient_steps': GRADIENT_STEPS,
                    'W_in_to_hid': Normal(std=1/sqrt(N)),
                    'nonlinearity': tanh
                },
                {
                    'type': DenseLayer,
                    'num_units': source.n_outputs,
                    'nonlinearity': None,
                    'W': Normal(std=(1/sqrt(N)))
                }
            ]
        },
        4001: {
            'remove_from': -3,
            'callback': change_subsample,
            'new_layers': [
                {
                    'type': FeaturePoolLayer,
                    'ds': 3, # number of feature maps to be pooled together
                    'axis': 1, # pool over the time axis
                    'pool_function': T.max
                },
                {
                    'type': BidirectionalRecurrentLayer,
                    'num_units': N,
                    'gradient_steps': GRADIENT_STEPS,
                    'W_in_to_hid': Normal(std=1/sqrt(N)),
                    'nonlinearity': tanh
                },
                {
                    'type': DenseLayer,
                    'num_units': source.n_outputs,
                    'nonlinearity': None,
                    'W': Normal(std=(1/sqrt(N)))
                }
            ]
        }
    }
    net = Net(**net_dict_copy)
    return net

def main():
    #     EXPERIMENTS = list('abcdefghijklmnopqrstuvwxyz')
    EXPERIMENTS = list('qrstuvwxyz')
    for experiment in EXPERIMENTS:
        full_exp_name = NAME + experiment
        func_call = init_experiment(PATH, experiment, full_exp_name)
        logger = logging.getLogger(full_exp_name)
        try:
            net = eval(func_call)
            run_experiment(net, epochs=6000 if experiment == 'z' else 2000)
        except KeyboardInterrupt:
            logger.info("KeyboardInterrupt")
            break
        except Exception as exception:
            logger.exception("Exception")

if __name__ == "__main__":
    main()

reason: import numpy,import theano


text:
import cv2
import numpy as np
import math
import sys
import getopt

fileInput = None
fileOutput = None

opts, args = getopt.getopt(sys.argv[1:], "i:o:", ["input=", "ouput="])

for k, v in opts:
    if k in ("--input", "-i"):
        fileInput = v
    if k in ("--output", "-o"):
        fileOutput = v

if fileInput == None or fileOutput == None:
    print "Input or output wrong"
    sys.exit()

ext = fileInput.split('.')[-1]

if ext == "tif":
    tif = cv2.imread(fileInput, -1)
    width = tif.shape[0]
    height = tif.shape[1]
    png = np.zeros((width, height, 4), np.uint8)

    tif = tif / 65535.0
    a = np.clip(np.amax(tif, axis=2), 1e-6, 1)
    np.ceil(a * 255) / 255
    a = a.reshape(width, height, 1)
    png[:,:,0:3] = tif / a * 255
    a = a.reshape(width, height)
    png[:,:,3] = a * 255
    cv2.imwrite(fileOutput, png)

elif ext == "png":
    png = cv2.imread(fileInput, -1)
    png = png / 255.0
    a = png[:,:,3:4]

    tif = np.zeros((png.shape[0], png.shape[1], 3), np.uint16)
    tif[:,:,0:3] = png[:,:,0:3] * a * 65535.0

    cv2.imwrite(fileOutput, tif)

else:
    print "Input file is not png or tif format"
    sys.exit()

print("Successfully converted " + fileInput + " to " + fileOutput)

ext: py

max_stars_repo_name: abopaper/epaper

max_issues_repo_name: abopaper/epaper

max_forks_repo_name: abopaper/epaper

reason: import numpy


text:
#  Created byMartin.cz

import numpy as np
from .enums import *
from . layer import Layer

class Activation(Layer):
    """Represents a baseclass for activation function."""

    def __str__(self):
        """Gets string representation."""

        return self.__class__.__name__

    @staticmethod
    def create(activation, **kwargs):
        """
        Args:
            activation: str or None
                Activation function name such as 'linear', 'sigmoid', 'relu', 'tanh' or
                'softmax'. If set to None, activation is not applied.

        Return:
            miniml.Activation
                Activation layer.
        """

        if activation is None:
            return None

        if activation is None:
            return None

        if isinstance(activation, Activation):
            return activation

        if activation == LINEAR:
            return Linear()

        if activation == SIGMOID:
            return Sigmoid()

        elif activation == RELU:
            return ReLU()

        elif activation == LRELU:
            return LeakyReLU()

        elif activation == TANH:
            return Tanh()

        elif activation == SOFTMAX:
            return Softmax()

        raise ValueError("Unknown activation function specified! -> '%s" % activation)

class Linear(Activation):
    """Represents a linear (identity) activation function."""

    def forward(self, Z, **kwargs):
        """Performs forward propagation through activation function."""

        return Z

    def backward(self, dA, **kwargs):
        """Performs backward propagation through activation function."""

        return dA * 1

class Sigmoid(Activation):
    """Represents a sigmoid activation function."""

    def forward(self, Z, **kwargs):
        """Performs forward propagation through activation function."""

        self._A = 1 / (1 + np.exp(-Z))
        return self._A

    def backward(self, dA, **kwargs):
        """Performs backward propagation through activation function."""

        return dA * self._A * (1 - self._A)

class ReLU(Activation):
    """Represents a ReLU activation function."""

    def forward(self, Z, **kwargs):
        """Performs forward propagation through activation function."""

        self._A = np.maximum(Z, 0)
        return self._A

    def backward(self, dA, **kwargs):
        """Performs backward propagation through activation function."""

        dZ = np.array(dA, copy=True)
        dZ[self._A <= 0] = 0
        return dZ

class LeakyReLU(Activation):
    """Represents a LeakyReLU activation function."""

    ALPHA = 0.01

    def forward(self, Z, **kwargs):
        """Performs forward propagation through activation function."""

        self._A = np.array(Z, copy=True)
        self._A[Z < 0] = Z[Z < 0] * self.ALPHA
        return self._A

    def backward(self, dA, **kwargs):
        """Performs backward propagation through activation function."""

        dZ = np.ones_like(self._A)
        dZ[self._A < 0] *= self.ALPHA
        return dA * dZ

class Tanh(Activation):
    """Represents a Tanh activation function."""

    def forward(self, Z, **kwargs):
        """Performs forward propagation through activation function."""

        self._A = np.tanh(Z)
        return self._A

    def backward(self, dA, **kwargs):
        """Performs backward propagation through activation function."""

        return dA * (1 - np.power(self._A, 2))

class Softmax(Activation):
    """Represents a Softmax activation function."""

    def forward(self, Z, **kwargs):
        """Performs forward propagation through activation function."""

        eZ = np.exp(Z - np.max(Z))
        self._A = eZ / eZ.sum(axis=1, keepdims=True)
        return self._A

    def backward(self, dA, **kwargs):
        """Performs backward propagation through activation function."""

        m, C = self._A.shape
        dZ = np.zeros((m, C))
        ones = np.ones((1, C))
        ident = np.identity(C)

        for j in range(m):

            a = self._A[j].reshape(C, 1)
            da = dA[j].reshape(C, 1)

            t = np.matmul(a, ones)
            mat = t * (ident - t.T)
            dz = np.matmul(mat, da)
            dZ[j] = dz.T[0]

        return dZ

max_stars_repo_name: xxao/miniml

max_issues_repo_name: xxao/miniml

max_forks_repo_name: xxao/miniml


text:
"""Stitch and build a mosaic of all images in a directory"""
import argparse
import cv2 as cv
import glob
import numpy as np
import math
import itertools
import matplotlib.pyplot as plt

def mosaic(images_in_path, tile_size, fill_value=0, display=True):
    """
    Mosaic each image in "images_in_path" after resizing them to tile_size,
    all the pixels that don't fit in by the resized tiles
    are filled with fill_value

    :param str images_in_path: Directory with images to mosaic with
    :param tuple tile_size: tuple that each image in the mosaic is resized to
    :param int fill_value: Bounding box line width
    :param boolean display: display the overlaid roi before returning the array
    """
    x_tiles = y_tiles = math.ceil(np.sqrt(len(images_in_path)))
    if tile_size is not None:
        tile_size_x = tile_size[0]
        tile_size_y = tile_size[1]
    else:
        shape = cv.imread(images_in_path[0], cv.IMREAD_ANYDEPTH | cv.IMREAD_ANYCOLOR).shape
        tile_size_x = shape[0]
        tile_size_y = shape[1]
    mosaiced_im = np.ones((x_tiles * tile_size_x, y_tiles * tile_size_y, 3), dtype=np.uint8)
    mosaiced_im = mosaiced_im * fill_value

    indices = list(itertools.product(range(x_tiles), range(y_tiles)))
    for index, im_path in enumerate(images_in_path):
        im_rgb = cv.imread(im_path, cv.IMREAD_ANYDEPTH | cv.IMREAD_ANYCOLOR)
        if tile_size is not None:
            resized = cv.resize(im_rgb, (tile_size_x, tile_size_y))
        else:
            resized = im_rgb
        x, y = indices[index]
        mosaiced_im[
            x * tile_size_x: tile_size_x * (x + 1),
            y * tile_size_y: tile_size_y * (y + 1),
            :] = resized

    plt.imshow(mosaiced_im)
    plt.axis('off')
    if display:
        plt.show()
    return mosaiced_im

def main():
    parser = argparse.ArgumentParser(
        description="Save the mosaic from images")
    parser.add_argument(
        "--im_dir", help="Absolute path to folder name containing images", required=True, type=str)
    parser.add_argument(
        "--tile_size",
        help="[x,y] list of tile size in x, y", nargs='+', type=int, required=False)
    parser.add_argument(
        "--fill_value",
        help="fill the image with zeros or the first intensity at [0,0] in the image",
        type=str, required=False)
    parser.add_argument(
        "--output_png",
        help="Absolute path to folder name to save the roi overlaid images to", required=True, type=str)
    parser.add_argument(
        "--fmt",
        help="Image format", required=True, type=str)
    parser.add_argument(
        "--display",
        help="Display overlaid images, Default False", action='store_true')

    args = parser.parse_args()
    display = args.display
    im_dir = args.im_dir
    output_png = args.output_png
    tile_size = args.tile_size
    fill_value = args.fill_value
    fmt = args.fmt
    images_in_path = glob.glob(im_dir + "*." + fmt)
    if fill_value == "first":
        fill_value = cv.imread(images_in_path[0], cv.IMREAD_GRAYSCALE)[0, 0]
    else:
        fill_value = int(fill_value)
    im_rgb = mosaic(images_in_path, tile_size, fill_value, display=display)
    cv.imwrite(output_png, im_rgb)

if __name__ == '__main__':
    main()


text:
# -*- coding: utf-8 -*-
import numpy as np

def Qx(w):
    if type(w) != np.ndarray:
        if type(w) != list:
            w = np.array([w])
        else:
            w = np.array(w)
    d = w.shape
    if len(w) > 1:
        out = np.array([[np.ones(d),  np.zeros(d), np.zeros(d)],
                        [np.zeros(d),   np.cos(w),   np.sin(w)],
                        [np.zeros(d),  -np.sin(w),   np.cos(w)]])
    else:
        out = np.array([[1.0,        0.0,       0.0],
                        [0.0,  np.cos(w), np.sin(w)],
                        [0.0, -np.sin(w), np.cos(w)]])
    return out

def Qz(w):
    if type(w) != np.ndarray:
        if type(w) != list:
            w = np.array([w])
        else:
            w = np.array(w)
    d = w.shape
    if len(w) > 1:
        out = np.array([[np.cos(w),     np.sin(w), np.zeros(d)],
                        [-np.sin(w),    np.cos(w), np.zeros(d)],
                        [np.zeros(d), np.zeros(d),  np.ones(d)]])
    else:
        out = np.array([[np.cos(w[0]),  np.sin(w[0]), 0.0],
                        [-np.sin(w[0]), np.cos(w[0]), 0.0],
                        [0.0,              0.0, 1.0]])
    return out

def Q(theta1, psi, theta2):
    Q1 = Qz(theta1)
    Q2 = Qx(psi)
    Q3 = Qz(theta2)
    Q12 = np.tensordot(Q1, Q2, axes=([0], [1]))
    if len(Q12.shape) > 2:
        Q12 = np.swapaxes(Q12, 1, 2)
    Q123 = np.tensordot(Q12, Q3, axes=([0], [1]))
    if len(Q123.shape) > 2:
        Q123 = np.rollaxis(Q123, 3, 1)
    return Q123

class UpdatesBank(object):
    def __init__(self, error_rate=0.1, kappa=0.1, grains=100):
        self.error_rate = error_rate
        self.kappa = kappa
        theta1 = np.linspace(0, np.pi, grains)
        psi = np.linspace(0, 2*np.pi, grains)
        theta2 = np.linspace(0, np.pi, grains)
        Q123 = Q(theta1, psi, theta2)
        self.Q = Q123
        self.QQ = np.einsum('ijpqr,klpqr->ijklpqr', Q123, Q123)
        self.probs = self.get_probs()

    def update(self, Qhat):
        Q123 = np.tensordot(Qhat, self.Q, axes=([0], [1]))
        self.Q = Q123
        self.QQ = np.einsum('ijpqr,klpqr->ijklpqr', Q123, Q123)
        self.probs = self.get_probs()

    def get_probs(self):
        # Prepare some matrices for going between axis eccentricities
        # and probabilities
        T = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]])
        Tinv = np.linalg.inv(T)

        # Convert probabilities to axes
        vecp = self.error_rate * np.array([1, 0, self.kappa])
        veca = 1.0 - 2.0 * np.dot(T, vecp)

        # Create channel matrix
        M0 = np.diag(veca)

        # Get all rotations of channel matrix
        M1 = np.tensordot(self.QQ, M0, axes=([1, 2], [0, 1]))

        # Retrieve projected axis eccentricities
        veca = np.diagonal(M1)
        veca = np.rollaxis(veca, 3, 0)

        # Retrieve projected probabilities
        vecp = np.tensordot(Tinv, veca - 1.0, axes=([0], [0]))
        px = vecp[0, :, :, :]
        py = vecp[1, :, :, :]
        pz = vecp[2, :, :, :]
        return {"x": px, "y": py, "z": pz, None: 1.0-px-py-pz}

class SurfboardEstimator(object):
    def __init__(self, error_rate=0.1, kappa=0.1, grains=100):
        self.kappa = kappa
        self.error_rate = error_rate
        self.grains = grains
        self.p_angles = np.ones((grains,)*3) / float(grains ** 3)
        theta1 = np.linspace(0, np.pi, grains)
        psi = np.linspace(0, 2*np.pi, grains)
        theta2 = np.linspace(0, np.pi, grains)
        Q123 = Q(theta1, psi, theta2)
        self.Q = Q123
        self.Qhat = Q(0.0, 0.0, 0.0)
        self.idx = [0, 0, 0]
        self.bank = UpdatesBank(error_rate, kappa, grains)

    def update(self, error):
        p_update = self.bank.probs.get(error, None)
        if p_update is not None:
            self.p_angles = self.p_angles * p_update
            self.p_angles = self.p_angles / np.sum(self.p_angles)
            idx = np.argmax(self.p_angles)
            idx = np.unravel_index(idx, self.p_angles.shape)
            self.idx = idx
            self.Qhat = self.Q[:, :, idx[0], idx[1], idx[2]]
            self.bank.update(self.Qhat)

class SurfboardChannel(object):
    def __init__(self, error_rate=0.1, kappa=0.1, grains=100):
        self.error_rate = error_rate
        self.kappa = kappa
        theta1 = np.linspace(0, np.pi, grains)
        psi = np.linspace(0, 2*np.pi, grains)
        theta2 = np.linspace(0, np.pi, grains)
        self.idx = np.random.randint(0, 100, 3)
        self.theta1val = theta1[self.idx[0]]
        self.psival = psi[self.idx[1]]
        self.theta2val = theta2[self.idx[2]]
        self.Qval = Q(self.theta1val, self.psival, self.theta2val)
        self.Qeff = self.Qval
        self.probs = self.get_probs()

    def get_probs(self):
        # Prepare some matrices for going between axis eccentricities
        # and probabilities
        T = np.array([[0, 1, 1], [1, 0, 1], [1, 1, 0]])
        Tinv = np.linalg.inv(T)

        # Convert probabilities to axes
        vecp = self.error_rate * np.array([1, 0, self.kappa])
        veca = 1.0 - 2.0 * np.dot(T, vecp)

        # Create channel matrix
        M0 = np.diag(veca)

        # Rotate channel matrix
        M1 = np.dot(self.Qeff, M0)
        M2 = np.dot(M1, self.Qeff.T)

        # Retrieve projected axis eccentricities
        veca = np.diagonal(M2)

        # Retrieve projected probabilities
        vecp = 0.5 * np.dot(Tinv, 1.0 - veca)
        px, py, pz = vecp[0], vecp[1], vecp[2]
        return {"x": px, "y": py, "z": pz, None: 1.0-px-py-pz}

    def update(self, Qhat):
        self.Qeff = np.dot(Qhat, self.Qval)
        self.probs = self.get_probs()

    def get_error(self):
        probs, errors = [], []
        for s, prob in self.probs.iteritems():
            if prob > 0:
                probs.append(prob)
                errors.append(s)
        s = np.random.choice(errors, 1, p=probs)[0]
        return s


text:
import numpy as np
import matplotlib.pyplot as plt

from uncertify.visualization.plotting import imshow, setup_plt_figure
from uncertify.utils.custom_types import Tensor

from typing import Tuple

def imshow_grid(grid_tensor: Tensor, one_channel: bool = True, plt_show: bool = True, **plt_kwargs) -> Tuple[plt.Figure,
                                                                                plt.Axes]:
    """Does an imshow on an grid returned by torchvision.utils.make_grid()."""
    fig, ax = setup_plt_figure(**plt_kwargs)
    if one_channel:
        grid_tensor = grid_tensor.mean(dim=0)
    np_img = grid_tensor.numpy()
    if one_channel:
        imshow(np_img, ax=ax, **plt_kwargs)
    else:
        imshow(np.transpose(np_img, (1, 2, 0)), ax=ax, **plt_kwargs)
    if plt_show:
        plt.show()
    return fig, ax

max_stars_repo_name: matthaeusheer/uncertify

max_issues_repo_name: matthaeusheer/uncertify

max_forks_repo_name: matthaeusheer/uncertify


text:
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = (20,8)

filename = 'node2-fast-rssi-10e6-ch25-withtx.txt'
logFile = open(filename, 'r')

logs = logFile.readlines()

rssi = []
signal = []
idx = []
ndx = []
for i, l in enumerate(logs):
  if ('EavesDr' in l):
    try:
      rs = int(l.split(' ')[-1])
      rssi.append(rs)
      idx.append(i)
    except:
      try:
        rs = int(l.split(' ')[-4])
        signal.append(rs)
        ndx.append(i)
      except:
        continue

plt.title('Node 2 -- 10e-6 -- CH25 - With Transmitter')
plt.ylim([-120, 0])
plt.grid(True, axis='y')
plt.plot(idx, rssi)
plt.scatter(ndx, signal, c='#F39C12')
plt.savefig(filename[:-4], dpi=300)
#plt.show()

max_stars_repo_name: CloudyPadmal/contiki-ng

max_issues_repo_name: CloudyPadmal/contiki-ng

max_forks_repo_name: CloudyPadmal/contiki-ng


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 29 22:15:54 2017

@author: Ramon Casero <rcasero@gmail.com>
"""

"""
This file is part of Cytometer
Copyright 2021 Medical Research Council
SPDX-License-Identifier: Apache-2.0
Author: Ramon Casero <rcasero@gmail.com>
"""

import os

os.environ['KERAS_BACKEND'] = 'tensorflow'

import sys

cytometer_dir = os.path.expanduser("~/Software/cytometer")
if cytometer_dir not in sys.path:
    sys.path.append(cytometer_dir)

# export LD_LIBRARY_PATH=$CONDA_PREFIX/lib:$LD_LIBRARY_PATH

# different versions of conda keep the path in different variables
if 'CONDA_ENV_PATH' in os.environ:
    conda_env_path = os.environ['CONDA_ENV_PATH']
elif 'CONDA_PREFIX' in os.environ:
    conda_env_path = os.environ['CONDA_PREFIX']
else:
    conda_env_path = '.'

if os.environ['KERAS_BACKEND'] == 'theano':
    # configure Theano
    os.environ['MKL_THREADING_LAYER'] = 'GNU'
    os.environ['THEANO_FLAGS'] = 'floatX=float32,device=cuda0,' \
                                 + 'dnn.include_path=' + conda_env_path + '/include,' \
                                 + 'dnn.library_path=' + conda_env_path + '/lib,' \
                                 + 'gcc.cxxflags=-I/usr/local/cuda-9.1/targets/x86_64-linux/include'
    import theano
elif os.environ['KERAS_BACKEND'] == 'tensorflow':
    # configure tensorflow
    import tensorflow
else:
    raise Exception('No configuration found when the backend is ' + os.environ['KERAS_BACKEND'])

import keras.backend as K
#from keras import __version__ as keras_version
#from pkg_resources import parse_version

# configure Keras, to avoid using file ~/.keras/keras.json
K.set_image_dim_ordering('tf')
K.set_floatx('float32')
K.set_epsilon('1e-07')
# fix "RuntimeError: Invalid DISPLAY variable" in cluster runs
# import matplotlib
# matplotlib.use('agg')

# load module dependencies
#import datetime
import matplotlib.pyplot as plt
import numpy as np

#import cytometer.deepcell as deepcell
import cytometer.deepcell_models as deepcell_models
import cytometer.models as models
import cytometer.layers as layers
#reload(deepcell)
#reload(deepcell_models)
#reload(layers)
#reload(models)

# instantiate CNN
model = models.sparse_feature_net_61x61_no_dilated_pooling()

optimizer = keras.optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
lr_sched = deepcell.rate_scheduler(lr = 0.01, decay = 0.95)
class_weight = {0:1, 1:1, 2:1}

model.compile(loss='categorical_crossentropy',
			  optimizer=optimizer,
			  metrics=['accuracy'])

# load data
datadir = "/home/rcasero/Software/cytometer/data/deepcell/training_data_npz/3T3"
datafile = "3T3_all_61x61.npz"
outdir = "/tmp"
outfile = "3T3_all_61x61"

train_dict, (X_test, Y_test) = deepcell.get_data_sample(os.path.join(datadir, datafile))

it = 0 # iteration
batch_size = 256
n_epoch = 25

training_data_file_name = os.path.join(direc_data, dataset + ".npz")
todays_date = datetime.datetime.now().strftime("%Y-%m-%d")

file_name_save = os.path.join(direc_save, todays_date + "_" + dataset + "_" + expt + "_" + str(it)  + ".h5")

file_name_save_loss = os.path.join(direc_save, todays_date + "_" + dataset + "_" + expt + "_" + str(it) + ".npz")

train_dict, (X_test, Y_test) = deepcell.get_data_sample(training_data_file_name)

# the data, shuffled and split between train and test sets
print('X_train shape:', train_dict["channels"].shape)
print(train_dict["pixels_x"].shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

max_stars_repo_name: rcasero/cytometer

max_issues_repo_name: rcasero/cytometer

max_forks_repo_name: rcasero/cytometer


text:
from keras.callbacks import ModelCheckpoint
import numpy as np
from keras import layers, models, optimizers
from keras import backend as K
from keras.utils import to_categorical
import matplotlib.pyplot as plt
from utils import combine_images
from PIL import Image
from capsulelayers import CapsuleLayer, PrimaryCap, Length, Mask
from six.moves import cPickle as pickle
from six.moves import range
from sklearn.model_selection import train_test_split
from keras.models import load_model
from capsulenet_vanilla_mnist import load_mnist, load_notMNIST_from_npy, CapsNet
import tensorflow as tf

import os
import argparse
from keras.preprocessing.image import ImageDataGenerator
from keras import callbacks

# setting the hyper parameters
parser = argparse.ArgumentParser(description="Capsule Network on MNIST.")
parser.add_argument('--epochs', default=50, type=int)
parser.add_argument('--batch_size', default=100, type=int)
parser.add_argument('--lr', default=0.001, type=float,
                    help="Initial learning rate")
parser.add_argument('--lr_decay', default=0.9, type=float,
                    help="The value multiplied by lr at each epoch. Set a larger value for larger epochs")
parser.add_argument('--lam_recon', default=0.392, type=float,
                    help="The coefficient for the loss of decoder")
parser.add_argument('-r', '--routings', default=3, type=int,
                    help="Number of iterations used in routing algorithm. should > 0")
parser.add_argument('--shift_fraction', default=0.1, type=float,
                    help="Fraction of pixels to shift at most in each direction.")
parser.add_argument('--debug', action='store_true',
                    help="Save weights by TensorBoard")
parser.add_argument('--save_dir', default='./result')
parser.add_argument('-t', '--testing', action='store_true',
                    help="Test the trained model on testing dataset")
parser.add_argument('--digit', default=5, type=int,
                    help="Digit to manipulate")
parser.add_argument('-w', '--weights', default=None,
                    help="The path of the saved weights. Should be specified when testing")
args = parser.parse_args()
print(args)

if not os.path.exists(args.save_dir):
    os.makedirs(args.save_dir)

# load data
(x_train, y_train), (x_test, y_test) = load_mnist()
x_train1, y_train1, x_test1, y_test1 = load_notMNIST_from_npy()

# define model
model, eval_model, manipulate_model = CapsNet(input_shape=x_train.shape[1:],
                                              n_class=len(np.unique(np.argmax(y_train, 1))),
                                              routings=args.routings)
model.summary()

# train or test
#model.load_weights(args.weights)
model.load_weights('notMNIST_vanilla_result/weights-31.h5')
#model.load_weights('mnist_vanilla_result/weights-04.h5')
saver = tf.train.Saver()
saver.save(K.get_session(), '/tmp/keras_notMNIST_model.ckpt')

max_stars_repo_name: crypdick/CapsNet-Keras-notMNIST

max_issues_repo_name: crypdick/CapsNet-Keras-notMNIST

max_forks_repo_name: crypdick/CapsNet-Keras-notMNIST


text:
import numpy as np

#------------------Giant flare energy------------------
def flare_energy():
    """
    Return a random value of giant flare energy, in the range
    10^38 erg to 10^47 erg.
    - Args: None.
    - Returns: Random giant flare energy value in ergs.
    """
    f_energy = np.random.uniform(1e38,1e47)
    return f_energy

#-------------------------------------------------------

def generate_energy(dirname):
    #Generate 100,000 giant flares
    energy = [flare_energy() for _ in range(100000)]

    arr_e = np.array(energy)
    np.savetxt(f'{dirname}/energy.txt', arr_e)

max_stars_repo_name: Kushaalkumar-pothula/FLARE

max_issues_repo_name: Kushaalkumar-pothula/FLARE

max_forks_repo_name: Kushaalkumar-pothula/FLARE


text:
import os

import pandas as pd
import numpy as np
import json
from scipy import stats
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from boost_functions import *
from sqlalchemy.orm import Session
from sqlalchemy import create_engine

from flask import Flask, jsonify, render_template, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///../data/fantasy_football_2018.db"
db = SQLAlchemy(app)

# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(db.engine, reflect=True)

# Save references to each table
week1_ppr_projections = Base.classes.week1_ppr_projections
# Samples = Base.classes.table2_name

@app.route("/")
def index():
    """Return the homepage."""
    return render_template("index.html")

# Josh
@app.route("/data", methods=['POST', 'GET'])
def data():
    if request.method == "GET":
        # Return an initial blank form before user inputs their custom factors
        #call database test
        stmt = """
            SELECT *
            FROM week3_ppr_projections
            """
        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #send dataframe to records (a list of dictionaries for each row)
        initialTableData = df_proj.to_dict('records')
        return render_template("data.html", tableData=initialTableData)

    elif request.method == "POST":
        # use the user input to operate on the dataframe and then a list of dictionaries (records) that can be referenced in the html
        print(request.form)
        filter_player_name = request.form.get("player_name", "no_player")
        filter_position = request.form.get("position", "no_position")
        filter_team = request.form.get("team", "no_team")
        if filter_player_name != "":
        # return jsonify(request.form)
        # scout = request.form.get('')

            #call database test
            stmt = """
                SELECT *
                FROM week3_ppr_projections
                WHERE PLAYER LIKE '%{}%'

                """.format(filter_player_name)
        elif filter_position != "":
                # return jsonify(request.form)
                # scout = request.form.get('')

                    #call database test
                stmt = """
                    SELECT *
                    FROM week3_ppr_projections
                    WHERE POS LIKE '%{}%'

                    """.format(filter_position)
        elif filter_team != "":
                # return jsonify(request.form)
                # scout = request.form.get('')

                    #call database test
                stmt = """
                    SELECT *
                    FROM week3_ppr_projections
                    WHERE TEAM LIKE '%{}%'

                    """.format(filter_team)

        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #send dataframe to records (a list of dictionaries for each row)
        userTableData = df_proj.to_dict('records')
        return render_template("data.html", tableData=userTableData)

# Connor
@app.route("/tweet", methods=['POST', 'GET'])
def tweet():
    if request.method == "GET":
        # Return an initial blank form before user inputs their custom factors
        #call database test
        stmt = """
            SELECT *
            FROM week_3_tweets
            """
        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #send dataframe to records (a list of dictionaries for each row)
        initialTableData = df_proj.to_dict('records')
        return render_template("tweet.html", tableData=initialTableData)

    elif request.method == "POST":
        # use the user input to operate on the dataframe and then a list of dictionaries (records) that can be referenced in the html
        filter_player_name = request.form.get("player_name")
        # return jsonify(request.form)
        # scout = request.form.get('')

        #call database test
        stmt = """
            SELECT *
            FROM week_3_tweets
            WHERE player LIKE '%{}%'
            """.format(filter_player_name)
        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #send dataframe to records (a list of dictionaries for each row)
        userTableData = df_proj.to_dict('records')
        return render_template("tweet.html", tableData=userTableData)

# Mike
@app.route("/input", methods=['POST', 'GET'])
def input():
    if request.method == "GET":
        # Return an initial blank data page before user inputs their custom factors
        #have the default values populate as 20% for each one and Full for each boost
        defaultInputs = {'Week': '3',
                        'ESPN': '20',
                        'CBS': '20',
                        'Sharks': '20',
                        'Scout': '20',
                        'Prior': '20',
                        'Defense': 'Full',
                        'OverUnder': 'Full',
                        'Twitter': 'Full'}

        return render_template("input.html", formData=defaultInputs)

    elif request.method == "POST":
        # use the user input to operate on the dataframe and then a list of dictionaries (records) that can be referenced in the html
        #define all of our variables from the form POST data
        week = request.form.get('Week')
        espn_wt = eval(request.form.get('ESPN'))
        cbs_wt = eval(request.form.get('CBS'))
        sharks_wt = eval(request.form.get('Sharks'))
        scout_wt = eval(request.form.get('Scout'))
        prior_wt = eval(request.form.get('Prior'))
        def_boost_wt = request.form.get('Defense')
        overunder_boost_wt = request.form.get('OverUnder')
        twitter_boost_wt = request.form.get('Twitter')

        ### CUSTOM PROJECTIONS AVERAGE ###
        #convert weights to percentages
        espn_pct = espn_wt/100
        cbs_pct = cbs_wt/100
        sharks_pct = sharks_wt/100
        scout_pct = scout_wt/100
        prior_pct = prior_wt/100

        #get the projections for the week from the database
        stmt = f"""
                SELECT *
                FROM week{week}_ppr_projections
                """
        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #create initial custom weighted average of the five projection sources based on user input
        df_proj['FPTS_PPR_CUSTOM_AVG'] = df_proj.apply(lambda row:
                                            (row['FPTS_PPR_ESPN']*espn_pct +
                                            row['FPTS_PPR_CBS']*cbs_pct +
                                            row['FPTS_PPR_SHARKS']*sharks_pct +
                                            row['FPTS_PPR_SCOUT']*scout_pct +
                                            row['FPTS_PPR_PRVS_WK_ACTUAL']*prior_pct),
                                            axis='columns')

        ### BOOST FACTORS ###

        #GET DEFENSE DATA for your week selected - all defense data for each week is one database,
        #select only columns you want
        #NOTE - currently it is pulling in defense sentiment always for week 3 - no sentiments for other weeks
        stmt_def = f"""
                    SELECT team_abbr, week_{week}_opp, week_{week}_proj, score_sentiment
                    FROM def_scores
                    """
        df_defense = pd.read_sql_query(stmt_def, db.session.bind)

        #currently score_sentiment is for the offensive opponenet perspective so Min's really good Defense
        #against Buf has score sentiment of 'Terrible' which is terrible for Buf, so all Buf players
        #would get negative points weighting - rename column
        #rename team_abbr to def_team_abbr
        df_defense.rename(columns={'score_sentiment': 'offense_opp_def_impact',
                                'team_abbr': 'def_team_abbr'}, inplace=True)

        #GET OVERUNDER DATA for your week selected
        try:
            stmt_overunder = f"""
                            SELECT *
                            FROM week_{week}_over_unders
                            """
            df_overunder = pd.read_sql_query(stmt_overunder, db.session.bind)

        except: #if error because table doesn't exist (we dont' have all previous week data)
            #then just return an empty dataframe with columns that match the other df so calcs run,
            # but end with zero boost factors
            df_overunder = pd.DataFrame(columns=['Unnamed: 0', 'team', 'over_under', 'team_abbr', 'binned', 'score_sentiment'])

        #GET TWEET DATA for your week selected
        try:
            stmt_tweets = f"""
                            SELECT *
                            FROM week_{week}_tweets
                            """
            df_tweets = pd.read_sql_query(stmt_tweets, db.session.bind)

        except: #if error because table doesn't exist (we dont' have all previous week data)
            #then just return an empty dataframe with columns that match the other df so calcs run,
            # but end with zero boost factors
            df_tweets = pd.DataFrame(columns=['Unnamed: 0', 'number', 'player', 'compound_score', 'binned',
                                                'expert_sentiment', 'color', 'all_tweets'])

        #MERGE BOOST FACTOR DATA TO PLAYERS/PROJECTION DF
        # #check size of starting df
        # print(df_proj.shape)

        #first merge in the twitter sentiment - always do left merge (rather have Nans for boost factors than lose players)
        df_boosts = pd.merge(df_proj, df_tweets[['player', 'compound_score', 'expert_sentiment']],
                            how='left', left_on='PLAYER', right_on='player')
        df_boosts.drop('player', axis='columns', inplace=True) #don't need to keep this added player name column
        # print(df_boosts.shape)

        #second merge in the overunder - always do left merge (rather have Nans for boost factors than lose players)
        df_boosts = pd.merge(df_boosts, df_overunder[['team_abbr', 'over_under', 'score_sentiment']],
                            how='left', left_on='TEAM', right_on='team_abbr')
        df_boosts.drop('team_abbr', axis='columns', inplace=True) #don't need to keep this added team abbr column
        # print(df_boosts.shape)

        #third merge in the defense projections - always do left merge (rather have Nans for boost factors than lose players)
        #will merge by the team of the datframe and in defense looking for the opponent (that is offensive opponent of the defense listed as team)
        df_boosts = pd.merge(df_boosts, df_defense[['def_team_abbr', f"week_{week}_opp", f"week_{week}_proj", 'offense_opp_def_impact']],
                            how='left', left_on='TEAM', right_on=f"week_{week}_opp")
        df_boosts.drop(f"week_{week}_opp", axis='columns', inplace=True) #don't need to keep this added team abbr column
        # print(df_boosts.shape)

        #convert any of the Nans in our binned sentiment columns to Neutral (that will just apply a zero boost to those players)
        df_boosts.fillna('Neutral', inplace=True)

        #CALCULATE BOOST FACTORS AND GET FINAL POINTS
        df_boosts['OPPOSING_DEFENSE_BOOST'] = df_boosts.apply(lambda row:
                                                defense_boost_calculator(row, def_boost_wt),
                                                axis='columns')

        df_boosts['OVER_UNDER_BOOST'] = df_boosts.apply(lambda row:
                                            over_under_boost_calculator(row, overunder_boost_wt),
                                            axis='columns')

        df_boosts['TWITTER_BOOST'] = df_boosts.apply(lambda row:
                                            twitter_boost_calculator(row, twitter_boost_wt),
                                            axis='columns')

        #calculate final FPTS ADDING UP ALL BOOST PTS TO THE CUSTOM AVG
        df_boosts['FPTS_PPR_CUSTOM_AVG_BOOST'] = df_boosts[['FPTS_PPR_CUSTOM_AVG', 'OPPOSING_DEFENSE_BOOST',
                                                    'OVER_UNDER_BOOST', 'TWITTER_BOOST']].sum(axis='columns')

        #reorder dataframe and limits columns for the final output data and sort descending by custom poitns
        df_boosts_output = df_boosts[['PLAYER', 'POS', 'TEAM', 'FPTS_PPR_CUSTOM_AVG_BOOST',
                                    'FPTS_PPR_ESPN', 'FPTS_PPR_CBS', 'FPTS_PPR_SHARKS', 'FPTS_PPR_SCOUT',
                                    'FPTS_PPR_PRVS_WK_ACTUAL', 'FPTS_PPR_CUSTOM_AVG',
                                    'OVER_UNDER_BOOST', 'TWITTER_BOOST',
                                    'OPPOSING_DEFENSE_BOOST']].sort_values('FPTS_PPR_CUSTOM_AVG_BOOST',
                                                                            ascending=False)

        ### SEND DATA/RENDER TEMPLATE ###
        #round dataframe numbers to tenths place (so doesn't show up whole long decimal)
        df_boosts_output = df_boosts_output.round(decimals=1)

        #send dataframe to records (a list of dictionaries for each row)
        userTableData = df_boosts_output.to_dict('records')

        #render input.html template, return tableData that can build table of the data with user specified calculations,
        #and return the original userinputs so that can populate those in the input fields so user can see what inputs
        #were being used for the calculations they are seeing
        return render_template("input.html", tableData=userTableData, formData=request.form)

# Zach
@app.route("/compare/<week>", methods=['POST', 'GET'])
def getCharts(week):
    #get week <week> projections & actuals
    stmt_proj = "SELECT * FROM week{}_ppr_projections".format(week)
    df_ppr_projections = pd.read_sql_query(stmt_proj, db.session.bind)

    stmt_actual = "SELECT * FROM week{}_ppr_actuals".format(week)
    df_ppr_actuals = pd.read_sql_query(stmt_actual, db.session.bind)

    #merge starting with the actuals db and merge inner (don't want to evaluate correlations of players not in db)
    df_ppr = pd.merge(df_ppr_actuals, df_ppr_projections[['PLAYER', 'POS',
                                                'FPTS_PPR_ESPN', 'FPTS_PPR_CBS', 'FPTS_PPR_SHARKS',
                                                'FPTS_PPR_SCOUT', 'FPTS_PPR_PRVS_WK_ACTUAL']],
                        how='inner', on=['PLAYER', 'POS'])

    #average all four of our projection sources of ESPN, CBS, Sharks, and Scout
    df_ppr['FPTS_PPR_AVG_PROJ'] = df_ppr[['FPTS_PPR_ESPN','FPTS_PPR_CBS',
                                    'FPTS_PPR_SHARKS', 'FPTS_PPR_SCOUT']].mean(axis='columns')

    json_ppr = df_ppr.to_dict('records')

    return jsonify(json_ppr)

@app.route("/compare", methods=['POST', 'GET'])
def compare():
    if request.method == "GET":
        # Return an initial blank form before user inputs their custom factors
        stmt = """
            SELECT *
            FROM week3_ppr_projections
            """
        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #send dataframe to records (a list of dictionaries for each row)
        initialTableData = df_proj.to_dict('records')
        return render_template("compare.html", tableData=initialTableData)

    elif request.method == "POST":
        # use the user input to operate on the dataframe and then a list of dictionaries (records) that can be referenced in the html

        #return jsonify(request.form)
        scout = request.form.get('')

        #call database test
        stmt = """
            SELECT *
            FROM week3_ppr_projections
            """
        df_proj = pd.read_sql_query(stmt, db.session.bind)

        #send dataframe to records (a list of dictionaries for each row)
        userTableData = df_proj.to_dict('records')
        return render_template("compare.html", tableData=userTableData)

if __name__ == '__main__':
   app.run(debug = True)

max_stars_repo_name: zgscherrer/Project-Fantasy-Football

max_issues_repo_name: zgscherrer/Project-Fantasy-Football

max_forks_repo_name: zgscherrer/Project-Fantasy-Football

reason: import numpy,from scipy


text:
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Model

def build_model(bert_layer, max_len=512):
    input_word_ids = Input(shape=(max_len, ), dtype=tf.int32, name='input_word_ids')
    input_mask = Input(shape=(max_len, ), dtype=tf.int32, name='input_mask')
    segment_ids = Input(shape=(max_len, ), dtype=tf.int32, name='segment_ids')

    _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
    clf_output = sequence_output[:, 0, :]

    model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=clf_output)

    return model

max_stars_repo_name: Pixir/Pixir

max_issues_repo_name: Pixir/Pixir

max_forks_repo_name: Pixir/Pixir