diff --git a/libs/models/som.py b/libs/models/som.py index ae89242..a736663 100644 --- a/libs/models/som.py +++ b/libs/models/som.py @@ -66,7 +66,7 @@ def __init__(self, X, latent_dim, resolution, sigma_max, sigma_min, tau, init='r def _cooperative_process(self, epoch): # 学習量を計算 # sigma = self.sigma_min + (self.sigma_max - self.sigma_min) * np.exp(-epoch / self.tau) # 近傍半径を設定 - self.sigma = max(self.sigma_min, self.sigma_max * (1 - (epoch / self.tau))) # 近傍半径を設定 + self.sigma = max(self.sigma_min, self.sigma_min + (self.sigma_max - self.sigma_min) * (1 - (epoch / self.tau))) # 近傍半径を設定 Dist = dist.cdist(self.Zeta, self.Z, 'sqeuclidean') # KxNの距離行列を計算 # ノードと勝者ノードの全ての組み合わせにおける距離を網羅した行列 diff --git a/libs/models/som_tensorflow.py b/libs/models/som_tensorflow.py index c8a7d1a..678f5ac 100644 --- a/libs/models/som_tensorflow.py +++ b/libs/models/som_tensorflow.py @@ -158,7 +158,7 @@ def sigma(self): with tf.name_scope('Sigma_computation'): - return tf.maximum(tf.cast(self.sigma_min, tf.float64), self.sigma_max*(1 - (self.iter_no/self.tau))) + return tf.maximum(tf.cast(self.sigma_min, tf.float64), self.sigma_min + (self.sigma_max - self.sigma_min) * (1 - (self.iter_no / self.tau))) def winning_nodes(self): """ diff --git a/libs/models/som_use_for.py b/libs/models/som_use_for.py index aad5760..59bef76 100644 --- a/libs/models/som_use_for.py +++ b/libs/models/som_use_for.py @@ -64,7 +64,7 @@ def fit(self, nb_epoch=100, verbose=True): # 学習量の計算 H = np.zeros((self.K,self.N)) # 全ての学習量を保持する配列を用意 # 近傍半径の計算 - sigma = max(self.sigma_min, self.sigma_max * ( 1 - (epoch / self.tau) ) )# 線形で減少 + sigma = max(self.sigma_min, self.sigma_min + (self.sigma_max - self.sigma_min) * (1 - (epoch / self.tau)))# 線形で減少 #sigma = self.sigma_min + (self.sigma_max - self.sigma_min) * ( 1 - (epoch / self.tau) ) # 指数的に減少 for k in range(self.K): for n in range(self.N): diff --git a/libs/models/tsom.py b/libs/models/tsom.py index 8e498b8..e975eaa 100644 --- a/libs/models/tsom.py +++ b/libs/models/tsom.py @@ -145,14 +145,14 @@ def fit(self, nb_epoch=200): for epoch in tqdm(np.arange(nb_epoch)): # 学習量の決定 # sigma1 = self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * np.exp(-epoch / self.TAU1) - sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MAX * (1 - (epoch / self.TAU1))) + sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * (1 - (epoch / self.TAU1))) distance1 = distance.cdist(self.Zeta1, self.Z1, 'sqeuclidean') # 距離行列をつくるDはN*K行列 H1 = np.exp(-distance1 / (2 * pow(sigma1, 2))) # かっこに気を付ける G1 = np.sum(H1, axis=1) # Gは行ごとの和をとったベクトル R1 = (H1.T / G1).T # 行列の計算なので.Tで転置を行う # sigma2 = self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * np.exp(-epoch / self.TAU2) - sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MAX * (1 - (epoch / self.TAU2))) + sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * (1 - (epoch / self.TAU2))) distance2 = distance.cdist(self.Zeta2, self.Z2, 'sqeuclidean') # 距離行列をつくるDはN*K行列 H2 = np.exp(-distance2 / (2 * pow(sigma2, 2))) # かっこに気を付ける G2 = np.sum(H2, axis=1) # Gは行ごとの和をとったベクトル diff --git a/libs/models/tsom2_ishida.py b/libs/models/tsom2_ishida.py index 22eff45..e827dc5 100644 --- a/libs/models/tsom2_ishida.py +++ b/libs/models/tsom2_ishida.py @@ -144,14 +144,14 @@ def fit(self, nb_epoch=200): for epoch in tqdm(np.arange(nb_epoch)): # 学習量の決定 # sigma1 = self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * np.exp(-epoch / self.TAU1) - sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MAX * (1 - (epoch / self.TAU1))) + sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * (1 - (epoch / self.TAU1))) distance1 = distance.cdist(self.Zeta1, self.Z1, 'sqeuclidean') # 距離行列をつくるDはK1*N1行列 H1 = np.exp(-distance1 / (2 * pow(sigma1, 2))) # かっこに気を付ける G1 = np.sum(H1, axis=1) # Gは行ごとの和をとったベクトル R1 = (H1.T / G1).T # 行列の計算なので.Tで転置を行う # sigma2 = self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * np.exp(-epoch / self.TAU2) - sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MAX * (1 - (epoch / self.TAU2))) + sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * (1 - (epoch / self.TAU2))) distance2 = distance.cdist(self.Zeta2, self.Z2, 'sqeuclidean') # 距離行列をつくるDはK2*N2行列 H2 = np.exp(-distance2 / (2 * pow(sigma2, 2))) # かっこに気を付ける G2 = np.sum(H2, axis=1) # Gは行ごとの和をとったベクトル diff --git a/libs/models/tsom3.py b/libs/models/tsom3.py index 57600c6..1b4d400 100644 --- a/libs/models/tsom3.py +++ b/libs/models/tsom3.py @@ -130,20 +130,20 @@ def fit(self, nb_epoch=200): for epoch in tqdm(np.arange(nb_epoch)): # 学習量の決定 # sigma1 = self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * np.exp(-epoch / self.TAU1) - sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MAX * (1 - (epoch / self.TAU1))) + sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * (1 - (epoch / self.TAU1))) distance1 = distance.cdist(self.Zeta1, self.Z1, 'sqeuclidean') # 距離行列をつくるDはK1*N1行列 H1 = np.exp(-distance1 / (2 * pow(sigma1, 2))) # かっこに気を付ける G1 = np.sum(H1, axis=1) # Gは行ごとの和をとったベクトル K1*1 R1 = (H1.T / G1).T # 行列の計算なので.Tで転置を行う K1*N1 # sigma2 = self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * np.exp(-epoch / self.TAU2) - sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MAX * (1 - (epoch / self.TAU2))) + sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * (1 - (epoch / self.TAU2))) distance2 = distance.cdist(self.Zeta2, self.Z2, 'sqeuclidean') # 距離行列をつくるDはK2*N2行列 H2 = np.exp(-distance2 / (2 * pow(sigma2, 2))) # かっこに気を付ける G2 = np.sum(H2, axis=1) # Gは行ごとの和をとったベクトル K2*1 R2 = (H2.T / G2).T # 行列の計算なので.Tで転置を行う K2*N2 - sigma3 = max(self.SIGMA3_MIN, self.SIGMA3_MAX * (1 - (epoch / self.TAU3))) + sigma3 = max(self.SIGMA3_MIN, self.SIGMA3_MIN + (self.SIGMA3_MAX - self.SIGMA3_MIN) * (1 - (epoch / self.TAU3))) distance3 = distance.cdist(self.Zeta3, self.Z3, 'sqeuclidean') # 距離行列をつくるDはK3*N3行列 # print(distance3.shape) H3 = np.exp(-distance3 / (2 * pow(sigma3, 2))) # かっこに気を付ける diff --git a/libs/models/tsom_tensorflow.py b/libs/models/tsom_tensorflow.py index fd9a7ae..3d4582c 100644 --- a/libs/models/tsom_tensorflow.py +++ b/libs/models/tsom_tensorflow.py @@ -200,7 +200,7 @@ def sigma(self): """ with tf.name_scope('Sigma_computation'): - return tf.maximum(self.sigma_min * tf.constant(1, tf.float64), self.sigma_max * (1 - (self.iter_no / self.tau))) + return tf.maximum(self.sigma_min * tf.constant(1, tf.float64), self.sigma_min + (self.sigma_max - self.sigma_min) * (1 - (self.iter_no / self.tau))) def winning_nodes(self, M, Y, axis): """ diff --git a/libs/models/tsom_use_for.py b/libs/models/tsom_use_for.py index 7f4a868..0aad81c 100644 --- a/libs/models/tsom_use_for.py +++ b/libs/models/tsom_use_for.py @@ -121,7 +121,7 @@ def fit(self, nb_epoch=200): # mode1の学習量の計算 #sigma1 = self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * np.exp(-epoch / self.TAU1) - sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MAX * (1 - (epoch / self.TAU1))) + sigma1 = max(self.SIGMA1_MIN, self.SIGMA1_MIN + (self.SIGMA1_MAX - self.SIGMA1_MIN) * (1 - (epoch / self.TAU1))) for i in np.arange(self.N1): for k in np.arange(self.K1): zeta_dis1 = 0 @@ -130,7 +130,7 @@ def fit(self, nb_epoch=200): h1[i][k] = np.exp(-0.5 * zeta_dis1 / (sigma1 ** 2)) # mode2の学習量の計算 - sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MAX * (1 - (epoch / self.TAU2))) + sigma2 = max(self.SIGMA2_MIN, self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * (1 - (epoch / self.TAU2))) #sigma2 = self.SIGMA2_MIN + (self.SIGMA2_MAX - self.SIGMA2_MIN) * np.exp(-epoch / self.TAU2) for j in np.arange(self.N2): for l in np.arange(self.K2): diff --git a/libs/tools/calc_sigma.py b/libs/tools/calc_sigma.py index d6b0eb8..6db613a 100644 --- a/libs/tools/calc_sigma.py +++ b/libs/tools/calc_sigma.py @@ -21,7 +21,7 @@ def __calc_sigma_exp(sigma_max: float, sigma_min: float, tau: float, epoch: int) def __calc_sigma_linear(sigma_max: float, sigma_min: float, tau: float, epoch: int) -> float: - return max(sigma_min, sigma_max * (1 - (epoch / tau))) + return max(sigma_min, sigma_min + (sigma_max - sigma_min) * (1 - (epoch / tau))) if __name__ == '__main__':