0

Javaでマルチスレッドを使用して並列計算を行っています。May のデータ構造は HashMap です。私のアルゴリズムには、除算と平方根がありません。それは確率的勾配降下です.それでも私は得てNaNいます.ハッシュマップに複数のアクセス、読み書きがあるかどうかを知りたいのですが、それは未定義の問題を引き起こしますか? はいの場合、どうすればそれを取り除くことができますか?

[編集]

for(Map.Entry<Integer, ArrayList<Double>>entry: Matrix.entrySet()){
        sum = sum + Math.exp(getScore(contextFeatureVector,entry.getValue()));
    }

getScoreメソッドは内積を計算します。戻り値はinfinityです。

[編集2]

この上記の方法でMatrixは、次のとおりです。

  Map<Integer,ArrayList<Double>> Matrix = new ConcurrentHashMap<Integer,ArrayList<Double>>();

私はまだ取得していNaNます.それは、私が値にアクセスしている方法が原因です. 誰かが明確な直感で答えてくれませんか?

4

1 に答える 1

2

you should be using java.util.concurrent.ConcurrentHashMap if you going to have two threads adding and removing entries from there. you be be NaN when one of your thread removes a key that the other thread just got a handel on it and will call value for key.

Or you could block on the HashMap Object when adding/remove entries.

[edit] Couple of thoughts:

• Integers are probablly not very reliable to sync on. (unless your doing the extra work of making sure you don't have two Integer for the same value, so you must keep track and reuse Integer.)

• NaN means your most likely getting a nil for a number. • division by 0 • calculating sqrt of a negative number.

• NaN != Infinity

Can you log values To console as you use them or just find the line where your having the exption thrown from. Maybe just dump out the whole map.

This map you have now is the way to go but you need to ensure its gettin what it needs to do its thing.

于 2012-07-21T13:27:30.143 に答える