Ejemplo n.º 1
0
    def train(self, dataset):
        #datasets = dataset.split_dataset(0.7)
        self.training_set = dataset
        #self.validation_set = datasets[1]

        #while True:
        for i in range(self.iterations):
            predictions = []
            for j in range(self.training_set.data.__len__()):
                prediction = self.__feed_forward(self.training_set.data[j])
                predictions.append(prediction)
                #print("PREDICTION: " + str(prediction))
                #print("REAL: " + str(self.training_set.target[j]))
                self.__set_targets(self.training_set.target[j])
                if prediction != self.training_set.target[j]:
                    #print("Bad prediction")
                    self.__back_propagate()
            accuracy = get_accuracy(predictions, self.training_set.target)
            self.accuracies.append(accuracy)
            #print("Current Accuracy: " + str(accuracy) + "%")
            self.training_set.randomize()
Ejemplo n.º 2
0
    def train(self, dataset):
        #datasets = dataset.split_dataset(0.7)
        self.training_set = dataset
        #self.validation_set = datasets[1]

        #while True:
        for i in range(self.iterations):
            predictions = []
            for j in range(self.training_set.data.__len__()):
                prediction = self.__feed_forward(self.training_set.data[j])
                predictions.append(prediction)
                #print("PREDICTION: " + str(prediction))
                #print("REAL: " + str(self.training_set.target[j]))
                self.__set_targets(self.training_set.target[j])
                if prediction != self.training_set.target[j]:
                    #print("Bad prediction")
                    self.__back_propagate()
            accuracy = get_accuracy(predictions, self.training_set.target)
            self.accuracies.append(accuracy)
            #print("Current Accuracy: " + str(accuracy) + "%")
            self.training_set.randomize()
Ejemplo n.º 3
0
Archivo: main.py Proyecto: g-mips/CS450
def main():
    while True:
        data_set_name = input(
            "Please provide the name of the data set you want to work with: ")

        # Load, Randomize, Normalize, Discretize Dataset
        data_set = Dataset()
        data_set.read_file_into_dataset(
            "C:\\Users\\Grant\\Documents\\School\\Winter 2016\\CS 450\\Neural\\"
            + data_set_name)
        data_set.randomize()
        data_set.data = normalize(data_set.data)
        #data_set.discretize()
        #print(data_set.data)

        data_set.set_missing_data()

        # Split Dataset
        split_percentage = 0.7
        data_sets = data_set.split_dataset(split_percentage)
        training_set = data_sets[0]
        testing_set = data_sets[1]

        # Create Custom Classifier, Train Dataset, Predict Target From Testing Set
        iterations = int(input("How many iterations do you want to do? "))
        layers = int(
            input("How many layers do you want in your neural network? "))
        num_nodes = []
        for i in range(layers):
            if i + 1 == layers:
                number = int(input("How many nodes on the output layer? "))
            else:
                number = int(
                    input("How many nodes on the " + str(i) + " layer? "))
            num_nodes.append(number)

        neuralNetwork = NeuralNetwork(iterations)
        neuralNetwork.create_layered_network(
            num_nodes, training_set.feature_names.__len__())
        #neuralNetwork.display_network()
        neuralNetwork.train(training_set)
        predictions = neuralNetwork.predict(testing_set)

        # Check Results
        my_accuracy = get_accuracy(predictions, testing_set.target)
        print("Accuracy: " + str(my_accuracy) + "%")

        # Compare To Existing Implementations
        layers_objs = []
        for i in range(layers):
            if i + 1 == layers:
                layers_objs.append(Layer("Softmax", units=num_nodes[i]))
            else:
                layers_objs.append(Layer("Sigmoid", units=num_nodes[i]))

        mlp_nn = Classifier(layers=layers_objs,
                            learning_rate=0.4,
                            n_iter=iterations)
        mlp_nn.fit(np.array(training_set.data), np.array(training_set.target))
        predictions = mlp_nn.predict(np.array(testing_set.data))

        mlp_nn_accuracy = get_accuracy(predictions, testing_set.target)
        print("NN Accuracy: " + str(mlp_nn_accuracy) + "%")

        create_csv_file(
            neuralNetwork.accuracies,
            "C:\\Users\\Grant\\Documents\\School\\Winter 2016\\CS 450\\Neural\\"
            + data_set_name + ".csv")
        # Do another or not
        toContinue = False

        while True:
            another = input("Do you want to examine another dataset? (y / n) ")

            if another != 'y' and another != 'n':
                print("Please provide you answer in a 'y' or 'n' format.")
            elif another == 'y':
                toContinue = True
                break
            else:
                toContinue = False
                break

        if not toContinue:
            break
Ejemplo n.º 4
0
Archivo: main.py Proyecto: gshawm/CS450
def main():
    while True:
        data_set_name = input("Please provide the name of the data set you want to work with: ")

        # Load, Randomize, Normalize, Discretize Dataset
        data_set = Dataset()
        data_set.read_file_into_dataset("C:\\Users\\Grant\\Documents\\School\\Winter 2016\\CS 450\\Neural\\" + data_set_name)
        data_set.randomize()
        data_set.data = normalize(data_set.data)
        #data_set.discretize()
        #print(data_set.data)

        data_set.set_missing_data()

        # Split Dataset
        split_percentage = 0.7
        data_sets    = data_set.split_dataset(split_percentage)
        training_set = data_sets[0]
        testing_set  = data_sets[1]

        # Create Custom Classifier, Train Dataset, Predict Target From Testing Set
        iterations = int(input("How many iterations do you want to do? "))
        layers = int(input("How many layers do you want in your neural network? "))
        num_nodes = []
        for i in range(layers):
            if i + 1 == layers:
                number = int(input("How many nodes on the output layer? "))
            else:
                number = int(input("How many nodes on the " + str(i) + " layer? "))
            num_nodes.append(number)

        neuralNetwork = NeuralNetwork(iterations)
        neuralNetwork.create_layered_network(num_nodes, training_set.feature_names.__len__())
        #neuralNetwork.display_network()
        neuralNetwork.train(training_set)
        predictions = neuralNetwork.predict(testing_set)

        # Check Results
        my_accuracy = get_accuracy(predictions, testing_set.target)
        print("Accuracy: " + str(my_accuracy) + "%")

        # Compare To Existing Implementations
        layers_objs = []
        for i in range(layers):
            if i + 1 == layers:
                layers_objs.append(Layer("Softmax", units=num_nodes[i]))
            else:
                layers_objs.append(Layer("Sigmoid", units=num_nodes[i]))

        mlp_nn = Classifier(layers=layers_objs, learning_rate=0.4, n_iter=iterations)
        mlp_nn.fit(np.array(training_set.data), np.array(training_set.target))
        predictions = mlp_nn.predict(np.array(testing_set.data))

        mlp_nn_accuracy = get_accuracy(predictions, testing_set.target)
        print("NN Accuracy: " + str(mlp_nn_accuracy) + "%")

        create_csv_file(neuralNetwork.accuracies, "C:\\Users\\Grant\\Documents\\School\\Winter 2016\\CS 450\\Neural\\" + data_set_name + ".csv")
        # Do another or not
        toContinue = False

        while True:
            another = input("Do you want to examine another dataset? (y / n) ")

            if another != 'y' and another != 'n':
                print("Please provide you answer in a 'y' or 'n' format.")
            elif another == 'y':
                toContinue = True
                break
            else:
                toContinue = False
                break

        if not toContinue:
            break