婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > tensorflow2 自定義損失函數使用的隱藏坑

tensorflow2 自定義損失函數使用的隱藏坑

熱門標簽:宿州電話機器人哪家好 西青語音電銷機器人哪家好 成都呼叫中心外呼系統哪家強 旅游廁所地圖標注怎么弄 南昌地圖標注 電梯新時達系統外呼顯示e 地圖標注與注銷 無錫智能外呼系統好用嗎 百應電話機器人總部

Keras的核心原則是逐步揭示復雜性,可以在保持相應的高級便利性的同時,對操作細節進行更多控制。當我們要自定義fit中的訓練算法時,可以重寫模型中的train_step方法,然后調用fit來訓練模型。

這里以tensorflow2官網中的例子來說明:

import numpy as np
import tensorflow as tf
from tensorflow import keras
x = np.random.random((1000, 32))
y = np.random.random((1000, 1))
class CustomModel(keras.Model):
    tf.random.set_seed(100)
    def train_step(self, data):
        # Unpack the data. Its structure depends on your model and
        # on what you pass to `fit()`.
        x, y = data

        with tf.GradientTape() as tape:
            y_pred = self(x, training=True)  # Forward pass
            # Compute the loss value
            # (the loss function is configured in `compile()`)
            loss = self.compiled_loss(y, y_pred, regularization_losses=self.losses)

        # Compute gradients
        trainable_vars = self.trainable_variables
        gradients = tape.gradient(loss, trainable_vars)
        # Update weights
        self.optimizer.apply_gradients(zip(gradients, trainable_vars))
        # Update metrics (includes the metric that tracks the loss)
        self.compiled_metrics.update_state(y, y_pred)
        # Return a dict mapping metric names to current value
        return {m.name: m.result() for m in self.metrics}
    


# Construct and compile an instance of CustomModel
inputs = keras.Input(shape=(32,))
outputs = keras.layers.Dense(1)(inputs)
model = CustomModel(inputs, outputs)
model.compile(optimizer="adam", loss=tf.losses.MSE, metrics=["mae"])

# Just use `fit` as usual

model.fit(x, y, epochs=1, shuffle=False)
32/32 [==============================] - 0s 1ms/step - loss: 0.2783 - mae: 0.4257

 
tensorflow.python.keras.callbacks.History at 0x7ff7edf6dfd0>

這里的loss是tensorflow庫中實現了的損失函數,如果想自定義損失函數,然后將損失函數傳入model.compile中,能正常按我們預想的work嗎?

答案竟然是否定的,而且沒有錯誤提示,只是loss計算不會符合我們的預期。

def custom_mse(y_true, y_pred):
    return tf.reduce_mean((y_true - y_pred)**2, axis=-1)
a_true = tf.constant([1., 1.5, 1.2])
a_pred = tf.constant([1., 2, 1.5])
custom_mse(a_true, a_pred)
tf.Tensor: shape=(), dtype=float32, numpy=0.11333332>
tf.losses.MSE(a_true, a_pred)
tf.Tensor: shape=(), dtype=float32, numpy=0.11333332>

以上結果證實了我們自定義loss的正確性,下面我們直接將自定義的loss置入compile中的loss參數中,看看會發生什么。

my_model = CustomModel(inputs, outputs)
my_model.compile(optimizer="adam", loss=custom_mse, metrics=["mae"])
my_model.fit(x, y, epochs=1, shuffle=False)
32/32 [==============================] - 0s 820us/step - loss: 0.1628 - mae: 0.3257

tensorflow.python.keras.callbacks.History at 0x7ff7edeb7810>

我們看到,這里的loss與我們與標準的tf.losses.MSE明顯不同。這說明我們自定義的loss以這種方式直接傳遞進model.compile中,是完全錯誤的操作。

正確運用自定義loss的姿勢是什么呢?下面揭曉。

loss_tracker = keras.metrics.Mean(name="loss")
mae_metric = keras.metrics.MeanAbsoluteError(name="mae")

class MyCustomModel(keras.Model):
    tf.random.set_seed(100)
    def train_step(self, data):
        # Unpack the data. Its structure depends on your model and
        # on what you pass to `fit()`.
        x, y = data

        with tf.GradientTape() as tape:
            y_pred = self(x, training=True)  # Forward pass
            # Compute the loss value
            # (the loss function is configured in `compile()`)
            loss = custom_mse(y, y_pred)
            # loss += self.losses

        # Compute gradients
        trainable_vars = self.trainable_variables
        gradients = tape.gradient(loss, trainable_vars)
        # Update weights
        self.optimizer.apply_gradients(zip(gradients, trainable_vars))
        
        # Compute our own metrics
        loss_tracker.update_state(loss)
        mae_metric.update_state(y, y_pred)
        return {"loss": loss_tracker.result(), "mae": mae_metric.result()}
    
    @property
    def metrics(self):
        # We list our `Metric` objects here so that `reset_states()` can be
        # called automatically at the start of each epoch
        # or at the start of `evaluate()`.
        # If you don't implement this property, you have to call
        # `reset_states()` yourself at the time of your choosing.
        return [loss_tracker, mae_metric]
    
# Construct and compile an instance of CustomModel
inputs = keras.Input(shape=(32,))
outputs = keras.layers.Dense(1)(inputs)
my_model_beta = MyCustomModel(inputs, outputs)
my_model_beta.compile(optimizer="adam")

# Just use `fit` as usual

my_model_beta.fit(x, y, epochs=1, shuffle=False)
32/32 [==============================] - 0s 960us/step - loss: 0.2783 - mae: 0.4257

tensorflow.python.keras.callbacks.History at 0x7ff7eda3d810>

終于,通過跳過在 compile() 中傳遞損失函數,而在 train_step 中手動完成所有計算內容,我們獲得了與之前默認tf.losses.MSE完全一致的輸出,這才是我們想要的結果。

總結一下,當我們在模型中想用自定義的損失函數,不能直接傳入fit函數,而是需要在train_step中手動傳入,完成計算過程。

到此這篇關于tensorflow2 自定義損失函數使用的隱藏坑的文章就介紹到這了,更多相關tensorflow2 自定義損失函數內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 一小時學會TensorFlow2之基本操作1實例代碼
  • 詳解TensorFlow2實現前向傳播
  • 詳解TensorFlow2實現線性回歸
  • tensorflow2.0實現復雜神經網絡(多輸入多輸出nn,Resnet)
  • tensorflow2.0教程之Keras快速入門
  • 一小時學會TensorFlow2基本操作之合并分割與統計

標簽:辛集 西安 許昌 濰坊 渭南 贛州 七臺河 雅安

巨人網絡通訊聲明:本文標題《tensorflow2 自定義損失函數使用的隱藏坑》,本文關鍵詞  tensorflow2,自定義,損失,函數,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《tensorflow2 自定義損失函數使用的隱藏坑》相關的同類信息!
  • 本頁收集關于tensorflow2 自定義損失函數使用的隱藏坑的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 金阳县| 山阳县| 克什克腾旗| 罗田县| 溆浦县| 仙居县| 淳化县| 德安县| 昭平县| 安化县| 报价| 饶河县| 中超| 武强县| 班玛县| 广汉市| 黑河市| 吉安市| 奇台县| 阳朔县| 威宁| 福鼎市| 靖远县| 岳阳县| 丹棱县| 侯马市| 赣榆县| 秀山| 沽源县| 新化县| 墨玉县| 玉山县| 锦州市| 习水县| 临泽县| 白朗县| 东兴市| 南川市| 荣昌县| 南陵县| 霍林郭勒市|