<commit_before>#!/usr/bin/env python

import ForwardNN
import numpy as np
import argparse
import os.path

np.set_printoptions(threshold='nan')

#Reads in file as an array of arrays.
def readFile(name):
    fileX = []
    for line in name:
        fileX.append([float(y) for y in line.split(' ')])
    name.close()
    return np.array(fileX)

def read_params(name):
    prev = []
    firstline = name.readline()
    layer = ()
    for arg in firstline:
        layer = layer + (int(arg),)
    prev.append(layer)
    prev.append(np.fromstring(name.readline(), dtype=float, sep=" "))
    name.close()
    return prev

# In place of a main, as python lacks one. Call run to read in the training data and train,
# As well as run a monte carlo to find a satisfactory network. Then it will sit in a loop
# waiting for input commands.
def visual(argv):
    print argv
    X = readFile(argv.input[0])
    Y = readFile(argv.output[0])

    if not X.shape[0] == Y.shape[0]:
        print "ERROR: Need equal number of Inputs and Outputs"
        return
    else:
        print str(X.shape[0]) + " data points given."

    #Creates a trainer and network, created as a X input to a set of hidden to Y output.
    print("This neural network will take in " + str(X.shape[1]) +
     " inputs and will output " + str(Y.shape[1]) + " floats.")

    layerNodes = None
    params = None

    if not argv.params == None:
        params = read_params(argv.params[0])
        if params[0][0] == X.shape[1] and params[0][len(params[0])] == Y.shape[1]:
            layerNodes = params[0]
            NN = ForwardNN.ForwardNN(layerNodes)
            NN.set_params(params[1])
        else:
            print "ERROR: Invalid layers given by parameters"
            return
    elif not argv.layers == None:
        layerNodes = (X.shape[1],) + tuple(argv.layers,) + (Y.shape[1],)
        NN = ForwardNN.ForwardNN(layerNodes)
        print("Neural Network Layers: " + str(layerNodes))
    else:
        layerNodes = (X.shape[1],) + (Y.shape[1],)
        NN = ForwardNN.ForwardNN(layerNodes)
        print("Neural Network Layers: " + str(layerNodes))


    # As the print states, runs a forward operation on the network with it's randomly generated weights.
    print("Now printing an initial run on the " + str(X.shape[0]) + " base inputs and their cost function.")
    print(NN.forward(X))
    print("Cost Function: " + NN.cost_function_type(X, Y))
    print("Cost: " + str(NN.cost_function(X, Y)))

    #Trains the network using the trainer and test data.
    max_count = argv.cycle[0]

    bestNN = ForwardNN.ForwardNN(layerNodes)
    bestNN.set_params(NN.get_params())

    #This is our monte carlo. Continually trains networks until one statisfies our conditions.
    if max_count > 0:
        count = 0
        while np.isnan(bestNN.cost_function(X, Y)) or count < max_count:
            train = ForwardNN.Trainer(NN)
            train.train(X, Y)
            if bestNN.cost_function(X, Y) > NN.cost_function(X, Y):
                bestNN = ForwardNN.ForwardNN(layerNodes)
                bestNN.set_params(NN.get_params())
                print("New cost: " + str(bestNN.cost_function(X, Y)))
            count += 1
            NN = ForwardNN.ForwardNN(layerNodes)
            print("Current cycle: " + str(count))

        NN.set_params(bestNN.get_params())
    #Print the results of the training and monte carlo.
    print("Now printing the final match results.")
    print(np.around(NN.forward(X), decimals=2))
    print("Cost function: " + str(NN.cost_function(X, Y)))

    #Input control loop.
    while 1:
        ans = raw_input("input a command: forward <file>, save <file>, or exit: ")

        # When a user inputs forward and a file, read in the file and run forward using it.
        if ans.split(' ')[0] == 'forward' and len(ans.split(' ')) > 1:
                print ans.split(' ')
                input = readFile(ans.split(' ')[1])
                print(np.around(NN.forward(input), decimals=2))

                #Additional checker tool, allows for a forwarded file to be added to test data.
                valid = raw_input("Is this the expected output? (y/n): ")
                if valid == "n":
                    actualOutput = raw_input("What is the correct output: ")
                    try:
                        actualOutput = int(actualOutput)<commit_msg>Edited comments in script.py<commit_after>#!/usr/bin/env python

import ForwardNN
import numpy as np
import argparse
import os.path

np.set_printoptions(threshold='nan')

# Reads in file as an array of arrays.
def readFile(name):
    fileX = []
    for line in name:
        fileX.append([float(y) for y in line.split(' ')])
    name.close()
    return np.array(fileX)

def read_params(name):
    prev = []
    firstline = name.readline()
    layer = ()
    for arg in firstline:
        layer = layer + (int(arg),)
    prev.append(layer)
    prev.append(np.fromstring(name.readline(), dtype=float, sep=" "))
    name.close()
    return prev

# In place of a main, as python lacks one. Call run to read in the training data and train,
# As well as run a monte carlo to find a satisfactory network. Then it will sit in a loop
# waiting for input commands.
def visual(argv):
    print argv
    X = readFile(argv.input[0])
    Y = readFile(argv.output[0])

    if not X.shape[0] == Y.shape[0]:
        print "ERROR: Need equal number of Inputs and Outputs"
        return
    else:
        print str(X.shape[0]) + " data points given."

    # Creates a trainer and network, created as a X input to a set of hidden to Y output.
    print("This neural network will take in " + str(X.shape[1]) +
     " inputs and will output " + str(Y.shape[1]) + " floats.")

    layerNodes = None
    params = None

    if not argv.params == None:
        params = read_params(argv.params[0])
        if params[0][0] == X.shape[1] and params[0][len(params[0])] == Y.shape[1]:
            layerNodes = params[0]
            NN = ForwardNN.ForwardNN(layerNodes)
            NN.set_params(params[1])
        else:
            print "ERROR: Invalid layers given by parameters"
            return
    elif not argv.layers == None:
        layerNodes = (X.shape[1],) + tuple(argv.layers,) + (Y.shape[1],)
        NN = ForwardNN.ForwardNN(layerNodes)
        print("Neural Network Layers: " + str(layerNodes))
    else:
        layerNodes = (X.shape[1],) + (Y.shape[1],)
        NN = ForwardNN.ForwardNN(layerNodes)
        print("Neural Network Layers: " + str(layerNodes))


    # As the print states, runs a forward operation on the network with it's randomly generated weights.
    print("Now printing an initial run on the " + str(X.shape[0]) + " base inputs and their cost function.")
    print(NN.forward(X))
    print("Cost Function: " + NN.cost_function_type(X, Y))
    print("Cost: " + str(NN.cost_function(X, Y)))

    # Trains the network using the trainer and test data.
    max_count = argv.cycle[0]

    bestNN = ForwardNN.ForwardNN(layerNodes)
    bestNN.set_params(NN.get_params())

    # This is our monte carlo. Continually trains networks until one statisfies our conditions.
    if max_count > 0:
        count = 0
        while np.isnan(bestNN.cost_function(X, Y)) or count < max_count:
            train = ForwardNN.Trainer(NN)
            train.train(X, Y)
            if bestNN.cost_function(X, Y) > NN.cost_function(X, Y):
                bestNN = ForwardNN.ForwardNN(layerNodes)
                bestNN.set_params(NN.get_params())
                print("New cost: " + str(bestNN.cost_function(X, Y)))
            count += 1
            NN = ForwardNN.ForwardNN(layerNodes)
            print("Current cycle: " + str(count))

        NN.set_params(bestNN.get_params())
    # Print the results of the training and monte carlo.
    print("Now printing the final match results.")
    print(np.around(NN.forward(X), decimals=2))
    print("Cost function: " + str(NN.cost_function(X, Y)))

    # Input control loop.
    while 1:
        ans = raw_input("input a command: forward <file>, save <file>, or exit: ")

        # When a user inputs forward and a file, read in the file and run forward using it.
        if ans.split(' ')[0] == 'forward' and len(ans.split(' ')) > 1:
                print ans.split(' ')
                input = readFile(ans.split(' ')[1])
                print(np.around(NN.forward(input), decimals=2))

                # Additional checker tool, allows for a forwarded file to be added to test data.
                valid = raw_input("Is this the expected output? (y/n): ")
                if valid == "n":
                    actualOutput = raw_input("What is the correct output: ")
                    try:
                        actualOutput = int(actualOutput)
{'fraction_non_alphanumeric': 0.12016922021495541, 'fraction_numerical': 0.007088954950834668, 'mean_word_length': 2.4355852317360567, 'pattern_counts': {'":': 2, '<': 9, '<?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': '553029', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2618, 'n_words': 824}