【问题标题】:How to use Tensorflow addons' metrics correctly in functional API?如何在功能 API 中正确使用 Tensorflow 插件的指标?
【发布时间】:2019-12-27 06:34:32
【问题描述】:

我有一个 LSTM 模型,可以使用多元智能手机传感器数据对人类活动进行二元分类。这两个类别不平衡(1:50)。因此,我想使用 F1 分数作为指标,但我看到它已被弃用。

以前是best practice to use a callback function 用于确保将其应用于整个数据集的度量标准,但是,最近是TensorFlow addons reintroduced the F1-Score

我现在无法将此分数应用于我的功能 API。这是我目前正在运行的代码:

import tensorflow as tf 
import tensorflow_addons as tfa
from tensorflow import kerasdef

create_model(n_neurons=150, learning_rate=0.01, activation="relu", loss="binary_crossentropy"):

   #create input layer and assign to current output layer
   input_ = keras.layers.Input(shape=(X_train.shape[1],X_train.shape[2])) 

   #add LSTM layer
   lstm = keras.layers.LSTM(n_neurons, activation=activation)(input_)

   #Output Layer
   output = keras.layers.Dense(1, activation="sigmoid")(lstm)

   #Create Model
   model = keras.models.Model(inputs=[input_], outputs=[output])

   #Add optimizer
   optimizer=keras.optimizers.SGD(lr=learning_rate, clipvalue=0.5)

   #Compile model
   model.compile(loss=loss, optimizer=optimizer, metrics=[tfa.metrics.F1Score(num_classes=2, average="micro")])

   print(model.summary())

   return model

#Create the model
model = create_model()

#fit the model
history = model.fit(X_train,y_train, 
                epochs=300, 
                validation_data=(X_val, y_val))

如果我对度量参数 average 使用另一个值(例如,average=Noneaverage="macro"),那么在拟合模型时会收到一条错误消息:

ValueError: 两个形状中的维度 0 必须相等,但是是 2 和 1。形状是 [2] 和 [1]。对于具有输入形状的“AssignAddVariableOp”(操作:“AssignAddVariableOp”):[]、[1]。

如果我使用值 average="micro",我不会收到错误,但在整个学习过程中 F1 分数是 0,而我的损失会减少。

我相信我在这里仍然做错了什么。谁能给我解释一下?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    更新答案:关键是导入 tf.keras,而不是 keras。然后你可以使用例如tf.keras.metrics.Precision 或 tfa.metrics.F1Score 没有问题。另见here

    旧答案: tensorflow-addons 的问题是当前版本(0.6.0)的实现只计算完全匹配,例如比较1 和 0.99 的结果为 0。当然,这在神经网络中实际上是无用的。这已在 0.7.0(尚未发布)中修复。您可以按如下方式安装:

    pip3 install --upgrade pip
    pip3 install tfa-nightly
    

    然后使用一个阈值(低于阈值的都算0,否则算1):

    tfa.metrics.FBetaScore(num_classes=2,average="micro",threshold=0.9)
    

    另见https://github.com/tensorflow/addons/issues/490average 的其他值的问题在这里讨论:https://github.com/tensorflow/addons/issues/746

    注意还有另外两个问题可能导致无用的结果,另见https://github.com/tensorflow/addons/issues/818

    1. 模型使用二进制分类,但 tfa 中的 f1-score 假定使用 one-hot 编码进行分类分类
    2. 在验证的每个批次步骤中都会调用 f1-score。

    使用 Keras 指标时不应出现这些问题。

    【讨论】:

      【解决方案2】:

      在这个例子中,我将展示如何使用 Tensorflow 使用 Adamw 优化器和 F1_score。

      lr_schedule = keras.optimizers.schedules.ExponentialDecay(
          initial_learning_rate, decay_steps=100000, decay_rate=0.96, staircase=True
      )
      import tensorflow_addons as tfa
      f1 = tfa.metrics.F1Score(num_classes=2, average=None)
      
      model=(..)
      model.compile(
          loss="binary_crossentropy",
          optimizer=tfa.optimizers.AdamW(learning_rate=lr_schedule,weight_decay = 0.0001),
          metrics=["acc",'AUC',f1],
      )
      

      【讨论】:

        猜你喜欢
        • 2020-06-15
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        • 2022-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多