AL-2020/coder/train_nn.py

30 lines
765 B
Python

import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
import pygame
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
from PIL import Image
# recznie napisane cyfry
digits = datasets.load_digits()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
x_train = x[:1000000]
y_train = y[:1000000]
x_test = x[1000:]
y_test = y[1000:]
mlp = MLPClassifier(hidden_layer_sizes=(15,), activation='logistic', alpha=1e-4,
solver='sgd', tol=1e-4, random_state=1,
learning_rate_init=.1, verbose=True)
mlp.fit(x_train, y_train)
predictions = mlp.predict(x_test)
print(accuracy_score(y_test, predictions))
print(x_test[1])