Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discriminative metric #45

Merged
merged 6 commits into from
May 24, 2024
Merged

Fix discriminative metric #45

merged 6 commits into from
May 24, 2024

Conversation

liyiersan
Copy link
Contributor

I was trying to use the discriminative metric in tsgm following evaluation.ipynb. I found in most cases, the results would be very close to 0.5. Besides, there would be a warning about using softmax in one class classification and the training loss was abnormal.
I have checked the source codes of classification models in the zoo, and I find that all models utilize softmax activation before the output layer.

 m_output = layers.Dense(self._output_dim, activation="softmax")(x)

When setting the output_dim to 1 following evaluation.ipynb for classification models, the output would be all ones, in which the classification model would not work.

# use LSTM classification model from TSGM zoo.
model = tsgm.models.zoo["clf_cl_n"](
    seq_len=Xr.shape[1], feat_dim=Xr.shape[2], output_dim=1).model # set output_dim to 1 would not work for softmax
model.compile(
    tf.keras.optimizers.Adam(),
    tf.keras.losses.CategoricalCrossentropy(from_logits=False)
)

If directly set output_dim to 2, there would be a size error for metric calculation. I think this is due to the implementation of DiscriminativeMetric:

 	y_pred = (model.predict(X_test) > 0.5).astype(int)
    if metric is None:
        return sklearn.metrics.accuracy_score(y_test, y_pred)
    else:
        return metric(y_test, y_pred)

This version is suitable for binary classification with sigmoid activation. Therefore, I recommend modifying it so that it also works with softmax.

        # check the shape, 1D array or N-D arrary
        if len(pred.shape) == 1: # binary classification with sigmoid activation
            y_pred = (pred > 0.5).astype(int)
        else: # multiple classification with softmax activation
            y_pred = np.argmax(pred, axis=-1).astype(int)
        if metric is None:
            return sklearn.metrics.accuracy_score(y_test, y_pred)
        else:
            return metric(y_test, y_pred)

Besides, the definition of the classification model should be changed as follows:

# use LSTM classification model from TSGM zoo.
model = tsgm.models.zoo["clf_cl_n"](
    # set output_dim to 2 so that softmax can work properly
    seq_len=Xr.shape[1], feat_dim=Xr.shape[2], output_dim=2).model 
model.compile(
    tf.keras.optimizers.Adam(),
    # SparseCategoricalCrossentropy for multiple classes
    tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
)

@liyiersan
Copy link
Contributor Author

In addition, function test_discriminative_metric() in test_metrics.py should be updated:

def test_discriminative_metric():
    ts = np.sin(np.arange(10)[:, None, None] + np.arange(6)[None, :, None])  # sin_sequence, [10, 6, 3]
    D1 = tsgm.dataset.Dataset(ts, y=None)

    diff_ts = np.sin(np.arange(10)[:, None, None] + np.arange(6)[None, :, None]) + 1000  # sin_sequence, [10, 6, 3]
    D2 = tsgm.dataset.Dataset(diff_ts, y=None)
    # output_dim =2 for softmax
    model = tsgm.models.zoo["clf_cl_n"](seq_len=ts.shape[1], feat_dim=ts.shape[2], output_dim=2).model
    model.compile(
        tf.keras.optimizers.Adam(),
        # SparseCategoricalCrossentropy for multiple classes
        tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
    )
    discr_metric = tsgm.metrics.DiscriminativeMetric()
    # should be easy to be classified 
    assert discr_metric(d_hist=D1, d_syn=D2, model=model, test_size=0.2, random_seed=42, n_epochs=5) == 1.0
    assert discr_metric(d_hist=D1, d_syn=D2, model=model, metric=sklearn.metrics.precision_score, test_size=0.2, random_seed=42, n_epochs=5) == 1.0

@AlexanderVNikitin AlexanderVNikitin self-assigned this May 24, 2024
@AlexanderVNikitin AlexanderVNikitin merged commit 6237a0f into AlexanderVNikitin:main May 24, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants