Skip to content

Instantly share code, notes, and snippets.

@koshian2
Created May 8, 2018 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koshian2/4b26f582fa3bbcc0ab28e119e3cbda8b to your computer and use it in GitHub Desktop.
Save koshian2/4b26f582fa3bbcc0ab28e119e3cbda8b to your computer and use it in GitHub Desktop.
Coursera Machine LearningをPythonで実装 - [Week5]ニューラルネットワーク(2) [2]組み込み
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
from sklearn.neural_network import MLPClassifier
# データの読み込み
def load_data1():
data = loadmat("ex4data1")
return np.array(data['X']), np.ravel(np.array(data['y']))
X_data, y = load_data1()
m = len(X_data[:, 1])
## ネットワークの可視化
def display_data(X, alpha, accuracy):
fig = plt.figure(figsize = (5, 5))
fig.subplots_adjust(hspace=0.05, wspace=0.05)
for i in range(X.shape[0]):
ax = fig.add_subplot(5, 5, i+1, xticks=[], yticks=[])
ax.imshow(X[i, :].reshape(20, 20, order="F"), cmap="gray")
plt.suptitle(f"Alpha = {alpha}, Accuracy = {accuracy}")
plt.show()
# 正則化の値を変えてみる
alphas = [0.0001, 1, 50, 1e10]
for a in alphas:
print("Alpha =", a)
clf = MLPClassifier(hidden_layer_sizes=(26, ), solver="adam", random_state=114514, max_iter=500, alpha=a)
clf.fit(X_data, y)
accuracy = clf.score(X_data, y)*100
print("Training Set Accuracy:" , accuracy)
print()
display_data(clf.coefs_[0][:, 1:].T, a, np.round(accuracy, 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment