CNNの出力から記述子を使用してトリプレット損失を定義しようとしていますが、ネットワークをトレーニングしようとするとこのエラーが表示されました。
損失関数の私の定義:
def compute_loss(descriptor, margin):
diff_pos = descriptor[0:1800:3] - descriptor[1:1800:3]
diff_neg = descriptor[0:1800:3] - descriptor[2:1800:3]
Ltriplet = np.maximum(0, 1 - tf.square(diff_neg)/(tf.square(diff_pos) + margin))
Lpair = tf.square(diff_pos)
Loss = Ltriplet + Lpair
return Loss
ここでの記述子はCNNの結果であり、CNNの収入は、アンカー、プーラー、プッシャーを正確にこの順序で含むトリプレットのセットです。入力として、600個のトリプレットをまとめてCNNにフィードしました。
次に、ネットワークのトレーニング中に次のエラーが発生しました。
2018-03-08 16:40:49.529263: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Traceback (most recent call last):
File "/Users/gaoyingqiang/Documents/GitHub/Master-TUM/TDCV/exercise_3/ex3/task2_new.py", line 78, in <module>
loss = compute_loss(h_fc2, margin)
File "/Users/gaoyingqiang/Documents/GitHub/Master-TUM/TDCV/exercise_3/ex3/task2_new.py", line 37, in compute_loss
Ltriplet = np.maximum(0, 1 - tf.square(diff_neg)/(tf.square(diff_pos) + margin))
File "/Users/gaoyingqiang/.virtualenvs/ex3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 614, in __bool__
raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
Process finished with exit code 1
どこが悪いのですか?
numpy操作とtensorflow操作を混在させています。Tensorflowは通常、numpy配列を受け入れます(それらの値は静的にわかっているため、定数に変換できます)が、その逆はできません(テンソル値は、熱心な評価を除いて、セッションの実行時にのみわかります)。
解決策:に変更np.maximum
しtf.maximum
ます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加