def fit(self, X, Y, optimizer=GradientDescent, epochs=25, zeros=False, save_best=False): self.weights = generate_weights(X.shape[1], 1, zeros=zeros) self.best_weights = {"weights": None, "loss": float('inf')} print("Starting training with loss:", optimizer.loss_func.loss(X, Y, self.weights)) for epoch in range(1, epochs + 1): print("======================================") print("epoch:", epoch) self.weights = optimizer.iterate(X, Y, self.weights) epoch_loss = optimizer.loss_func.loss(X, Y, self.weights) if save_best and epoch_loss < self.best_weights["loss"]: print("updating best weights (loss: {})".format(epoch_loss)) self.best_weights['weights'] = self.weights self.best_weights['loss'] = epoch_loss version = "model_best_" + datetime.now().strftime(DATE_FORMAT) print("Saving best model version: ", version) self.save(version) print("Loss in this step: ", epoch_loss) version = "model_final_" + datetime.now().strftime(DATE_FORMAT) print("Saving final model version: ", version) self.save(version) print("======================================\n") print("Finished training with final loss:", optimizer.loss_func.loss(X, Y, self.weights)) print("=====================================================\n")
def fit(self, X, Y, optimizer=GradientDescent, epochs=25, zeros=False, save_best=False): """ Train the Linear Regression Model by fitting its associated weights, according to Dataset's Inputs and their corresponding Output Values. PARAMETERS ========== X: ndarray(dtype=float,ndim=1) 1-D Array of Dataset's Input. Y: ndarray(dtype=float,ndim=1) 1-D Array of Dataset's Output. optimizer: class Class of one of the Optimizers like AdamProp,SGD,MBGD,RMSprop,AdamDelta, Gradient Descent,etc. epochs: int Number of times, the loop to calculate loss and optimize weights, will going to take place. zeros: boolean Condition to initialize Weights as either zeroes or some random decimal values. save_best: boolean Condition to enable or disable the option of saving the suitable Weight values for the model after reaching the region nearby the minima of Loss-Function with respect to Weights. epoch_loss: float The degree of how much the predicted value is diverted from actual values, given by implementing one of choosen loss functions from loss_func.py . version: str Descriptive update of Model's Version at each step of Training Loop, along with Time description according to DATA_FORMAT. RETURNS ======= None """ self.weights = generate_weights(X.shape[1], 1, zeros=zeros) self.best_weights = {"weights": None, "loss": float('inf')} print("Starting training with loss:", optimizer.loss_func.loss(X, Y, self.weights)) for epoch in range(1, epochs + 1): print("======================================") print("epoch:", epoch) self.weights = optimizer.iterate(X, Y, self.weights) epoch_loss = optimizer.loss_func.loss(X, Y, self.weights) if save_best and epoch_loss < self.best_weights["loss"]: print("updating best weights (loss: {})".format(epoch_loss)) self.best_weights['weights'] = self.weights self.best_weights['loss'] = epoch_loss version = "model_best_" + datetime.now().strftime(DATE_FORMAT) print("Saving best model version: ", version) self.save(version) print("Loss in this step: ", epoch_loss) version = "model_final_" + datetime.now().strftime(DATE_FORMAT) print("Saving final model version: ", version) self.save(version) print("======================================\n") print("Finished training with final loss:", optimizer.loss_func.loss(X, Y, self.weights)) print("=====================================================\n")
def Plot(self, X, Y, actual_predictions, optimizer=GradientDescent, epochs=25, zeros=False ): """ Plots for Logistic Regression. PARAMETERS ========== X: ndarray(dtype=float,ndim=1) 1-D Array of Dataset's Input. Y: ndarray(dtype=float,ndim=1) 1-D Array of Dataset's Output. actual_predictions: ndarray(dtype=int,ndim=1) 1-D Array of Output, associated to each Input of Dataset, Predicted by Trained Logistic Regression Model. optimizer: class Class of one of the Optimizers like AdamProp,SGD,MBGD,GradientDescent etc epochs: int Number of times, the loop to calculate loss and optimize weights, will going to take place. error: float The degree of how much the predicted value is diverted from actual values, given by implementing one of choosen loss functions from loss_func.py . zeros: boolean Condition to initialize Weights as either zeroes or some random decimal values. RETURNS ======= 2-D graph of Sigmoid curve, Comparision Plot of True output and Predicted output versus Feacture. 2-D graph of Loss versus Number of iterations. """ Plot = plt.figure(figsize=(8, 8)) plot1 = Plot.add_subplot(2, 2, 1) plot2 = Plot.add_subplot(2, 2, 2) plot3 = Plot.add_subplot(2, 2, 3) # 2-D graph of Sigmoid curve. x = np.linspace(- max(X[:, 0]) - 2, max(X[:, 0]) + 2, 1000) plot1.set_title('Sigmoid curve') plot1.grid() sigmoid = Sigmoid() plot1.scatter(X.T[0], Y, color="red", marker="+", label="labels") plot1.plot(x, 0*x+0.5, linestyle="--", label="Decision bound, y=0.5") plot1.plot(x, sigmoid.activation(x), color="green", label='Sigmoid function: 1 / (1 + e^-x)' ) plot1.legend() # Comparision Plot of Actual output and Predicted output vs Feacture. plot2.set_title('Actual output and Predicted output versus Feacture') plot2.set_xlabel("x") plot2.set_ylabel("y") plot2.scatter(X[:, 0], Y, color="orange", label='Actual output') plot2.grid() plot2.scatter(X[:, 0], actual_predictions, color="blue", marker="+", label='Predicted output' ) plot2.legend() # 2-D graph of Loss versus Number of iterations. plot3.set_title("Loss versus Number of iterations") plot3.set_xlabel("iterations") plot3.set_ylabel("Cost") iterations = [] cost = [] self.weights = generate_weights(X.shape[1], 1, zeros=zeros) for epoch in range(1, epochs + 1): iterations.append(epoch) self.weights = optimizer.iterate(X, Y, self.weights) error = optimizer.loss_func.loss(X, Y, self.weights) cost.append(error) plot3.plot(np.array(iterations), np.array(cost)) plt.show()
def plot( self, X, Y, Z, optimizer=GradientDescent, epochs=60, zeros=False, save_best=False ): """ Plot the graph of Loss vs Epochs Plot the graph of line Of Polynomial Regression PARAMETERS ========== X: ndarray(dtype=float, ndim=1) 1-D array of Dataset's input Y: ndarray(dtype=float, ndim=1) 1-D array of Dataset's output Z: ndarray(dtype=float, ndim=1) 1-D array of Predicted Values optimizer: class Class of one of the Optimizers like AdamProp,SGD,MBGD,RMSprop,AdamDelta, Gradient Descent,etc. epochs: int Number of times, the loop to calculate loss and optimize weights, is going to take place. zeros: boolean Condition to initialize Weights as either zeroes or some random decimal values. save_best: boolean Condition to enable or disable the option of saving the suitable Weight values for the model after reaching the region nearby the minima of Loss-Function with respect to Weights. RETURNS ======= None """ M, N = X.shape P = X[:, 0:1] for i in range(2, self.degree+1): P = np.hstack(( P, (np.power(X[:, 0:1], i)).reshape(M, 1) )) P = np.hstack(( P, X[:, 1:2] )) X = P m = [] List = [] self.weights = generate_weights(X.shape[1], 1, zeros=zeros) self.best_weights = {"weights": self.weights, "loss": optimizer.loss_func.loss(X, Y, self.weights)} print("Starting training with loss:", optimizer.loss_func.loss(X, Y, self.weights)) for epoch in range(1, epochs + 1): m.append(epoch) self.weights = optimizer.iterate(X, Y, self.weights) epoch_loss = optimizer.loss_func.loss(X, Y, self.weights) if save_best and epoch_loss < self.best_weights["loss"]: self.best_weights['weights'] = self.weights self.best_weights['loss'] = epoch_loss List.append(epoch_loss) x = np.array(m) y = np.array(List) plt.figure(figsize=(10, 5)) plt.xlabel('EPOCHS', family='serif', fontsize=15) plt.ylabel('LOSS', family='serif', fontsize=15) plt.scatter(x, y, color='navy') plt.show() z = np.reshape(Z, (1, M)) pred_value = z[0] true_value = Y[0] A = [] for i in range(0, len(Y[0])): A.append(i) x_axis = np.array(A) plt.xlabel('Number of Datasets', family='serif', fontsize=15) plt.ylabel('Values', family='serif', fontsize=15) plt.scatter(x_axis, true_value, label="True Values") plt.plot(x_axis, pred_value, label="Predicted Values") plt.legend(loc="upper right") plt.show()