<commit_before>"""
Trains a Neural Network predictor with binary output.

@copyright: The Broad Institute of MIT and Harvard 2015
"""

import argparse
import sys
import math
import pandas as pd
import numpy as np
from scipy.optimize import fmin_bfgs
import matplotlib.pyplot as plt
from utils import thetaMatrix, gradientArray, sigmoid, forwardProp, backwardProp, predict

"""Evaluates the cost function
"""
def cost(theta, X, y, N, L, S, K, gamma):
    M = X.shape[0]

    # The cost argument is a 1D-array that needs to be reshaped into the
    # parameter matrix for each layer:
    thetam = thetaMatrix(theta, N, L, S, K)

    h = np.zeros(M)
    terms = np.zeros(M)
    for i in range(0, M):
        a = forwardProp(X[i,:], thetam, L)
        h[i] = a[L]
        t0 = -y[i] * np.log(h[i]) if 0 < y[i] else 0
        t1 = -(1-y[i]) * np.log(1-h[i]) if y[i] < 1 else 0
        if math.isnan(t0) or math.isnan(t1):
            #print "NaN detected when calculating cost contribution of observation",i
            terms[i] = 10
        else:
            terms[i] = t0 + t1 

    # Regularization penalty
    penalty = (gamma/2) * np.sum(theta * theta)

    return terms.mean() + penalty;

"""Computes the gradient of the cost function<commit_msg>put back prefix and title functions in nnet/train<commit_after>"""
Trains a Neural Network predictor with binary output.

@copyright: The Broad Institute of MIT and Harvard 2015
"""

import argparse
import sys
import math
import pandas as pd
import numpy as np
from scipy.optimize import fmin_bfgs
import matplotlib.pyplot as plt
from utils import thetaMatrix, gradientArray, sigmoid, forwardProp, backwardProp, predict

def prefix():
    return "nnet"

def title():
    return "Neural Network"

"""Evaluates the cost function
"""
def cost(theta, X, y, N, L, S, K, gamma):
    M = X.shape[0]

    # The cost argument is a 1D-array that needs to be reshaped into the
    # parameter matrix for each layer:
    thetam = thetaMatrix(theta, N, L, S, K)

    h = np.zeros(M)
    terms = np.zeros(M)
    for i in range(0, M):
        a = forwardProp(X[i,:], thetam, L)
        h[i] = a[L]
        t0 = -y[i] * np.log(h[i]) if 0 < y[i] else 0
        t1 = -(1-y[i]) * np.log(1-h[i]) if y[i] < 1 else 0
        if math.isnan(t0) or math.isnan(t1):
            #print "NaN detected when calculating cost contribution of observation",i
            terms[i] = 10
        else:
            terms[i] = t0 + t1 

    # Regularization penalty
    penalty = (gamma/2) * np.sum(theta * theta)

    return terms.mean() + penalty;

"""Computes the gradient of the cost function
{'fraction_non_alphanumeric': 0.11401151631477927, 'fraction_numerical': 0.01689059500959693, 'mean_word_length': 2.809941520467836, 'pattern_counts': {'":': 0, '<': 7, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2074056', 'n_tokens_mistral': 922, 'n_tokens_neox': 873, 'n_words': 323}