<commit_before>from collections import namedtuple
import itertools

import numpy as np
import tensorflow as tf

from model import Model


Layer = namedtuple('Layer', ('name', 'nodes', 'activation'))

class GridSearch():
    """Tune hyperparameters and neural netowrk hidden layer architecture"""
    def __init__(self, d_hyperparam_grid, d_layer_grid):
        """Implement grid search to fine-tune hyperparams and layer architecture
        according to cross-validation values of associated neural nets

        Args: d_hyperparam_grid (dictionary) - hyperparameter names (string)
                as keys + corresponding potential settings (list) as values
              d_layer_grid (dictionary) - containing the following items...
                'activation': list of tensorflow activation functions, &
                'hidden_nodes': list of lists of hidden layer architectures,
                i.e. nodes per layer per config
        """
        DEFAULTS = {'learning_rate': [0.05],
                    'dropout': [1], # no dropout - i.e. keep prob 1
                    'lambda_l2_reg': [0], # no L2 regularization
                    'n_minibatch': [100],
                    'epochs': [200]}
        self.d_hyperparam_grid = DEFAULTS.copy()
        # any DEFAULT items with key duplicated by d_hyperparam_grid will be replaced
        self.d_hyperparam_grid.update(**d_hyperparam_grid)

        self.d_layer_grid = d_layer_grid

    def iterCombos(self):
        """Generate all combinations of hyperparameter and layer configs

        Returns: generator yielding tuple(dictionary, list of Layers)
          corresponding, respectively, to hyperparams, architecture
        """
        # yields generators of tuples of (k, v) pairs for each hyperparameter + value
        hyperparam_tuples = (itertools.izip(itertools.repeat(k), v)
                             for k, v in self.d_hyperparam_grid.iteritems())
        # all combinatorial hyperparameter settings
        HYPERPARAMS = (dict(params) for params in
                       itertools.product(*hyperparam_tuples))
<commit_msg>fix imports and reorg defaults as class attributes<commit_after>from collections import namedtuple
import itertools

import numpy as np
import tensorflow as tf

from model import Model, Layer


class GridSearch():
    """Tune hyperparameters and neural network hidden layer architecture"""

    DEFAULTS = {'learning_rate': [0.05],
                'dropout': [1], # no dropout - i.e. keep prob 1
                'lambda_l2_reg': [0], # no L2 regularization
                'n_minibatch': [100],
                'epochs': [200]}

    def __init__(self, d_hyperparam_grid, d_layer_grid):
        """Implement grid search to fine-tune hyperparams and layer architecture
        according to cross-validation values of associated neural nets

        Args: d_hyperparam_grid (dictionary) - hyperparameter names (string)
                as keys + corresponding potential settings (list) as values
              d_layer_grid (dictionary) - containing the following items...
                'activation': list of tensorflow activation functions, &
                'hidden_nodes': list of lists of hidden layer architectures,
                i.e. nodes per layer per config
        """
        self.d_hyperparam_grid = self.DEFAULTS.copy()
        # any defaults with key duplicated by d_hyperparam_grid will be replaced
        self.d_hyperparam_grid.update(**d_hyperparam_grid)

        self.d_layer_grid = d_layer_grid

    def iterCombos(self):
        """Generate all combinations of hyperparameter and layer configs

        Returns: generator yielding tuple(dictionary, list of Layers)
          corresponding, respectively, to hyperparams, architecture
        """
        # yields generators of tuples of (k, v) pairs for each hyperparameter + value
        hyperparam_tuples = (itertools.izip(itertools.repeat(k), v)
                             for k, v in self.d_hyperparam_grid.iteritems())
        # all combinatorial hyperparameter settings
        HYPERPARAMS = (dict(params) for params in
                       itertools.product(*hyperparam_tuples))

{'fraction_non_alphanumeric': 0.07603464870067372, 'fraction_numerical': 0.006737247353224254, 'mean_word_length': 2.552991452991453, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2424812', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1033, 'n_words': 366}