👌

2022/05/26に公開

# はじめに

コードを自分なりに解説していきます。

## 全体のコード

``````from tensorflow import keras
from sklearn.utils import shuffle
import numpy as np

M = 3
K = 3
n = 500
N = n * K

X1 = np.random.randn(n, M) + np.array([0, 10, 10])
X2 = np.random.randn(n, M) + np.array([5, 5, 5])
X3 = np.random.randn(n, M) + np.array([10, 0, 0])
Y1 = np.array([[1, 0, 0] for i in range(n)])
Y2 = np.array([[0, 1, 0] for k in range(n)])
Y3 = np.array([[0, 0, 1] for l in range(n)])

X = np.concatenate((X1, X2, X3), axis=0)
Y = np.concatenate((Y1, Y2, Y3), axis=0)

model = keras.Sequential()

minibatch = 50
model.fit(X, Y, epochs=20, batch_size=minibatch)

X_, Y_ = shuffle(X, Y)
classes = model.predict_classes(X_[0:10], batch_size=minibatch)
prob = model.predict_proba(X_[0:10], batch_size=1)
print('classified:')
print(np.argmax(model.predict(X_[0:10]), axis=1) == classes)
print()
print('output probability')
print(prob)
``````

classified:
[ True True True True True True True True True True]
[[1.2065215e-14 2.5491940e-04 9.9974507e-01]
[9.9746346e-01 2.5364908e-03 2.1635964e-14]
[2.5158457e-17 1.2430476e-06 9.9999881e-01]
[5.6705085e-07 9.9467885e-01 5.3205532e-03]
[7.7383193e-18 6.0985526e-06 9.9999392e-01]
[4.0933984e-19 5.0811457e-07 9.9999952e-01]
[2.6586259e-04 9.9967742e-01 5.6769386e-05]
[5.5576580e-05 9.9969482e-01 2.4965103e-04]
[2.5015077e-04 9.9918383e-01 5.6604022e-04]
[1.0618347e-03 9.9887949e-01 5.8662932e-05]]

それでは各々のコードを解説していきます。