#!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib.pyplot as plt from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from sklearn.model_selection import KFold from sklearn.metrics import precision_score import random import time """ from sklearn.datasets import fetch_openml mnist = fetch_openml('mnist_784') """ """ images = mnist.data.values.reshape((-1, 28, 28)) plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest") plt.show() print(mnist.target[0]) """ indices = [i for i in range(len(mnist.data))] random.shuffle(indices) indices = indices[:15000] data = [mnist.data.values[i] for i in indices] target = [mnist.target[i] for i in indices] bestClf = None bestScore = 0 scores = [] train_sizes = [0.05*i for i in range(1,20)] xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7) """ network = MLPClassifier(hidden_layer_sizes = (50)) network.fit(xtrain, ytrain) ytest_pred = clf.predict(xtest) print("Average = macro:") print(precision_score(ytest, ytest_pred, average = "macro")) print("Average = None:") print(precision_score(ytest, ytest_pred, average = None)) print(target[4]) print(network.predict(data[4].reshape(1,-1))) plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest") plt.show() """ """ n_layers = [i for i in range(1,51)] for n in n_layers: print(n) network = MLPClassifier(hidden_layer_sizes = (50,)*n) network.fit(xtrain, ytrain) scores += [network.score(xtest, ytest)] plt.plot(n_layers, scores) """ """ for _ in range(5): n_layers = random.randint(1,3) layer_size = tuple([random.randint(10,300) for _ in range(n_layers)]) print(layer_size) network = MLPClassifier(hidden_layer_sizes = layer_size, solver = "adam", activation = "relu") network.fit(xtrain, ytrain) print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}") """ """ solvers = ["lbfgs", "sgd", "adam"] for s in solvers: print(s + ":") start_time = time.time() network = MLPClassifier(hidden_layer_sizes = (50,)*5, solver = s) network.fit(xtrain, ytrain) print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}") """ """ functions = ["identity", "logistic", "tanh", "relu"] for f in functions: print(f + ":") start_time = time.time() network = MLPClassifier(hidden_layer_sizes = (50,)*5, activation = f) network.fit(xtrain, ytrain) print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}") """ times = [] scores = [] for i in range(-7,7): alpha = 10**i print(f"alpha = {alpha}:") start_time = time.time() network = MLPClassifier(hidden_layer_sizes = (50,)*5, alpha = alpha) network.fit(xtrain, ytrain) trainingTime = time.time() - start_time score = network.score(xtest, ytest) times +=[trainingTime] scores += [score] plt.plot([i for i in range(-7,7)], times) plt.show() plt.plot([i for i in range(-7,7)], scores)