def question10(): perceptron = Perceptron() svm = SVM() lda = LDA() repeat = 500 mean_accuracies = np.empty((3, len(all_m))) accuracies = np.empty((3, repeat)) for i, m in enumerate(all_m): for j in range(repeat): X, yx = draw_points_until_two_classes(m) # training set Z, yz = draw_points_until_two_classes(k) # test set perceptron.fit(X, yx) svm.fit(X, yx) lda.fit(X, yx) accuracies[0, j] = perceptron.score(Z, yz)["accuracy"] accuracies[1, j] = svm.score(Z, yz)["accuracy"] accuracies[2, j] = lda.score(Z, yz)["accuracy"] mean_accuracies[:, i] = accuracies.mean(axis=1) models = ["Perceptron", "SVM", "LDA"] colors = ['blue', 'red', 'green'] fig = plt.figure() for i, model in enumerate(models): plt.plot(all_m, mean_accuracies[i, :], color=colors[i], label=model) plt.legend() plt.title("Q10: mean accuracy as function of m") fig.savefig("q10.png", bbox_inches='tight', pad_inches=0.2, dpi=fig.dpi)
def question9(): perceptron = Perceptron() svm = SVM() fig = plt.figure() plt.suptitle("Q9: True vs. Perceptron vs. SVM hyperplanes") for i, m in enumerate(all_m): X, y = draw_points_until_two_classes(m) svm.fit(X, y) ax = fig.add_subplot(2, 3, i + 1) ax.scatter(X[y == -1, 0], X[y == -1, 1], color="blue", label="y=-1") # first class, labeled -1 ax.scatter(X[y == 1, 0], X[y == 1, 1], color="red", label="y=1") # second class, labeled 1 xmin, xmax = plt.xlim() xx = np.linspace(xmin, xmax) true_hyperplane = a * xx - (b / w[1]) # perceptron perceptron.fit(X, y) w_perc = perceptron.model[:-1] a_perceptron = -w_perc[0] / w_perc[1] b_perceptron = perceptron.model[-1] / perceptron.model[1] perceptron_hyperplane = a_perceptron * xx - b_perceptron w_svm = svm.model.coef_[0] a_svm = -w_svm[0] / w_svm[1] b_svm = svm.model.intercept_[0] / w_svm[1] SVM_hyperplane = a_svm * xx - b_svm ax.plot(xx, true_hyperplane, color="black", label="true hyperplane") ax.plot(xx, perceptron_hyperplane, color="green", label="perceptron hyperplane") ax.plot(xx, SVM_hyperplane, color="orange", label="svm hyperplane") ax.title.set_text("m=" + str(m)) if i == 0: plt.legend() fig.savefig("q9.png", bbox_inches='tight', pad_inches=0.3, dpi=fig.dpi)
# Y_train = (Y_train-1)/2.0 X[:, 0:X_train.shape[1]] = X_train Y[:, 0:Y_train.shape[1]] = Y_train Y = to_categorical(Y) X = np.reshape(X, (X.shape[0], X.shape[1], 4)) #train model = SVM((X.shape[1], 4)) model._get_distribution_strategy = lambda: None json_string = model.to_json() open('model.json', 'w').write(json_string) checkpointer = ModelCheckpoint(path_best_weights, verbose=1, monitor='val_loss', mode='auto', save_best_only=True) tbCallback = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True, write_images=True, profile_batch=100000000) model.fit(X, Y, epochs=N_epochs, batch_size=batch_size, verbose=2, shuffle=True, validation_split=0.2, callbacks=[checkpointer]) model.save_weights(path_last_weights, overwrite=True)
results0 = np.zeros(3000) len_files = len(FILES) for i in range(len_files): γ = gamma_list[i] λ = lambda_list[i] X_train, Y_train, X_test = load_data(i, data_dir=DATA_DIR, files_dict=FILES) kernel = GaussianKernel(γ) clf = SVM(_lambda=λ, kernel=kernel) clf.fit(X_train, Y_train) y_pred = clf.predict(X_test) results0[i * 1000:i * 1000 + 1000] = y_pred # SAVE Results save_results("results_SVM_gaussian.csv", results0, RESULT_DIR) print("1/3 Ending SVM with Gaussian kernel...") ##################################### # 2) SVM with Convolutional kernel # ##################################### print("2/3 Starting SVM with Convolutional kernel...") # Define parameters lists sigma_list = [0.31, 0.31, 0.3] k_list = [9, 10, 11] lambda_list = [1e-5, 1e-9, 1e-9]