def train(epochs): train_ds, test_ds, field_dict, field_index = get_data() model = DeepFM(embedding_size=config.EMBEDDING_SIZE, num_feature=len(field_index), num_field=len(field_dict), field_index=field_index) optimizer = tf.keras.optimizers.SGD(learning_rate=0.01) print("Start Training: Batch Size: {}, Embedding Size: {}".format(config.BATCH_SIZE, config.EMBEDDING_SIZE)) start = perf_counter() for i in range(epochs): acc = BinaryAccuracy(threshold=0.5) auc = AUC() loss_history = [] for x, y in train_ds: loss = train_on_batch(model, optimizer, acc, auc, x, y) loss_history.append(loss) print("Epoch {:03d}: 누적 Loss: {:.4f}, Acc: {:.4f}, AUC: {:.4f}".format( i, np.mean(loss_history), acc.result().numpy(), auc.result().numpy())) test_acc = BinaryAccuracy(threshold=0.5) test_auc = AUC() for x, y in test_ds: y_pred = model(x) test_acc.update_state(y, y_pred) test_auc.update_state(y, y_pred) print("테스트 ACC: {:.4f}, AUC: {:.4f}".format(test_acc.result().numpy(), test_auc.result().numpy())) print("Batch Size: {}, Embedding Size: {}".format(config.BATCH_SIZE, config.EMBEDDING_SIZE)) print("걸린 시간: {:.3f}".format(perf_counter() - start)) model.save_weights('weights/weights-epoch({})-batch({})-embedding({}).h5'.format( epochs, config.BATCH_SIZE, config.EMBEDDING_SIZE))
def get_metrics(y, y_pred): cnf_matrix = confusion_matrix(y, y_pred) false_positive = cnf_matrix.sum( axis=0) - np.diag(cnf_matrix).astype(float) false_negative = cnf_matrix.sum( axis=1) - np.diag(cnf_matrix).astype(float) true_positive = np.diag(cnf_matrix).astype(float) true_negative = ( cnf_matrix.sum() - (false_positive + false_negative + true_positive)).astype(float) y = to_categorical(y, num_classes=5) y_pred = to_categorical(y_pred, num_classes=5) auc = AUC() _ = auc.update_state(y, y_pred) acc = CategoricalAccuracy() _ = acc.update_state(y, y_pred) return { 'accuracy': acc.result().numpy(), 'auc': auc.result().numpy(), 'sensitivity': true_positive / (true_positive + false_negative), 'specificity': true_negative / (true_negative + false_positive) }
def get_auc(label_value, predict_value): m = AUC() _ = m.update_state(y_true=label_value, y_pred=predict_value) auc = m.result().numpy() return auc