def test_mc_5_classes(): actuals = tf.constant( [ [1, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], ], dtype=tf.float32, ) predictions = tf.constant( [ [0.85, 0, 0.15, 0, 0], [0, 0, 0, 1, 0], [0, 1, 0, 0, 0], [0.05, 0.90, 0.04, 0, 0.01], [0.10, 0, 0.81, 0.09, 0], [0.10, 0.045, 0, 0.81, 0.045], [1, 0, 0, 0, 0], [0, 0.85, 0, 0, 0.15], ], dtype=tf.float32, ) # Initialize hl_obj = HammingLoss("multiclass", threshold=0.8) hl_obj.update_state(actuals, predictions) # Check results check_results(hl_obj, 0.25)
def test_ml_5_classes(): actuals = tf.constant( [ [1, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 1, 0, 1, 0], [0, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 1, 1, 0], [1, 0, 0, 0, 1], [0, 1, 1, 0, 0], ], dtype=tf.float32, ) predictions = tf.constant( [ [1, 0.75, 0.2, 0.55, 0], [0.65, 0.22, 0.97, 0.88, 0], [0, 1, 0, 1, 0], [0, 0.85, 0.9, 0.34, 0.5], [0.4, 0.65, 0.87, 0, 0.12], [0.66, 0.55, 1, 0.98, 0], [0.95, 0.34, 0.67, 0.65, 0.10], [0.45, 0.97, 0.89, 0.67, 0.46], ], dtype=tf.float32, ) # Initialize hl_obj = HammingLoss("multilabel", threshold=0.7) hl_obj.update_state(actuals, predictions) # Check results check_results(hl_obj, 0.075)
def test_ml_4_classes(): actuals = tf.constant([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 0, 1]], dtype=tf.float32) predictions = tf.constant( [[0.97, 0.56, 0.83, 0.77], [0.34, 0.95, 0.7, 0.89], [0.95, 0.45, 0.23, 0.56]], dtype=tf.float32, ) # Initialize hl_obj = HammingLoss("multilabel", threshold=0.8) hl_obj.update_state(actuals, predictions) # Check results check_results(hl_obj, 0.16666667)
def test_keras_model(): model = tf.keras.Sequential() model.add(layers.Dense(64, activation="relu")) model.add(layers.Dense(3, activation="softmax")) h1 = HammingLoss(mode="multiclass") model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=[h1]) data = np.random.random((100, 10)) labels = np.random.random((100, 3)) model.fit(data, labels, epochs=1, batch_size=32, verbose=0)
def initialize_vars(self, mode, threshold): hl_obj = HammingLoss(mode=mode, threshold=threshold) return hl_obj
def test_config(self): hl_obj = HammingLoss(mode='multilabel', threshold=0.8) self.assertEqual(hl_obj.name, 'hamming_loss') self.assertEqual(hl_obj.dtype, tf.float32)
break return metrics #------------------------------------------------# #------------------------------------------------# # Init # #------------------------------------------------# s_optimizer = Adam(0.0002) train_loss = Mean(name='train_loss') test_loss = Mean(name='test_loss') train_accuracy = BinaryAccuracy(name='train_accuracy') test_accuracy = BinaryAccuracy(name='test_accuracy') hl = HammingLoss(mode='multilabel', threshold=0.5) f1micro = F1Score(5, average='micro', name='f1_micro', threshold=0.5) ema = ExponentialMovingAverage(0.99) es=early(30,80) #------------------------------------------------# #------------------------------------------------# # Load # #------------------------------------------------# # UKDALE uk_dale = DataSet(os.path.join(PATH_TO_DATASET_DIR,'dale/ukdale.h5')) appliances = ['kettle', 'microwave', 'dish washer', 'washing machine', 'fridge'] dh1 = house(uk_dale, appliances, 1, '2016-06-01', '2016-08-31') dh2 = house(uk_dale, appliances, 2, '2013-06-01', '2013-08-05')
def test_config(): hl_obj = HammingLoss(mode="multilabel", threshold=0.8) assert hl_obj.name == "hamming_loss" assert hl_obj.dtype == tf.float32