def test_multiple_classes(): gt_label = np.array([ [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], ]) preds = np.array([ [0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], ]) tensor_gt_label = tf.constant(gt_label, dtype=tf.float32) tensor_preds = tf.constant(preds, dtype=tf.float32) # Initialize mcc = MatthewsCorrelationCoefficient(3) # Update mcc.update_state(tensor_gt_label, tensor_preds) # Check results by comparing to results of scikit-learn matthew implementation. sklearn_result = sklearn_matthew(gt_label.argmax(axis=1), preds.argmax(axis=1)) check_results(mcc, sklearn_result)
def test_binary_classes(): gt_label = tf.constant([[1.0], [1.0], [1.0], [0.0]], dtype=tf.float32) preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], dtype=tf.float32) # Initialize mcc = MatthewsCorrelationCoefficient(1) # Update mcc.update_state(gt_label, preds) # Check results check_results(mcc, [-0.33333334])
def test_config(): # mcc object mcc1 = MatthewsCorrelationCoefficient(num_classes=1) assert mcc1.num_classes == 1 assert mcc1.dtype == tf.float32 # check configure mcc2 = MatthewsCorrelationCoefficient.from_config(mcc1.get_config()) assert mcc2.num_classes == 1 assert mcc2.dtype == tf.float32
def test_config(self): # mcc object mcc1 = MatthewsCorrelationCoefficient(num_classes=1) self.assertEqual(mcc1.num_classes, 1) self.assertEqual(mcc1.dtype, tf.float32) # check configure mcc2 = MatthewsCorrelationCoefficient.from_config(mcc1.get_config()) self.assertEqual(mcc2.num_classes, 1) self.assertEqual(mcc2.dtype, tf.float32)
def test_reset_states_graph(): gt_label = tf.constant([[1.0], [1.0], [1.0], [0.0]], dtype=tf.float32) preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], dtype=tf.float32) mcc = MatthewsCorrelationCoefficient(1) mcc.update_state(gt_label, preds) @tf.function def reset_states(): mcc.reset_states() reset_states() # Check results check_results(mcc, [0])
def test_multiple_classes(): gt_label = tf.constant( [[1.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [0.0, 1.0, 1.0]], dtype=tf.float32, ) preds = tf.constant( [[1.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0]], dtype=tf.float32, ) # Initialize mcc = MatthewsCorrelationCoefficient(3) mcc.update_state(gt_label, preds) # Check results check_results(mcc, [-0.33333334, 1.0, 0.57735026])
def test_keras_model(): model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(64, activation="relu")) model.add(tf.keras.layers.Dense(64, activation="relu")) model.add(tf.keras.layers.Dense(1, activation="softmax")) mcc = MatthewsCorrelationCoefficient(num_classes=1) model.compile(optimizer="Adam", loss="binary_crossentropy", metrics=["accuracy", mcc]) # data preparation data = np.random.random((10, 1)) labels = np.random.random((10, 1)) labels = np.where(labels > 0.5, 1.0, 0.0) model.fit(data, labels, epochs=1, batch_size=32, verbose=0)
def initialize_vars(self, n_classes): mcc = MatthewsCorrelationCoefficient(num_classes=n_classes) self.evaluate(tf.compat.v1.variables_initializer(mcc.variables)) return mcc