前提・実現したいこと
Kerasのサンプルコードを使ってオリジナルのデータセットでVAEを実装したのですが、ROC曲線とAUCを表示させる部分でつまづいています。
初めはscikit-learnのMSE関数でy_true(元画像)とy_pred(復元画像)の平均二乗誤差を出そうとしたのですが、どうやらこちらの関数は2次元までしか対応していないようで(画像は4次元)、うまくいかなかったため、tensoflowで実装したところ、やはりエラーが出てしまいました。
・Scikit-learnのMSE関数で4次元のy_trueとy_predの平均二乗誤差を算出する方法
・tensorflowのMSE関数で4次元のy_trueとy_predの平均二乗誤差を算出する方法
上記2点のいずれかをご教示いただけますと幸いです。
お手数をお掛けしますがどうぞよろしくお願いいたします。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 55 try: ---> 56 return getattr(obj, method)(*args, **kwds) 57 7 frames 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. During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in __bool__(self) 651 `TypeError`. 652 """ --> 653 raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. " 654 "Use `if t is not None:` instead of `if t:` to test if a " 655 "tensor is defined, and use TensorFlow ops such as " 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.
該当のソースコード
#ROC曲線とAUCスコアの表示 #from sklearn.metrics import mean_squared_error from sklearn.metrics import roc_curve from sklearn.metrics import roc_auc_score import tensorflow as tf test_normal_pred = vae.predict(test_normal, verbose=1) test_anomaly_pred = vae.predict(test_anomaly, verbose=1) score = [] for i in range(len(test_normal)): squared_error_normal = tf.reduce_mean(tf.square(test_normal_pred[i,:,:,:] -test_normal[i,:,:,:]))# mean_squared_error(test_normal[i,:,:,:], test_normal_pred[i,:,:,:]) score.append(squared_error_normal) for i in range(len(test_anomaly)): squared_error_anomaly = tf.reduce_mean(tf.square(test_anomaly_pred[i,:,:,:] - test_anomaly[i,:,:,:]))# mean_squared_error(test_anomaly[i,:,:,:], test_anomaly_pred[i,:,:,:]) score.append(squared_error_anomaly) score = np.array(score) #print(score.shape) #print(score) #ROC曲線の描画 y_true = np.zeros(len(test_normal)+len(test_anomaly)) y_true[len(test_normal):] = 1#0:正常、1:異常 #print(y_true.shape) #print(score.shape) #print(score) # FPR, TPR(, しきい値) を算出 fpr, tpr, thresholds = roc_curve(y_true, score) # AUC auc = roc_auc_score(y_true, score) # ROC曲線をプロット plt.figure plt.plot(fpr, tpr, label='VAE method (area = %.2f)'%auc, c="r") plt.legend() plt.title('ROC curve') plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.grid(True) plt.savefig("./ROC curve.png") plt.show()
補足情報(FW/ツールのバージョンなど)
学習の部分のコードは省略させていただいております。
Google Colabを使っています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。