<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Create a neural network model file."""

import logging
import os
import tarfile
import random
import yaml
import h5py
import numpy
import theano


def get_parser():
    """Return the parser object for this script."""
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(description=__doc__,
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument("-t", "--type",
                        choices=["mlp"],
                        default="mlp",
                        dest="type",
                        help="which type of neural network do you want to "
                             "create?",
                        metavar="TYPE")
    parser.add_argument("-a", "--architecture",
                        dest="architecture",
                        help="""architecture of the network""",
                        default="160:500:369")
    parser.add_argument("-f", "--file",
                        dest="model_file",
                        help="write model file to MODEL_FILE",
                        metavar="MODEL_FILE")
    return parser


def main(nn_type, architecture, model_file):
    """Create a neural network file of ``nn_type`` with ``architecture``.
       Store it in ``model_file``.
       :param nn_type: A string, e.g. 'mlp'
       :param model_file: A path which should end with .tar. The created model
       will be written there.
    """
    if os.path.isfile(model_file):
        logging.error("'%s' already exists.", model_file)
        return
    if not model_file.endswith(".tar"):
        logging.error("'%s' does not end with '.tar'.", model_file)
        return
    logging.info("Create %s with a %s architecture...", nn_type, architecture)

    filenames = ["model.yml"]  # "input_semantics.csv", "output_semantics.csv"

    if nn_type == 'mlp':
        # Create layers by looking at 'architecture'
        layers = []
        layers_binary = []

        # TODO: the activation function could be here!
        neurons = list(map(int, architecture.split(':')))

        layer_counter = 0
        for neurons_b, neurons_a in zip(neurons, neurons[1:]):
            fan_in = neurons_a
            fan_out = neurons_b - 2
            init_weight = 4.0*numpy.sqrt(6.0/(fan_in+fan_out))
            W = [numpy.random.uniform(low=-init_weight,
                                      high=init_weight,
                                      size=neurons_a)
                 for j in range(neurons_b)]

            b = [random.random() for i in range(neurons_a)]
            # TODO: parse architecture string to allow arbitrary activation
            # functions
            if layer_counter + 2 == len(neurons):
                layer_activation = 'softmax'
            else:
                layer_activation = 'sigmoid'
            layers_binary.append({'W': numpy.array(W,
                                                   dtype=theano.config.floatX),<commit_msg>create.py: softmax->Softmax and sigmoid->Sigmoid as they represent class names<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Create a neural network model file."""

import logging
import os
import tarfile
import random
import yaml
import h5py
import numpy
import theano


def get_parser():
    """Return the parser object for this script."""
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(description=__doc__,
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument("-t", "--type",
                        choices=["mlp"],
                        default="mlp",
                        dest="type",
                        help="which type of neural network do you want to "
                             "create?",
                        metavar="TYPE")
    parser.add_argument("-a", "--architecture",
                        dest="architecture",
                        help="""architecture of the network""",
                        default="160:500:369")
    parser.add_argument("-f", "--file",
                        dest="model_file",
                        help="write model file to MODEL_FILE",
                        metavar="MODEL_FILE")
    return parser


def main(nn_type, architecture, model_file):
    """Create a neural network file of ``nn_type`` with ``architecture``.
       Store it in ``model_file``.
       :param nn_type: A string, e.g. 'mlp'
       :param model_file: A path which should end with .tar. The created model
       will be written there.
    """
    if os.path.isfile(model_file):
        logging.error("'%s' already exists.", model_file)
        return
    if not model_file.endswith(".tar"):
        logging.error("'%s' does not end with '.tar'.", model_file)
        return
    logging.info("Create %s with a %s architecture...", nn_type, architecture)

    filenames = ["model.yml"]  # "input_semantics.csv", "output_semantics.csv"

    if nn_type == 'mlp':
        # Create layers by looking at 'architecture'
        layers = []
        layers_binary = []

        # TODO: the activation function could be here!
        neurons = list(map(int, architecture.split(':')))

        layer_counter = 0
        for neurons_b, neurons_a in zip(neurons, neurons[1:]):
            fan_in = neurons_a
            fan_out = neurons_b - 2
            init_weight = 4.0*numpy.sqrt(6.0/(fan_in+fan_out))
            W = [numpy.random.uniform(low=-init_weight,
                                      high=init_weight,
                                      size=neurons_a)
                 for j in range(neurons_b)]

            b = [random.random() for i in range(neurons_a)]
            # TODO: parse architecture string to allow arbitrary activation
            # functions
            if layer_counter + 2 == len(neurons):
                layer_activation = 'Softmax'
            else:
                layer_activation = 'Sigmoid'
            layers_binary.append({'W': numpy.array(W,
                                                   dtype=theano.config.floatX),
{'fraction_non_alphanumeric': 0.10885458976441917, 'fraction_numerical': 0.006173842404549147, 'mean_word_length': 1.7679856115107915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5353190', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1593, 'n_words': 443}