4

I'm training a model using TensorFlow 2.0 using tf.GradientTape(), but I find that the model's accuracy is 95% if I use tf.keras.losses.BinaryCrossentropy, but degrade to 75% if I use tf.keras.losses.binary_crossentropy. So I'm confused about the difference about the same metric here?

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers

from sklearn.model_selection import train_test_split

def read_data():
    red_wine = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=";")
    white_wine = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv", sep=";")
    red_wine["type"] = 1
    white_wine["type"] = 0
    wines = red_wine.append(white_wine)
    return wines

def get_x_y(df):
    x = df.iloc[:, :-1].values.astype(np.float32)
    y = df.iloc[:, -1].values.astype(np.int32)
    return x, y

def build_model():
    inputs = layers.Input(shape=(12,))
    dense1 = layers.Dense(12, activation="relu", name="dense1")(inputs)
    dense2 = layers.Dense(9, activation="relu", name="dense2")(dense1)
    outputs = layers.Dense(1, activation = "sigmoid", name="outputs")(dense2)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    return model

def generate_dataset(df, batch_size=32, shuffle=True, train_or_test = "train"):
    x, y = get_x_y(df)
    ds = tf.data.Dataset.from_tensor_slices((x, y))
    if shuffle:
        ds = ds.shuffle(10000)
    if train_or_test == "train":
        ds = ds.batch(batch_size)
    else:
        ds = ds.batch(len(df))
    return ds

# loss_object = tf.keras.losses.binary_crossentropy
loss_object = tf.keras.losses.BinaryCrossentropy()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)

def train_step(model, optimizer, x, y):
    with tf.GradientTape() as tape:
        pred = model(x, training=True)
        loss = loss_object(y, pred)
    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))


def train_model(model, train_ds, epochs=10):
    for epoch in range(epochs):
        print(epoch)
        for x, y in train_ds:
            train_step(model, optimizer, x, y)

def main():
    data = read_data()
    train, test = train_test_split(data, test_size=0.2, random_state=23)
    train_ds = generate_dataset(train, 32, True, "train")
    test_ds = generate_dataset(test, 32, False, "test")
    model = build_model()
    train_model(model, train_ds, 10)
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy']
                  )
    model.evaluate(test_ds)

main()
Rogers
  • 81
  • 1
  • 5
  • What is the question here? – D. Lawrence Jan 06 '20 at 13:40
  • Hi, the problem is that I change the loss_object from `tf.keras.losses.BinaryCrossentropy()` to `tf.keras.losses.binary_crossentropy`, the accuracy is reduced from `95%` to `75` – Rogers Jan 06 '20 at 13:47

2 Answers2

3

They should indeed work the same; BinaryCrossentropy uses binary_crossentropy, with difference apparent in docstring descriptions; former's intended for two class labels, whereas later supports an arbitrary class count. However, if passing in targets in expected format, both apply same preprocessing before calling backend's binary_crossentropy, which does the actual computing.

The difference you observe is likely a reproducibility issue; ensure you set the random seed - see function below. For a more complete answer on reproducibility, see here.


Function

def reset_seeds(reset_graph_with_backend=None):
    if reset_graph_with_backend is not None:
        K = reset_graph_with_backend
        K.clear_session()
        tf.compat.v1.reset_default_graph()
        print("KERAS AND TENSORFLOW GRAPHS RESET")  # optional

    np.random.seed(1)
    random.seed(2)
    tf.compat.v1.set_random_seed(3)
    print("RANDOM SEEDS RESET")  # optional

Usage:

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

reset_seeds(K)
OverLordGoldDragon
  • 1
  • 9
  • 53
  • 101
  • Thanks, but I think it's not the problem here, I run the code many times, but the accuracy is reduced from `95%` to `75%` when switch `loss_object` from `tf.keras.losses.BinaryCrossentropy()` to `tf.keras.losses.binary_crossentropy`. – Rogers Jan 06 '20 at 13:49
  • @JunkaiSun Share your full code; too many possible sources of error to guess – OverLordGoldDragon Jan 06 '20 at 13:54
  • Hi, I paste the full code in the question, you can test the program in your computer. The evaluation metric is reduced from 95% to 75% when switch `loss_object` from `tf.keras.losses.BinaryCrossentropy()` to `tf.keras.losses.binary_crossentropy`. – Rogers Jan 06 '20 at 14:03
  • @OverLordGoldDragon 'docstring descriptions', can you share those for both? I just found this on TF website:https://www.tensorflow.org/api_docs/python/tf/keras/losses/BinaryCrossentropy – Dr Nisha Arora Nov 08 '20 at 04:05
  • @DrNishaArora Check their [clickable](https://i.stack.imgur.com/z3Ez0.png) links and code beneath docstrings. – OverLordGoldDragon Nov 08 '20 at 23:20
1

Thanks, I find the reasons of the inconsistent accuracy:

  1. The shape of outputs in the model is (None, 1), but the feeded label is (None, ), which cause a wrong meaning with python's broadcast mechanism.

  2. In the source code of tf.keras.losses.BinaryCrossentropy(), while calculating the loss, both y_pred and y_true are processed through a function called squeeze_or_expand_dimensions, which is lacked in tf.keras.losses.binary_crossentropy.

  3. Note: Take care that whether the shape is consistent between input data and model outputs.

Rogers
  • 81
  • 1
  • 5
  • Right, it's what I meant by "if passing in targets in expected format" - stepping through your code, confirmed your resolution. Nonetheless, do use random seeds, as results will still differ by a few percentage points otherwise. – OverLordGoldDragon Jan 06 '20 at 14:48