def MLP(data, batch_size): #data = load_iris() # list1 = list(data.iloc[0,:-2]) # print(list1) # target = list(data.iloc[0,4:]) # print(target) mlp = MultiLayerPerceptron(_layers=1, _bias=1, _inputs=4, _outputs=2, _learningRate=0.01, _maxIter=200) for i in range(mlp.maxIter): mlp.totalError = 0 data = shuffle(data) #print(data.data[0]) for j in range(len(data)): row = list(data.iloc[j, :-mlp.outputs]) target = list(data.iloc[j, mlp.inputs:]) mlp.estimate(row) #mlp.estimate(data.data[j]) mlp.updateDeltaWeight(target) if j % batch_size == 0: mlp.updateWeight() mlp.updateTotalError(target) # print("at iteration ", i, " error = " + str(mlp.totalError)) if j % batch_size != 0: mlp.updateWeight() mlp.updateTotalError(target) # print("at iteration ", i, " error = " + str(mlp.totalError)) if (mlp.totalError < 0.01): break return mlp
def get_trained_model(self): if self.model_name == 'LDA': self.model = LDA(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() elif self.model_name == 'LR': self.model = LogisticReg(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() elif self.model_name == 'MLP': self.model = MultiLayerPerceptron(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() elif self.model_name == 'SVM': self.model = SupportVectorMachine(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() return self.model.get_model()
def get_trained_model(self): if self.model_name == 'MLP': self.model = MultiLayerPerceptron(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() elif self.model_name == 'LR': self.model = LogisticReg(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() elif self.model_name == 'DT': self.model = DecisionTree(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() elif self.model_name == 'XGB': self.model = XGBoost(self.x_train, self.y_train, self.x_test, self.y_test) self.model.train_model() return self.model.get_model()
from MultiLayerPerceptron import MultiLayerPerceptron import numpy as np from matplotlib import pyplot as plt output_file = "/home/hugh/connect_comp/ProgrammingAssignment/Task1.txt" mlp = MultiLayerPerceptron((2, 2, 1), hidden_activation="sigmoid", max_iters=10000, linear_factor=0.2, learning_rate=0.7, verbose=(True, 1000,), output_activation="linear", output_file=output_file) x = np.array([[1, 0], [0, 1], [0, 0], [1, 1]]) y = np.array([1, 1, 0, 0]) errors = mlp.fit(x, y) plt.figure() plt.plot(errors, color="blue", label="Sigmoid + Linear") plt.title("Mean Squared Error Over Time") plt.xlabel("$Epochs$") plt.ylabel("$Error$") plt.show() #plt.savefig("/home/hugh/connect_comp/ProgrammingAssignment/Task1.png") print("1, 1") print(mlp.predict(np.array([1, 1]))) print("0, 0") print(mlp.predict(np.array([0, 0]))) print("0, 1") print(mlp.predict(np.array([0, 1]))) print("1, 0") print(mlp.predict(np.array([1, 0])))
# split into train test split index = np.random.rand(len(y)) < 0.9 x_train = np.array(x[index]) y_train = np.array(y[index]) x_test = np.array(x[~index]) y_test = np.array(y[~index]) mlp = MultiLayerPerceptron(( len(x.columns), 20, len(y.unique()), ), max_iters=200, hidden_activation="relu", output_activation="softmax", linear_factor=1, learning_rate=0.01, verbose=(True, 100), weight_update=1, loss="crossentropy") errors = mlp.fit(x_train, y_train) plt.plot(errors, color="blue", label="Relu + Softmax") plt.title("Cross Entropy Error Over Time") plt.xlabel("$Epochs$") plt.ylabel("$Error$") mlp = MultiLayerPerceptron((
from TrainingPattern import TrainingPattern from MultiLayerPerceptron import MultiLayerPerceptron if __name__ == "__main__": trainingPatterns = [ TrainingPattern([1,0,0,0], [1,0,0,0]), TrainingPattern([0,1,0,0], [0,1,0,0]), TrainingPattern([0,0,1,0], [0,0,1,0]), TrainingPattern([0,0,0,1], [0,0,0,1]) ] mlp = MultiLayerPerceptron(inputLayerSize=4, hiddenLayersSize=[2], outputLayerSize=4, epochs=10000, learningStep=0.5, biasNeuron=True) mlp.train(trainingPatterns) for tp in trainingPatterns: out = mlp.calculateNetworkOutput(tp) print("Actual output : {} mlp returned : {}".format(tp.outputs, out))
output_vector.append(combine(unit)) input_vector = np.array(input_vector) output_vector = np.array(output_vector) index = np.random.rand(len(input_vector)) < 0.8 x_train = input_vector[index] y_train = output_vector[index] x_test = input_vector[~index] y_test = output_vector[~index] mlp = MultiLayerPerceptron((4, 5, 1), hidden_activation="sigmoid", max_iters=1000, linear_factor=1, learning_rate=0.3, verbose=(True, 100), weight_update=10) errors = mlp.fit(x_train, y_train) plt.plot(errors, color="blue", label="Sigmoid + Linear") plt.title("Mean Squared Error Over Time") plt.xlabel("$Epochs$") plt.ylabel("$Error$") plt.legend() #plt.show() plt.savefig("/home/hugh/connect_comp/ProgrammingAssignment/Task2.png")