<commit_before>from keras.layers import Layer, InputSpec
from keras.models import Sequential
from keras import initializations, regularizers
from keras.utils.layer_utils import layer_from_config
from keras import backend as K
from inspect import getargspec
import numpy as np
from . import backend


'''Provides a simpler API for building complex recurrent neural networks using Keras.

The RNN logic is written inside RNNCells, which are added sequentially to a RecurrentContainer.
A RecurrentContainer behaves similar to a Recurrent layer in Keras, and accepts arguments like
return_sequences, unroll, stateful, etc [See Keras Recurrent docstring]
The .add() method of a RecurrentContainer is used to add RNNCells and other layers to it. Each
element in the input sequence passes through the layers in the RecurrentContainer in the order
in which they were added.
'''


__author__ = "<NAME>"
__copyright__ = "Copyright 2016, datalog.ai"
__credits__ = ["<NAME>", "<NAME>"]
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"


_backend = getattr(K, K.backend() + '_backend')


class learning_phase(object):

	def __init__(self, value):
		self.value = value

	def __enter__(self):
		self.learning_phase_place_holder = _backend._LEARNING_PHASE
		_backend._LEARNING_PHASE = self.value

	def __exit__(self, *args):
		_backend._LEARNING_PHASE = self.learning_phase_place_holder


if K.backend() == 'theano':
	rnn = backend.rnn
else:
	rnn = lambda *args, **kwargs: list(K.rnn(*args, **kwargs)) + [[]]


def _isRNN(layer):
	return issubclass(layer.__class__, RNNCell)

def _get_first_timestep(x):
	slices = [slice(None)] * K.ndim(x)
	slices[1] = 0
	return x[slices]

def _get_last_timestep(x):
	ndim = K.ndim(x)
	if K.backend() == 'tensorflow':
		import tensorflow as tf
		slice_begin = tf.pack([0, tf.shape(x)[1] - 1] + [0] * (ndim - 2))
		slice_size = tf.pack([-1, 1] + [-1] * (ndim - 2))
		last_output = tf.slice(x, slice_begin, slice_size)
		last_output = tf.squeeze(last_output, [1])
		return last_output
	else:
		return x[:, -1]


class weight(object):

	def __init__(self, value, init='glorot_uniform', regularizer=None, trainable=True, name=None):
		if type(value) == int:
			value = (value,)
		if type(value) in [tuple, list]:
			if type(init) == str:
				init = initializations.get(init)
			self.value = init(value, name=name)
		elif 'numpy' in str(type(value)):
			self.value = K.variable(value, name=name)
		else:
			self.value = value
		if type(regularizer) == str:
			regularizer = regularizers.get(regularizer)
		self.regularizer = regularizer
		self.trainable = trainable


class RNNCell(Layer):

	def __init__(self, **kwargs):
		if 'input_dim' in kwargs:
			kwargs['input_shape'] = (kwargs['input_dim'],)
			del kwargs['input_dim']
		if 'output_dim' in kwargs:
			self.output_dim = kwargs['output_dim']
			del kwargs['output_dim']
		self.initial_states = {}
		super(RNNCell, self).__init__(**kwargs)

	def _step(self, x, states):
		args = [x, states]
		if hasattr(self, 'weights'):
			args += [self.weights]
		if hasattr(self, 'constants'):
			args += [self.constants]
		args = args[:len(getargspec(self.step).args)]
		return self.step(*args)

	def build(self, input_shape):
		self.input_spec = [InputSpec(shape=input_shape)]
		super(RNNCell, self).build(input_shape)

	@property
	def weights(self):
		w = []
		if hasattr(self, 'trainable_weights'):
			w += self.trainable_weights
		if hasattr(self, 'non_trainable_weights'):
			w += self.non_trainable_weights
		return w

	def get_layer(self, **kwargs):
		rc = RecurrentContainer(**kwargs)
		rc.add(self)
		return rc<commit_msg>bug fix<commit_after>from keras.layers import Layer, InputSpec
from keras.models import Sequential
from keras import initializations, regularizers
from keras.utils.layer_utils import layer_from_config
from keras import backend as K
from inspect import getargspec
import numpy as np
from . import backend


'''Provides a simpler API for building complex recurrent neural networks using Keras.

The RNN logic is written inside RNNCells, which are added sequentially to a RecurrentContainer.
A RecurrentContainer behaves similar to a Recurrent layer in Keras, and accepts arguments like
return_sequences, unroll, stateful, etc [See Keras Recurrent docstring]
The .add() method of a RecurrentContainer is used to add RNNCells and other layers to it. Each
element in the input sequence passes through the layers in the RecurrentContainer in the order
in which they were added.
'''


__author__ = "<NAME>"
__copyright__ = "Copyright 2016, datalog.ai"
__credits__ = ["<NAME>", "<NAME>"]
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Production"


_backend = getattr(K, K.backend() + '_backend')


class learning_phase(object):

	def __init__(self, value):
		self.value = value

	def __enter__(self):
		self.learning_phase_place_holder = _backend._LEARNING_PHASE
		_backend._LEARNING_PHASE = self.value

	def __exit__(self, *args):
		_backend._LEARNING_PHASE = self.learning_phase_place_holder


if K.backend() == 'theano':
	rnn = backend.rnn
else:
	rnn = lambda *args, **kwargs: list(K.rnn(*args, **kwargs)) + [[]]


def _isRNN(layer):
	return issubclass(layer.__class__, RNNCell)

def _get_first_timestep(x):
	slices = [slice(None)] * K.ndim(x)
	slices[1] = 0
	return x[slices]

def _get_last_timestep(x):
	ndim = K.ndim(x)
	if K.backend() == 'tensorflow':
		import tensorflow as tf
		slice_begin = tf.pack([0, tf.shape(x)[1] - 1] + [0] * (ndim - 2))
		slice_size = tf.pack([-1, 1] + [-1] * (ndim - 2))
		last_output = tf.slice(x, slice_begin, slice_size)
		last_output = tf.squeeze(last_output, [1])
		return last_output
	else:
		return x[:, -1]


class weight(object):

	def __init__(self, value, init='glorot_uniform', regularizer=None, trainable=True, name=None):
		if type(value) == int:
			value = (value,)
		if type(value) in [tuple, list]:
			if type(init) == str:
				init = initializations.get(init)
			self.value = init(value, name=name)
		elif 'numpy' in str(type(value)):
			self.value = K.variable(value, name=name)
		else:
			self.value = value
		if type(regularizer) == str:
			regularizer = regularizers.get(regularizer)
		self.regularizer = regularizer
		self.trainable = trainable


class RNNCell(Layer):

	def __init__(self, **kwargs):
		if 'input_dim' in kwargs:
			kwargs['input_shape'] = (kwargs['input_dim'],)
			del kwargs['input_dim']
		if 'output_dim' in kwargs:
			self.output_dim = kwargs['output_dim']
			del kwargs['output_dim']
		self.initial_states = {}
		super(RNNCell, self).__init__(**kwargs)

	def _step(self, x, states):
		args = [x, states]
		if hasattr(self, 'weights'):
			args += [self.weights]
		if hasattr(self, 'constants'):
			args += [self.constants]
		args = args[:len(getargspec(self.step).args)]
		return self.step(*args)

	def call(self, x, mask=None):
		input_ndim = K.ndim(x)
		output_ndim = len(self.get_output_shape_for((10,) * input_ndim))
		return K.zeros((10,) * output_ndim)

	def build(self, input_shape):
		self.input_spec = [InputSpec(shape=input_shape)]
		super(RNNCell, self).build(input_shape)

	@property
	def weights(self):
		w = []
		if hasattr(self, 'trainable_weights'):
			w += self.trainable_weights
		if hasattr(self, 'non_trainable_weights'):
			w += self.non_trainable_weights
		return w

	def get_layer(self, **kwargs):
		rc = RecurrentContainer(**kwargs)
		rc.add(self)
		return rc
{'fraction_non_alphanumeric': 0.12821543408360128, 'fraction_numerical': 0.005894962486602358, 'mean_word_length': 4.773395204949729, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '320764', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2585, 'n_words': 704}