<commit_before>import os
import sys
import numpy as np
import theano
import theano.tensor as T
import lasagne
import time

from os.path import dirname
from lasagne.regularization import l2, l1
from lib.utils.data_utils import *

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


def predict_fn(input_var, test_prediction):
    """Function to predict network output"""

    return theano.function([input_var], T.argmax(test_prediction, axis=1),
                           allow_input_downcast=True)
#------------------------------------------------------------------------------#


def grad_fn(input_var, target_var, test_prediction):
    """
    Function to calculate gradient at given data point. Note that the gradient
    in the FSG method attack is taken with respect to the true label.
    The recently discovered 'label leaking' phenomenon can be corrected by
    taking the loss with respect to the predicted label.
    """

    test_loss = loss_fn(test_prediction, target_var)
    req_gradient = T.grad(test_loss, input_var)
    return theano.function([input_var, target_var], req_gradient,
                           allow_input_downcast=True)
#------------------------------------------------------------------------------#


def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
    """Function to go over minibatches required for training"""

    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
            excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]
#------------------------------------------------------------------------------#


def loss_fn(model_predict, target_var, reg=None, network=None, layers=None):
    """
    Create a loss expression for training, i.e., a scalar objective we want
    to minimize (for our multi-class problem, it is the cross-entropy loss)
    """

    loss_temp = lasagne.objectives.categorical_crossentropy(model_predict,
                                                            target_var)
    loss_temp = loss_temp.mean()
    # Optional regularization
    # layers={layer_1:1e-7,layer_2:1e-7,network:1e-7}
    # l2_penalty=lasagne.regularization.regularize_layer_params_weighted(layers,
    # l2)
    if reg == 'l2':
        if layers is not None:
            layer_1 = layers[0]
            layer_2 = layers[1]
            layer_dict = {layer_1: 1e-7}
            l2_penalty = lasagne.regularization
                .regularize_layer_params_weighted(layer_dict, l2)
        else:<commit_msg>Update theano_utils.py<commit_after>import os
import sys
import numpy as np
import theano
import theano.tensor as T
import lasagne
import time

from os.path import dirname
from lasagne.regularization import l2, l1
from lib.utils.data_utils import *

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


def predict_fn(input_var, test_prediction):
    """Function to predict network output"""

    return theano.function([input_var], T.argmax(test_prediction, axis=1),
                           allow_input_downcast=True)
#------------------------------------------------------------------------------#


def grad_fn(input_var, target_var, test_prediction):
    """
    Function to calculate gradient at given data point. Note that the gradient
    in the FSG method attack is taken with respect to the true label.
    The recently discovered 'label leaking' phenomenon can be corrected by
    taking the loss with respect to the predicted label.
    """

    test_loss = loss_fn(test_prediction, target_var)
    req_gradient = T.grad(test_loss, input_var)
    return theano.function([input_var, target_var], req_gradient,
                           allow_input_downcast=True)
#------------------------------------------------------------------------------#


def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
    """Function to go over minibatches required for training"""

    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
            excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]
#------------------------------------------------------------------------------#


def loss_fn(model_predict, target_var, reg=None, network=None, layers=None):
    """
    Create a loss expression for training, i.e., a scalar objective we want
    to minimize (for our multi-class problem, it is the cross-entropy loss)
    """

    loss_temp = lasagne.objectives.categorical_crossentropy(model_predict,
                                                            target_var)
    loss_temp = loss_temp.mean()
    # Optional regularization
    # layers={layer_1:1e-7,layer_2:1e-7,network:1e-7}
    # l2_penalty=lasagne.regularization.regularize_layer_params_weighted(layers,
    # l2)
    if reg == 'l2':
        if layers is not None:
            layer_1 = layers[0]
            layer_2 = layers[1]
            layer_dict = {layer_1: 1e-7}
            l2_penalty = lasagne.regularization \
                .regularize_layer_params_weighted(layer_dict, l2)
        else:
{'fraction_non_alphanumeric': 0.1874441065998927, 'fraction_numerical': 0.008942944017170452, 'mean_word_length': 1.9323544834819089, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4521275', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1465, 'n_words': 406}