Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iteration 1: codebook contains NaN or Inf values #176

Open
yanglianbing18 opened this issue Jul 28, 2024 · 13 comments
Open

Iteration 1: codebook contains NaN or Inf values #176

yanglianbing18 opened this issue Jul 28, 2024 · 13 comments

Comments

@yanglianbing18
Copy link

The result after each iteration of som is the same, no change

@yanglianbing18
Copy link
Author

The weight matrix is nan every time

@yanglianbing18
Copy link
Author

this the code
import numpy as np
from somoclu import Somoclu
from sklearn.preprocessing import StandardScaler

生成示例数据并确保数据类型为float32

data = np.random.rand(10000, 3).astype(np.float32)

数据标准化

scaler = StandardScaler()
data = scaler.fit_transform(data).astype(np.float32)

再次检查数据是否包含 NaN 或 Inf

if np.any(np.isnan(data)) or np.any(np.isinf(data)):
raise ValueError("Input data contains NaN or Inf values.")

设置SOM网格大小

n_rows, n_columns = 20, 20

初始化Somoclu对象

som = Somoclu(n_columns, n_rows)

记录初始BMU

initial_bmus = som.get_bmus(data)

记录每次迭代后的BMU

iterations = 10
bmus_history = []

训练SOM并记录BMU变化

for epoch in range(iterations):
print(f"Training epoch {epoch + 1}...")

# 使用合理的训练参数
som.train(data, epochs=1, radius0=5, radiusN=1, scale0=0.05, scaleN=0.005)

# 强制将 codebook 转换为 numpy 数组
codebook_array = np.array(som.codebook)

# 检查权重矩阵是否包含 NaN 或 Inf
if np.any(np.isnan(codebook_array)) or np.any(np.isinf(codebook_array)):
    print(f"Iteration {epoch + 1}: codebook contains NaN or Inf values.")
    print(f"NaN indices: {np.where(np.isnan(codebook_array))}")
    print(f"Inf indices: {np.where(np.isinf(codebook_array))}")
    raise ValueError(f"Iteration {epoch + 1}: codebook contains NaN or Inf values.")

# 记录BMU
bmus = som.get_bmus(data)
bmus_history.append(bmus)

# 打印权重矩阵
print(f"第 {epoch + 1} 次迭代后的权重矩阵:")
print(codebook_array)

# 计算BMU变化
changes = np.sum(initial_bmus != bmus)
print(f"迭代 {epoch + 1} 后的 BMU 变化: {changes}")
initial_bmus = bmus  # 更新初始BMU用于下次比较

检查每次迭代后的BMU变化

for i, bmus in enumerate(bmus_history):
changes = np.sum(initial_bmus != bmus_history[0]) # 比较每次迭代后的BMU与初始BMU
print(f"迭代 {i + 1} 后的 BMU 变化: {changes}")

@xgdgsc
Copy link
Collaborator

xgdgsc commented Jul 29, 2024

什么版本的python和环境

@yanglianbing18
Copy link
Author

python3.9

@yanglianbing18
Copy link
Author

但是我调用MiniSom写时,权值和分类类别值会更新,Somoclu就更新不了

@xgdgsc
Copy link
Collaborator

xgdgsc commented Jul 30, 2024

pip安装的还是anaconda里装的

@yanglianbing18
Copy link
Author

yanglianbing18 commented Jul 30, 2024 via email

@xgdgsc
Copy link
Collaborator

xgdgsc commented Jul 30, 2024

什么系统 什么版本的somoclu

@yanglianbing18
Copy link
Author

win10\1.7.6

@xgdgsc
Copy link
Collaborator

xgdgsc commented Aug 3, 2024

但epoch>1的时候都正常? 感觉一般没遇到过用epoch=1的?

@yanglianbing18
Copy link
Author

yanglianbing18 commented Aug 3, 2024 via email

@xgdgsc
Copy link
Collaborator

xgdgsc commented Aug 3, 2024

一般用法不是epoch>1然后运行完再看?没见过epoch=1然后外面循环的

@yanglianbing18
Copy link
Author

yanglianbing18 commented Aug 3, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants