Skip to content

Commit

Permalink
Merge pull request #168 from furukawa-laboratory/#167_fix_linearly_de…
Browse files Browse the repository at this point in the history
…creasing_sigma_function

#167 近傍半径の線形減少の式を変更する
  • Loading branch information
ae14watanabe authored Sep 21, 2020
2 parents 2aaa356 + 022042c commit d020278
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion libs/models/som.py
Original file line number Diff line number Diff line change
Expand Up @@ -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の距離行列を計算
# ノードと勝者ノードの全ての組み合わせにおける距離を網羅した行列
Expand Down
2 changes: 1 addition & 1 deletion libs/models/som_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion libs/models/som_use_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions libs/models/tsom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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は行ごとの和をとったベクトル
Expand Down
4 changes: 2 additions & 2 deletions libs/models/tsom2_ishida.py
Original file line number Diff line number Diff line change
Expand Up @@ -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は行ごとの和をとったベクトル
Expand Down
6 changes: 3 additions & 3 deletions libs/models/tsom3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))) # かっこに気を付ける
Expand Down
2 changes: 1 addition & 1 deletion libs/models/tsom_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions libs/models/tsom_use_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion libs/tools/calc_sigma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit d020278

Please sign in to comment.