<commit_before>#################################################################
# Code written by <NAME> (<EMAIL>)
# For bug report, please contact author using the email address
#################################################################

import sys, random
import numpy as np
import cPickle as pickle
from collections import OrderedDict
import argparse

import theano
import theano.tensor as T
from theano import config
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

from sklearn.metrics import roc_auc_score

_TEST_RATIO = 0.2
_VALIDATION_RATIO = 0.1

def unzip(zipped):
	new_params = OrderedDict()
	for key, value in zipped.iteritems():
		new_params[key] = value.get_value()
	return new_params

def numpy_floatX(data):
	return np.asarray(data, dtype=config.floatX)

def get_random_weight(dim1, dim2, left=-0.1, right=0.1):
	return np.random.uniform(left, right, (dim1, dim2)).astype(config.floatX)

def load_embedding(infile):
	Wemb = np.array(pickle.load(open(infile, 'rb'))).astype(config.floatX)
	return Wemb

def init_params(options):
	params = OrderedDict()
	timeFile = options['timeFile']
	embFile = options['embFile']
	embDimSize = options['embDimSize']
	inputDimSize = options['inputDimSize']
	alphaHiddenDimSize= options['alphaHiddenDimSize']
	betaHiddenDimSize= options['betaHiddenDimSize']
	numClass = options['numClass']

	if len(embFile) > 0: 
		print 'using external code embedding'
		params['W_emb'] = load_embedding(embFile)
		embDimSize = params['W_emb'].shape[1]
	else: 
		print 'using randomly initialized code embedding'
		params['W_emb'] = get_random_weight(inputDimSize, embDimSize)

	gruInputDimSize = embDimSize
	if len(timeFile) > 0: gruInputDimSize = embDimSize + 1

	params['W_gru_a'] = get_random_weight(gruInputDimSize, 3*alphaHiddenDimSize)
	params['U_gru_a'] = get_random_weight(alphaHiddenDimSize, 3*alphaHiddenDimSize)
	params['b_gru_a'] = np.zeros(3 * alphaHiddenDimSize).astype(config.floatX)

	params['W_gru_b'] = get_random_weight(gruInputDimSize, 3*betaHiddenDimSize)
	params['U_gru_b'] = get_random_weight(betaHiddenDimSize, 3*betaHiddenDimSize)
	params['b_gru_b'] = np.zeros(3 * betaHiddenDimSize).astype(config.floatX)

	params['w_alpha'] = get_random_weight(alphaHiddenDimSize, 1)
	params['b_alpha'] = np.zeros(1).astype(config.floatX)
	params['W_beta'] = get_random_weight(betaHiddenDimSize, embDimSize)
	params['b_beta'] = np.zeros(embDimSize).astype(config.floatX)
	params['w_output'] = get_random_weight(embDimSize, numClass)
	params['b_output'] = np.zeros(numClass).astype(config.floatX)

	return params

def load_params(options):
	return np.load(options['modelFile'])

def init_tparams(params, options):
	tparams = OrderedDict()
	for key, value in params.iteritems():
		if not options['embFineTune'] and key == 'W_emb': continue
		tparams[key] = theano.shared(value, name=key)
	return tparams

def dropout_layer(state_before, use_noise, trng, dropout_rate=0.5):
	proj = T.switch(use_noise, (state_before * trng.binomial(state_before.shape, p=dropout_rate, n=1, dtype=state_before.dtype)), 
state_before / dropout_rate)
	return proj

def _slice(_x, n, dim):
	if _x.ndim == 3:
		return _x[:, :, n*dim:(n+1)*dim]
	return _x[:, n*dim:(n+1)*dim]

def gru_layer(tparams, emb, name, hiddenDimSize):
	timesteps = emb.shape[0]<commit_msg>Modified the dropout behavior. It now operates in the same manner as TensorFlow's tf.nn.dropout.<commit_after>#################################################################
# Code written by <NAME> (<EMAIL>)
# For bug report, please contact author using the email address
#################################################################

import sys, random
import numpy as np
import cPickle as pickle
from collections import OrderedDict
import argparse

import theano
import theano.tensor as T
from theano import config
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

from sklearn.metrics import roc_auc_score

_TEST_RATIO = 0.2
_VALIDATION_RATIO = 0.1

def unzip(zipped):
	new_params = OrderedDict()
	for key, value in zipped.iteritems():
		new_params[key] = value.get_value()
	return new_params

def numpy_floatX(data):
	return np.asarray(data, dtype=config.floatX)

def get_random_weight(dim1, dim2, left=-0.1, right=0.1):
	return np.random.uniform(left, right, (dim1, dim2)).astype(config.floatX)

def load_embedding(infile):
	Wemb = np.array(pickle.load(open(infile, 'rb'))).astype(config.floatX)
	return Wemb

def init_params(options):
	params = OrderedDict()
	timeFile = options['timeFile']
	embFile = options['embFile']
	embDimSize = options['embDimSize']
	inputDimSize = options['inputDimSize']
	alphaHiddenDimSize= options['alphaHiddenDimSize']
	betaHiddenDimSize= options['betaHiddenDimSize']
	numClass = options['numClass']

	if len(embFile) > 0: 
		print 'using external code embedding'
		params['W_emb'] = load_embedding(embFile)
		embDimSize = params['W_emb'].shape[1]
	else: 
		print 'using randomly initialized code embedding'
		params['W_emb'] = get_random_weight(inputDimSize, embDimSize)

	gruInputDimSize = embDimSize
	if len(timeFile) > 0: gruInputDimSize = embDimSize + 1

	params['W_gru_a'] = get_random_weight(gruInputDimSize, 3*alphaHiddenDimSize)
	params['U_gru_a'] = get_random_weight(alphaHiddenDimSize, 3*alphaHiddenDimSize)
	params['b_gru_a'] = np.zeros(3 * alphaHiddenDimSize).astype(config.floatX)

	params['W_gru_b'] = get_random_weight(gruInputDimSize, 3*betaHiddenDimSize)
	params['U_gru_b'] = get_random_weight(betaHiddenDimSize, 3*betaHiddenDimSize)
	params['b_gru_b'] = np.zeros(3 * betaHiddenDimSize).astype(config.floatX)

	params['w_alpha'] = get_random_weight(alphaHiddenDimSize, 1)
	params['b_alpha'] = np.zeros(1).astype(config.floatX)
	params['W_beta'] = get_random_weight(betaHiddenDimSize, embDimSize)
	params['b_beta'] = np.zeros(embDimSize).astype(config.floatX)
	params['w_output'] = get_random_weight(embDimSize, numClass)
	params['b_output'] = np.zeros(numClass).astype(config.floatX)

	return params

def load_params(options):
	return np.load(options['modelFile'])

def init_tparams(params, options):
	tparams = OrderedDict()
	for key, value in params.iteritems():
		if not options['embFineTune'] and key == 'W_emb': continue
		tparams[key] = theano.shared(value, name=key)
	return tparams

def dropout_layer(state_before, use_noise, trng, dropout_rate=0.5):
	proj = T.switch(
                use_noise,
                state_before * trng.binomial(state_before.shape, p=dropout_rate, n=1, dtype=state_before.dtype) / (1.0 - dropout_rate),
                state_before)
	return proj

def _slice(_x, n, dim):
	if _x.ndim == 3:
		return _x[:, :, n*dim:(n+1)*dim]
	return _x[:, n*dim:(n+1)*dim]

def gru_layer(tparams, emb, name, hiddenDimSize):
	timesteps = emb.shape[0]
{'fraction_non_alphanumeric': 0.15811270522112114, 'fraction_numerical': 0.00946605531726076, 'mean_word_length': 6.5807174887892375, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '552991', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2346, 'n_words': 488}