誤差逆伝播法(3) 乱数の種(2)
Published:
By nobCategory: Posts
乱数の種
MultiLayerPerceptron
クラスを作成したのだが、乱数の種(シード)を変えるとどうなるか試してみる。
XORを識別してみる
import matplotlib.pyplot as plt
import numpy as np
# 自作クラス
from mlp import (
ActivationLayer,
FullyConnectedLayer,
InputLayer,
MeanSquaredError,
MultiLayerPerceptron,
Sigmoid,
)
from numpy.random import MT19937, Generator, SeedSequence
np.set_printoptions(precision=3)
data = np.array(
[
[0, 0, 0],
[1, 0, 1],
[0, 1, 1],
[1, 1, 0],
]
)
x = data[:, :-1]
y = data[:, -1:]
def model(activation, rng):
mlp = MultiLayerPerceptron(
[
InputLayer(2),
FullyConnectedLayer(2, 2, rng),
ActivationLayer(Sigmoid()),
FullyConnectedLayer(2, 1, rng),
ActivationLayer(Sigmoid()),
],
MeanSquaredError(),
epochs=1000,
learning_rate=1,
)
return mlp
fig = plt.figure(figsize=(15, 160))
seeds = 200
cols = 4
rows = seeds // cols
for seed in range(seeds):
mlp_sigm_mt1 = model(Sigmoid(), Generator(MT19937(seed)))
mlp_sigm_mt1.fit(x, y)
mlp_sigm_pcg1 = model(Sigmoid(), np.random.default_rng(seed))
mlp_sigm_pcg1.fit(x, y)
ax = fig.add_subplot(rows, cols, seed + 1)
ax.set_title("XOR {}".format(seed))
ax.set_xlabel("epoch")
ax.set_ylabel("cost")
ax.plot(
mlp_sigm_mt1.history,
marker="o",
markersize=1,
alpha=0.3,
label=r"$\sigma, \; MT19937$",
)
ax.plot(
mlp_sigm_pcg1.history,
marker="o",
markersize=1,
alpha=0.3,
label=r"$\sigma, \; PCG64$",
)
ax.set_ylim(-0.1, ax.get_ylim()[1])
ax.legend()
ax.grid()
ax.minorticks_on()
plt.tight_layout()
plt.show()