Created
May 8, 2018 17:20
-
-
Save koshian2/4d0daeddc85f2514083ac71a3affd4c6 to your computer and use it in GitHub Desktop.
Coursera Machine LearningをPythonで実装 - [Week4]ニューラルネットワーク(1) [3]ニューラルネットワーク、自分で実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from scipy.io import loadmat | |
# データの読み込み | |
def load_data1(): | |
data = loadmat("ex3data1") | |
# yが元データだと5000x1の行列なので、ベクトルに変換する | |
return np.array(data['X']), np.ravel(np.array(data['y'])) | |
X_data, y = load_data1() | |
m = len(X_data[:, 1]) | |
# 計算済みの係数を読み込み | |
def load_weights(): | |
data = loadmat("ex3weights") | |
return np.array(data['Theta1']), np.array(data['Theta2']) | |
Theta1, Theta2 = load_weights() | |
# Forward propagation | |
def predict(Theta1, Theta2, X): | |
XX = np.c_[np.ones(X.shape[0]), X] | |
Z2 = 1 / (1 + np.exp(-np.dot(XX, Theta1.T))) | |
XX = np.c_[np.ones(Z2.shape[0]), Z2] | |
Z3 = 1 / (1 + np.exp(-np.dot(XX, Theta2.T))) | |
return np.argmax(Z3, axis=1)+1 | |
# 精度 | |
pred = predict(Theta1, Theta2, X_data) | |
print("Training Set Accuracy: ", np.mean(pred == y) * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment