<commit_before>import tensorflow as tf
import numpy as np
import traceback
from .param import Variable, Parameterized, graph_key
from .tf_wraps import clip
from ._settings import settings
float_type = settings.dtypes.float_type
np_float_type = np.float32 if float_type is tf.float32 else np.float64

class MatBias(Parameterized):
    def __init__(self, nodes, n_layers=[1],
                    mean=0.0, stddev=1.0,
                    variable = tf.Variable,
                    collections = [graph_key.VARIABLES]):
        """
        A simple class that handles matrix and baias pair, w*x + b
        input:
        - nodes: 2-integer-element list or tuples indicating number of input and
               output for the matrix
        """
        assert(len(nodes)==2)
        Parameterized.__init__(self)
        # --- define matrices and biases ---
        self.w = variable(shape=[nodes[1], nodes[0]], n_layers=n_layers,
                            mean=mean, stddev=stddev,
                            collections=collections)
        self.b = variable(shape=[nodes[1], 1], n_layers=n_layers,
                            mean=mean, stddev=stddev,
                            collections=collections)

    def __call__(self, x):
        return clip(tf.batch_matmul(self.w, x) + self.b)

class NeuralNet(Parameterized):
    def __init__(self, nodes, n_layers = [],
                        mean=0.0, stddev=1.0,
                        variable_types = Variable,
                        neuron_types = tf.sigmoid,
                        collections = [graph_key.VARIABLES]):
        """
        nodes: list of nodes num of the neural net.
        n_layers: number of layers.
        variable typs: single or list of Variable object, one of
                        [tf.Variable or Variational]
        types: single or list of n.n. object.
        name: name of this neural net
        """
        Parameterized.__init__(self)
        self.nodes = nodes
        # --- variable types ----
        if not isinstance(variable_types, list):
            variable_types = [variable_types for _ in range(len(nodes)-1)]
        else:
            variable_types = variable_types
        # --- neuron types ----
        if not isinstance(neuron_types, list):
            self.neuron_types = [neuron_types for _ in range(len(nodes)-2)]
        else:
            self.neuron_types = neuron_types
        # --- define matrices and biases ---
        for i in range(len(nodes)-1):
            key = 'matbias' + str(i)
            name_matbias = self.name + str('.matbias')
            setattr(self, key, MatBias(nodes=[nodes[i], nodes[i+1]],
                            n_layers=n_layers,
                            mean=mean, stddev=stddev,
                            variable = variable_types[i],
                            collections=collections))

    def matmul(self, i, x):
        """
        Returns W*x + b for i-th layer
        This method should be executed in tf_mode
        """
        key = 'matbias' + str(i)
        matbias = getattr(self, key)
        return matbias(x)

    def __call__(self, x):
        """
        x: tf.tensor
        Returns Op for this neural net.
        This method should be executed in tf_mode
        """
        y = x
        for i in range(len(self.nodes)-2):
            typ = self.neuron_types[i]
            name_nn = None if self.name is None else self.name + str('.nn')+str(i)
            y = typ(self.matmul(i,y), name=name_nn)
        return self.matmul(len(self.nodes)-2, y)
<commit_msg>Indexing is enabled in nn.<commit_after>import tensorflow as tf
import numpy as np
import traceback
from .param import Variable, Parameterized, graph_key
from .tf_wraps import clip
from ._settings import settings
float_type = settings.dtypes.float_type
np_float_type = np.float32 if float_type is tf.float32 else np.float64

class MatBias(Parameterized):
    def __init__(self, nodes, n_layers=[1],
                    mean=0.0, stddev=1.0,
                    variable = tf.Variable,
                    collections = [graph_key.VARIABLES]):
        """
        A simple class that handles matrix and baias pair, w*x + b
        input:
        - nodes: 2-integer-element list or tuples indicating number of input and
               output for the matrix
        """
        assert(len(nodes)==2)
        Parameterized.__init__(self)
        # --- define matrices and biases ---
        self.w = variable(shape=[nodes[1], nodes[0]], n_layers=n_layers,
                            mean=mean, stddev=stddev,
                            collections=collections)
        self.b = variable(shape=[nodes[1], 1], n_layers=n_layers,
                            mean=mean, stddev=stddev,
                            collections=collections)

    def __call__(self, x):
        return clip(tf.batch_matmul(self.w, x) + self.b)

class NeuralNet(Parameterized):
    def __init__(self, nodes, n_layers = [],
                        mean=0.0, stddev=1.0,
                        variable_types = Variable,
                        neuron_types = tf.sigmoid,
                        collections = [graph_key.VARIABLES]):
        """
        nodes: list of nodes num of the neural net.
        n_layers: number of layers.
        variable typs: single or list of Variable object, one of
                        [tf.Variable or Variational]
        types: single or list of n.n. object.
        name: name of this neural net
        """
        Parameterized.__init__(self)
        self.nodes = nodes
        # --- variable types ----
        if not isinstance(variable_types, list):
            variable_types = [variable_types for _ in range(len(nodes)-1)]
        else:
            variable_types = variable_types
        # --- neuron types ----
        if not isinstance(neuron_types, list):
            self.neuron_types = [neuron_types for _ in range(len(nodes)-2)]
        else:
            self.neuron_types = neuron_types
        # --- define matrices and biases ---
        self._matbias_list = []
        for i in range(len(nodes)-1):
            matbias = MatBias(nodes=[nodes[i], nodes[i+1]],
                            n_layers=n_layers,
                            mean=mean, stddev=stddev,
                            variable = variable_types[i],
                            collections=collections)
            self._matbias_list.append(matbias)
            key = 'matbias' + str(i)
            name_matbias = self.name + str('.matbias')
            setattr(self, key, matbias)

    def __call__(self, x):
        """
        x: tf.tensor
        Returns Op for this neural net.
        This method should be executed in tf_mode
        """
        y = x
        for i in range(len(self.nodes)-2):
            typ = self.neuron_types[i]
            name_nn = None if self.name is None else self.name + str('.nn')+str(i)
            y = typ(self._matbias_list[i](y), name=name_nn)
        return self._matbias_list[-1](y)

    def __getitem__(self, i):
        return self._matbias_list[i]

{'fraction_non_alphanumeric': 0.09903244166192374, 'fraction_numerical': 0.007683551508252703, 'mean_word_length': 1.7013835511145272, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6456417', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1951, 'n_words': 533}