<commit_before>import numpy as np
import sknn.mlp
import time
from sklearn import metrics, preprocessing
import matplotlib.pyplot as plt
import src.nnload as nnload
import src.nntrain as nntrain
import pickle

# ---  BUILDING NEURAL NETS  --- #

def train_nn_wrapper(hidneur,
                     minlev=0.0,rainonly=False,data_dir='./data/'):
    #Import packages
    # Load data
    x, y, Pout, lat, lev, dlev, timestep = nnload.loaddata(data_dir + 'nntest.nc', minlev)
    # Ensure that input and outputs are lined up in time!
    x=x[1:,:]
    y=y[0:-1,:]
    # Print some statistics about rain and limit to when it's raining if True
    x, y, Pout = nnload.limitrain(x, y, Pout, rainonly)
    # Store when convection occurs
    cv = nnload.whenconvection(y)
    # Normalize input using the scikit-learn preprocessing tools
    scaler_x = preprocessing.MinMaxScaler(feature_range=(-1.0,1.0))
    scaler_y = preprocessing.MaxAbsScaler()
    # Randomly choose samples
    samples = np.random.choice(x.shape[0], x.shape[0], replace=False)
    x1,x2,x3    = nnload.pp(x,  samples, scaler_x)
    y1,y2,y3    = nnload.pp(y,  samples, scaler_y)
    cv1,cv2,cv3 = nnload.pp(cv, samples, None, scale_data=False)
    # Build, train, and save model
    r_mlp, r_str = nntrain.build_nn('regress',['Rectifier'],[hidneur],
                                    'momentum',batch_size=100,n_stable=25)
                                #, regularize = 'L2', weight_decay = 0.0001)
    print(r_str)
    r_mlp, r_errors = nntrain.train_nn(r_mlp,r_str,x1,y1,x2,y2)
    pickle.dump([r_mlp, r_str, r_errors], 
                open(data_dir + 'regressors/' + r_str + '.pkl', 'wb'))

error=[]
def store_stats( avg_train_error, best_train_error, avg_valid_error, best_valid_error,**_):
    error.append((avg_train_error, best_train_error, avg_valid_error, best_valid_error))

def build_nn(method,actv_fnc,hid_neur,learning_rule,batch_size=100,n_iter=None, n_stable=None,
             learning_rate=0.01,learning_momentum=0.9,
             regularize = None, weight_decay = None, valid_size = 0.25):
    """Builds a multi-layer perceptron via the scikit neural network interface"""
    # First build layers
    layers = [sknn.mlp.Layer(f,units=h) for f,h in zip(actv_fnc,hid_neur)]
    # Append a linear output layer if regressing and a softmax layer if classifying
    if method == 'regress':
        layers.append(sknn.mlp.Layer("Linear"))
        mlp = sknn.mlp.Regressor(layers,n_iter=n_iter,batch_size=batch_size,learning_rule=learning_rule,
                                 learning_rate=learning_rate,learning_momentum=learning_momentum,
                                 regularize = regularize, weight_decay = weight_decay, n_stable=n_stable,
                                 valid_size=valid_size, callback={'on_epoch_finish': store_stats})
    if method == 'classify':
        layers.append(sknn.mlp.Layer("Softmax"))
        mlp = sknn.mlp.Classifier(layers,n_iter=n_iter,batch_size=batch_size,learning_rule=learning_rule,
                                 learning_rate=learning_rate,learning_momentum=learning_momentum,
                                 regularize = regularize, weight_decay = weight_decay, n_stable=n_stable,
                                 valid_size=valid_size, callback={'on_epoch_finish': store_stats})
    #Write nn string
    layerstr='_'.join([str(h)+f[0] for h,f in zip(hid_neur,actv_fnc)])
    mlp_str = method[0] + "_" +  layerstr + "_" + learning_rule[0:3] + "{}".format(str(learning_momentum) if learning_rule=='momentum'
                                                                      else str(learning_rate))
    if regularize is not None:<commit_msg>Fixed module import bugs when training NN<commit_after>import numpy as np
import sknn.mlp
import time
from sklearn import metrics, preprocessing
import matplotlib.pyplot as plt
import src.nnload as nnload
import pickle

# ---  BUILDING NEURAL NETS  --- #

def train_nn_wrapper(hidneur,
                     minlev=0.0,rainonly=False,data_dir='./data/'):
    #Import packages
    # Load data
    x, y, Pout, lat, lev, dlev, timestep = nnload.loaddata(data_dir + 'nntest.nc', minlev)
    # Ensure that input and outputs are lined up in time!
    x=x[1:,:]
    y=y[0:-1,:]
    # Print some statistics about rain and limit to when it's raining if True
    x, y, Pout = nnload.limitrain(x, y, Pout, rainonly)
    # Store when convection occurs
    cv = nnload.whenconvection(y)
    # Normalize input using the scikit-learn preprocessing tools
    scaler_x = preprocessing.MinMaxScaler(feature_range=(-1.0,1.0))
    scaler_y = preprocessing.MaxAbsScaler()
    # Randomly choose samples
    samples = np.random.choice(x.shape[0], x.shape[0], replace=False)
    x1,x2,x3    = nnload.pp(x,  samples, scaler_x)
    y1,y2,y3    = nnload.pp(y,  samples, scaler_y)
    cv1,cv2,cv3 = nnload.pp(cv, samples, None, scale_data=False)
    # Build, train, and save model
    r_mlp, r_str = build_nn('regress',['Rectifier'],[hidneur],
                                    'momentum',batch_size=100,n_stable=25)
                                #, regularize = 'L2', weight_decay = 0.0001)
    print(r_str)
    r_mlp, r_errors = train_nn(r_mlp,r_str,x1,y1,x2,y2)
    pickle.dump([r_mlp, r_str, r_errors], 
                open(data_dir + 'regressors/' + r_str + '.pkl', 'wb'))

error=[]
def store_stats( avg_train_error, best_train_error, avg_valid_error, best_valid_error,**_):
    error.append((avg_train_error, best_train_error, avg_valid_error, best_valid_error))

def build_nn(method,actv_fnc,hid_neur,learning_rule,batch_size=100,n_iter=None, n_stable=None,
             learning_rate=0.01,learning_momentum=0.9,
             regularize = None, weight_decay = None, valid_size = 0.25):
    """Builds a multi-layer perceptron via the scikit neural network interface"""
    # First build layers
    layers = [sknn.mlp.Layer(f,units=h) for f,h in zip(actv_fnc,hid_neur)]
    # Append a linear output layer if regressing and a softmax layer if classifying
    if method == 'regress':
        layers.append(sknn.mlp.Layer("Linear"))
        mlp = sknn.mlp.Regressor(layers,n_iter=n_iter,batch_size=batch_size,learning_rule=learning_rule,
                                 learning_rate=learning_rate,learning_momentum=learning_momentum,
                                 regularize = regularize, weight_decay = weight_decay, n_stable=n_stable,
                                 valid_size=valid_size, callback={'on_epoch_finish': store_stats})
    if method == 'classify':
        layers.append(sknn.mlp.Layer("Softmax"))
        mlp = sknn.mlp.Classifier(layers,n_iter=n_iter,batch_size=batch_size,learning_rule=learning_rule,
                                 learning_rate=learning_rate,learning_momentum=learning_momentum,
                                 regularize = regularize, weight_decay = weight_decay, n_stable=n_stable,
                                 valid_size=valid_size, callback={'on_epoch_finish': store_stats})
    #Write nn string
    layerstr='_'.join([str(h)+f[0] for h,f in zip(hid_neur,actv_fnc)])
    mlp_str = method[0] + "_" +  layerstr + "_" + learning_rule[0:3] + "{}".format(str(learning_momentum) if learning_rule=='momentum'
                                                                      else str(learning_rate))
    if regularize is not None:
{'fraction_non_alphanumeric': 0.11112626124897737, 'fraction_numerical': 0.013635124079629125, 'mean_word_length': 2.918269230769231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3896589', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2443, 'n_words': 493}