簡體   English   中英

在 TF 2.0 中使用 Keras 自定義損失函數

[英]Custom Loss Function with Keras in TF 2.0

TF 1.13 中 Tensorflow 2.0 Alpha 中的自定義損失函數

我正在嘗試在 TF 2.0 的model.compile()中使用此中的model.compile()損失函數。 雖然我的實現沒有錯誤,但損失和准確性不會移動。

我首先使用 Google 建議的代碼將 1.0 TF 代碼轉換為 2.0。

然后我從庫中導入該函數並按以下方式使用:

model.compile(optimizer='adam',
              loss=roc_auc_loss,
              metrics=['accuracy',acc0, acc1, acc2, acc3, acc4])
Epoch 17/100
100/100 [==============================] - 20s 197ms/step - loss: 469.7043 - accuracy: 0.0000e+00 - acc0: 0.0000e+00 - acc1: 0.0000e+00 - acc2: 0.0000e+00 - acc3: 0.0000e+00 - acc4: 0.0000e+00 - val_loss: 152.2152 - val_accuracy: 0.0000e+00 - val_acc0: 0.0000e+00 - val_acc1: 0.0000e+00 - val_acc2: 0.0000e+00 - val_acc3: 0.0000e+00 - val_acc4: 0.0000e+00
Epoch 18/100
100/100 [==============================] - 20s 198ms/step - loss: 472.0472 - accuracy: 0.0000e+00 - acc0: 0.0000e+00 - acc1: 0.0000e+00 - acc2: 0.0000e+00 - acc3: 0.0000e+00 - acc4: 0.0000e+00 - val_loss: 152.2152 - val_accuracy: 0.0000e+00 - val_acc0: 0.0000e+00 - val_acc1: 0.0000e+00 - val_acc2: 0.0000e+00 - val_acc3: 0.0000e+00 - val_acc4: 0.0000e+00
Epoch 19/100
 78/100 [======================>.......] - ETA: 4s - loss: 467.4657 - accuracy: 0.0000e+00 - acc0: 0.0000e+00 - acc1: 0.0000e+00 - acc2: 0.0000e+00 - acc3: 0.0000e+00 - acc4: 0.0000e+00

我想了解 TF 2.0 中的 Keras 有什么問題,它顯然不是反向傳播。 謝謝。

@ruben 你能分享一個獨立的代碼來重現這個問題嗎? 我認為我們需要檢查函數定義。 您是否在函數定義之上添加了 @tf.function()? 謝謝!

請查看以下示例(來自 TF 網站的簡單示例)

!pip install tensorflow==2.0.0-beta1

import tensorflow as tf
from tensorflow import keras 
import keras.backend as K

# load mnist data
mnist=tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
x_train,x_test=x_train/255.0,x_test/255.0

# Custom Metric1 (for example)
@tf.function()
def customMetric1(yTrue,yPred):
    return tf.reduce_mean(yTrue-yPred)

# Custom Metric2 (for example)
@tf.function()
def customMetric2(yTrue, yPred):
  return tf.reduce_mean(tf.square(tf.subtract(yTrue,yPred)))

model=tf.keras.models.Sequential([
                                  tf.keras.layers.Flatten(input_shape=(28,28)),
                                  tf.keras.layers.Dense(128, activation='relu'),
                                  tf.keras.layers.Dropout(0.2),
                                  tf.keras.layers.Dense(10,activation='softmax')
])

# Compile the model with custom loss functions
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy', customMetric1, customMetric2])

# Fit and evaluate model
model.fit(x_train,y_train,epochs=5)
model.evaluate(x_test,y_test)

輸出

警告:標志解析前的日志記錄進入 stderr。 W0711 23:57:16.453042 139687207184256 deprecation.py:323] 來自 /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_from.tensorflow.wrapper .ops.array_ops) 已棄用,並將在未來版本中刪除。 更新說明:使用2.0中的tf.where,廣播規則與np.where Train on 60000個樣本相同

Epoch 1/5 60000/60000 [==============================] - 5s 87us/sample - 損失:0.2983 - 准確度: 0.9133 - customMetric1: 4.3539 - customMetric2: 27.3769 Epoch 2/5 60000/60000 [============================] - 5s 83us/sample - 損失:0.1456 - 准確度:0.9555 - customMetric1:4.3539 - customMetric2:27.3860

Epoch 3/5 60000/60000 [==============================] - 5s 82us/sample - 損失:0.1095 - 准確度: 0.9663 - customMetric1: 4.3539 - customMetric2: 27.3881

Epoch 4/5 60000/60000 [==============================] - 5s 83us/sample - 損失:0.0891 - 准確度: 0.9717 - customMetric1: 4.3539 - customMetric2: 27.3893

Epoch 5/5 60000/60000 [==============================] - 5s 87us/sample - 損失:0.0745 - 准確度: 0.9765 - customMetric1: 4.3539 - customMetric2: 27.3901

10000/10000 [==============================] - 0s 46us/樣本 - 損失:0.0764 - 准確度:0.9775 - customMetric1 : 4.3429 - customMetric2: 27.3301 [0.07644735965565778, 0.9775, 4.342905, 27.330126]

編輯 1

例如,如果您想使用customMetric1作為自定義損失函數,請按如下方式更改幾項並運行代碼。 希望這可以幫助。

# Custom Metric1 (for example)
@tf.function()
def customMetric1(yTrue,yPred,name='CustomLoss1'):
  yTrue = tf.dtypes.cast(yTrue, tf.float32)
  return tf.reduce_mean(yTrue-yPred)

model.compile(optimizer='adam',loss=customMetric1, metrics=['accuracy', customMetric2])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM