This file contains hidden or 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
from sklearn.neural_network import MLPClassifier | |
from sklearn.metrics import accuracy_score | |
# 教師データを通常の表記に戻す(sikit-learnの多クラスロジスティック回帰ではonehotにしなくてよい) | |
y_train = np.array([np.argmax(yi) for yi in Y_train]) | |
y_test = np.array([np.argmax(yi) for yi in Y_test]) | |
batch_size = int(len(X_train)*0.2) # ミニバッチサイズ | |
epochs = 100 # エポック数 | |
mu = 0.05 # 学習率 |
This file contains hidden or 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 matplotlib.pyplot as plt | |
from sklearn import datasets | |
from sklearn.preprocessing import OneHotEncoder | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.utils import shuffle | |
from sklearn.model_selection import train_test_split | |
# アヤメデータセット | |
iris = datasets.load_iris() | |
X = iris.data |
This file contains hidden or 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
class MultiLayerPerceptron(): | |
def __init__(self, hidden_layer_sizes=(10,), activation=“relu”, random_state=0): | |
self.init_state = True # 重みの初期化判定フラグ | |
self.dense = None # 全結合インスタンスリスト | |
self.act_func = None # 活性化関数インスタンスリスト | |
self.n_units = None # 各レイヤーのユニット数リスト | |
self.loss = None # トレーニングデータのLoss | |
self.val_loss = None # テストデータのLoss | |
self.acc = None # トレーニングデータの正答率 |
This file contains hidden or 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
class Dense: | |
def __init__(self, units, input_dim, kernel_initializer='he_normal', bias_initializer='zeros'): | |
self.units = units | |
self.input_dim = input_dim | |
self.kernel_initializer = kernel_initializer | |
self.bias_initializer = bias_initializer | |
self.W = None | |
self.b = None | |
self.dW = None | |
self.db = None |
This file contains hidden or 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
class SoftMax(): | |
def __init__(self, name="softmax"): | |
self.name = name | |
def forward_prop(self, Z): | |
self.Z = Z | |
return self.softmax(Z) | |
def back_prop(self, Z, Y): | |
return self.grad_softmax_with_loss(Z, Y) |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import numpy as np | |
class Activation(): | |
def __init__(self, name="sigmoid"): | |
self.name = name | |
def forward_prop(self, Z): | |
if self.name=="sigmoid": | |
return self.sigmoid(Z) |
This file contains hidden or 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 | |
n = 3 | |
m = 3 | |
B = np.random.rand(n,m) | |
Y = np.zeros_like(B) | |
for i in range(Y.shape[0]): | |
Y[i, np.random.choice(np.arange(Y.shape[1]), 1)] = 1 |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.metrics import accuracy_score | |
# 教師データを通常の表記に戻す(sikit-learnの多クラスロジスティック回帰ではonehotにしなくてよい) | |
y_train = np.array([np.argmax(yi) for yi in Y_train]) | |
y_test = np.array([np.argmax(yi) for yi in Y_test]) | |
# 正則化項:1/C/2*W^2 →C = ∞で正則化0 | |
clf_sk = LogisticRegression(C=1e10, max_iter=500, solver="sag", tol=1e-10) |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import matplotlib.pyplot as plt | |
from sklearn import datasets | |
from sklearn.preprocessing import OneHotEncoder | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.utils import shuffle | |
from sklearn.model_selection import train_test_split | |
# アヤメデータセット |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import numpy as np | |
class MultiClassLogisticRegression(): | |
def __init__(self): | |
self.init_state = True # 重みの初期化判定フラグ | |
self.W = None # 重み | |
self.b = None # 閾値 | |
self.loss = np.array([]) # トレーニングデータのLoss |