<jupyter_start><jupyter_text>Mnist<jupyter_code>(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((60000, 28, 28, 1)) / 255.0
x_test = x_test.reshape((10000, 28, 28, 1)) / 255.0
print(x_train.shape)
print(x_test.shape)


def handle_data(ood_class: int):
    # id = 1, ood = -1
    index = np.array([e != ood_class for e in y_train])
    new_x_train, new_y_train = x_train[index], y_train[index]
    for i in range(len(new_y_train)):
        new_y_train[i] = new_y_train[i] - 1 if new_y_train[i] > ood_class else new_y_train[i]
    
    new_ood_x_test_1 = x_train[index == False]
    
    index = np.array([e != ood_class for e in y_test])
    new_ood_x_test_2 = x_test[index == False]
    new_ood_x_test = list(new_ood_x_test_1) + list(new_ood_x_test_2)
    new_id_x_test = x_test[index][:len(new_ood_x_test)]
    
    new_x_test = np.array(list(new_id_x_test) + list(new_ood_x_test))
    new_y_test = np.array([1 for _ in range(len(new_id_x_test))] + [-1 for _ in range(len(new_ood_x_test))])
    new_x_test, new_y_test = shuffle(new_x_test, new_y_test)
    
    # new_y_train：[0,1,...,8], new_y_test: {-1, 1}
    return new_x_train, new_y_train, new_x_test, new_y_test
for ood_class in range(10):
    new_x_train, new_y_train, new_x_test, new_y_test = handle_data(ood_class)
    x_val = new_x_train[50000:]
    y_val = new_y_train[50000:]
    new_x_train = new_x_train[:50000]
    new_y_train = new_y_train[:50000]
    for ite in range(1):
        print('*' * 30, 'ood={}, ite={}'.format(ood_class, ite), '*' * 30)
        saved_model_name = './saved_model/mnist_ood{}_ite{}.h5'.format(ood_class, ite)

        min_recon_err = float('inf')
        model = get_lamae_model()
        model.fit(new_x_train, {'recon': new_x_train, 'clf': new_y_train},
              shuffle=True,
              epochs=200,
              batch_size=128,
              verbose=0,
              callbacks = [LAMAECallback(saved_model_name)])
        
        # Testing
        model = load_model(saved_model_name, custom_objects={'MemoryUnit': MemoryUnit})        
        x_test_recon, _ = model.predict(new_x_test)
        recon_err = np.mean(np.square(new_x_test - x_test_recon), axis=(1, 2, 3))
        
        # LAMAE
        auroc = round(100 * roc_auc_score(new_y_test, -recon_err), 2)
        auprc = round(100 * average_precision_score(new_y_test, -recon_err), 2)
        print('LAMAE test set, ood={}, ite={}, auroc={}%, auprc={}%'.format(ood_class, ite, auroc, auprc))
        
        # LAMAE+
        entropys = np.array([shannon_entropy(e) for e in new_x_test])
        sigma = 1e-9
        auroc = round(100 * roc_auc_score(new_y_test, -recon_err / (sigma + np.log(entropys + 1))), 2)
        auprc = round(100 * average_precision_score(new_y_test, -recon_err / (sigma + np.log(entropys + 1))), 2)
        print('LAMAE+ test set, ood={}, ite={}, auroc={}%, auprc={}%'.format(ood_class, ite, auroc, auprc))
        print()<jupyter_output>****************************** ood=0, ite=0 ******************************
save model, epoch=0, recon_err_mean=0.07304661010102313
save model, epoch=1, recon_err_mean=0.06439831797065132
save model, epoch=2, recon_err_mean=0.06438556842932208
save model, epoch=4, recon_err_mean=0.06434732218505604
save model, epoch=6, recon_err_mean=0.06434575164646973
save model, epoch=8, recon_err_mean=0.06429845968085995
save model, epoch=10, recon_err_mean=0.062117923859081485
save model, epoch=11, recon_err_mean=0.05589562052728274
save model, epoch=12, recon_err_mean=0.05273641010001821
save model, epoch=18, recon_err_mean=0.0523946469219118
save model, epoch=23, recon_err_mean=0.05007775427277692
save model, epoch=30, recon_err_mean=0.039101891806901615
save model, epoch=39, recon_err_mean=0.03816913532914424
save model, epoch=46, recon_err_mean=0.037245903512328704
save model, epoch=48, recon_err_mean=0.03567006885536443
save model, epoch=54, recon_err_mean=0.03383220303225055
save model, epoch[...]<jupyter_text>Fashion Mnist<jupyter_code>(x_train, y_train), (id_x, _) = fashion_mnist.load_data()
x_train = x_train.reshape((60000, 28, 28, 1)) / 255.0
id_x = id_x.reshape((10000, 28, 28, 1)) / 255.0

x_val = x_train[50000:]
y_val = y_train[50000:]
new_x_train = x_train[:50000]
new_y_train = y_train[:50000]

(_, _), (ood_x, _) = mnist.load_data()
ood_x = ood_x.reshape((10000, 28, 28, 1)) / 255.0

new_x_test = np.array(list(id_x) + list(ood_x))
new_y_test = np.array([1 for _ in range(len(id_x))] + [-1 for _ in range(len(ood_x))])

for ite in range(1):
    print('*' * 30, 'ite={}'.format(ite), '*' * 30)
    saved_model_name = './saved_model/fashion_mnist_ite{}.h5'.format(ite)

    min_recon_err = float('inf')
    model = get_lamae_model(class_num=10)
    model.fit(new_x_train, {'recon': new_x_train, 'clf': new_y_train},
          shuffle=True,
          epochs=200,
          batch_size=128,
          verbose=0,
          callbacks = [LAMAECallback(saved_model_name)])

    # Testing
    model = load_model(saved_model_name, custom_objects={'MemoryUnit': MemoryUnit})        
    x_test_recon, _ = model.predict(new_x_test)
    recon_err = np.mean(np.square(new_x_test - x_test_recon), axis=(1, 2, 3))

    # LAMAE
    auroc = round(100 * roc_auc_score(new_y_test, -recon_err), 2)
    auprc = round(100 * average_precision_score(new_y_test, -recon_err), 2)
    print('LAMAE test set, ite={}, auroc={}%, auprc={}%'.format(ite, auroc, auprc))

    # LAMAE+
    entropys = np.array([shannon_entropy(e) for e in new_x_test])
    sigma = 1e-9
    auroc = round(100 * roc_auc_score(new_y_test, -recon_err / (sigma + np.log(entropys + 1))), 2)
    auprc = round(100 * average_precision_score(new_y_test, -recon_err / (sigma + np.log(entropys + 1))), 2)
    print('LAMAE+ test set, ite={}, auroc={}%, auprc={}%'.format(ite, auroc, auprc))
    print()<jupyter_output>****************************** ite=0 ******************************
save model, epoch=0, recon_err_mean=0.10740130426265644
save model, epoch=1, recon_err_mean=0.08917269044603907
save model, epoch=3, recon_err_mean=0.044029929882564225
save model, epoch=9, recon_err_mean=0.0427236279861599
save model, epoch=12, recon_err_mean=0.041053776987119255
save model, epoch=15, recon_err_mean=0.0400308906560903
save model, epoch=25, recon_err_mean=0.039979674336593406
save model, epoch=31, recon_err_mean=0.03202413620805771
save model, epoch=42, recon_err_mean=0.027136426937306543
save model, epoch=51, recon_err_mean=0.026271429139769424
save model, epoch=56, recon_err_mean=0.024075263515351372
save model, epoch=59, recon_err_mean=0.024062929630682173
save model, epoch=68, recon_err_mean=0.023059636714099503
save model, epoch=71, recon_err_mean=0.022979406500009788
save model, epoch=87, recon_err_mean=0.022644260849935566
save model, epoch=89, recon_err_mean=0.02221079706781963
save model, epoc[...]<jupyter_text>CIFAR10<jupyter_code>def handle_fashion(ood_x):
    # 28 * 28 -> 32 * 32 * 3
    images = []

    for t in range(len(ood_x)):
        img = np.zeros((32, 32, 3), dtype=int)
        for i in range(28):
            for j in range(28):
                img[i + 2][j + 2][0] = ood_x[t][i][j]
                img[i + 2][j + 2][1] = ood_x[t][i][j]
                img[i + 2][j + 2][2] = ood_x[t][i][j]
        img = img / 255.0
        images.append(img)

    return np.array(images)
(x_train, y_train), (id_x, _) = cifar10.load_data()
x_train, id_x = x_train / 255.0, id_x / 255.0
y_train = y_train.reshape(y_train.shape[0])

x_val = x_train[45000:]
y_val = y_train[45000:]
new_x_train = x_train[:45000]
new_y_train = y_train[:45000]

(_, _), (ood_x, _) = fashion_mnist.load_data()
ood_x = handle_fashion(ood_x)

new_x_test = np.array(list(id_x) + list(ood_x))
new_y_test = np.array([1 for _ in range(len(id_x))] + [-1 for _ in range(len(ood_x))])

for ite in range(1):
    print('*' * 30, 'ite={}'.format(ite), '*' * 30)
    saved_model_name = './saved_model/cifar10_ite{}.h5'.format(ite)

    min_recon_err = float('inf')
    model = get_lamae_model(input_shape=(32, 32, 3), 
                            channels=(64, 128, 128, 256), 
                            latent_dim=64, 
                            skip_dim=16, 
                            class_num=10, 
                            block_size=50)
    model.fit(new_x_train, {'recon': new_x_train, 'clf': new_y_train},
          shuffle=True,
          epochs=200,
          batch_size=128,
          verbose=0,
          callbacks = [LAMAECallback(saved_model_name)])

    # Testing
    model = load_model(saved_model_name, custom_objects={'MemoryUnit': MemoryUnit})        
    x_test_recon, _ = model.predict(new_x_test)
    recon_err = np.mean(np.square(new_x_test - x_test_recon), axis=(1, 2, 3))

    # LAMAE
    auroc = round(100 * roc_auc_score(new_y_test, -recon_err), 2)
    auprc = round(100 * average_precision_score(new_y_test, -recon_err), 2)
    print('LAMAE test set, ite={}, auroc={}%, auprc={}%'.format(ite, auroc, auprc))

    # LAMAE+
    entropys = np.array([shannon_entropy(e) for e in new_x_test])
    sigma = 1e-9
    auroc = round(100 * roc_auc_score(new_y_test, -recon_err / (sigma + np.log(entropys + 1))), 2)
    auprc = round(100 * average_precision_score(new_y_test, -recon_err / (sigma + np.log(entropys + 1))), 2)
    print('LAMAE+ test set, ite={}, auroc={}%, auprc={}%'.format(ite, auroc, auprc))
    print()<jupyter_output>****************************** ite=0 ******************************
save model, epoch=0, recon_err_mean=0.045423547576021316
save model, epoch=1, recon_err_mean=0.02964047663231754
save model, epoch=2, recon_err_mean=0.0271723156510119
save model, epoch=6, recon_err_mean=0.026033273632340592
save model, epoch=7, recon_err_mean=0.02571689254719984
save model, epoch=8, recon_err_mean=0.02442935849129246
save model, epoch=10, recon_err_mean=0.02429117083186729
save model, epoch=11, recon_err_mean=0.024230629314131013
save model, epoch=13, recon_err_mean=0.023326100364181663
save model, epoch=18, recon_err_mean=0.022671189151649514
save model, epoch=25, recon_err_mean=0.022185325189091304
save model, epoch=28, recon_err_mean=0.021717012363867944
save model, epoch=37, recon_err_mean=0.02142850466613022
save model, epoch=43, recon_err_mean=0.021220117146877424
save model, epoch=56, recon_err_mean=0.021129029082184586
save model, epoch=57, recon_err_mean=0.021126819115736294
save model, epoch[...]
{'fraction_non_alphanumeric': 0.15399441071600656, 'fraction_numerical': 0.12498795412932447, 'mean_word_length': 4.137623762376237, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '603737', 'license': 'MIT', 'n_tokens_mistral': 5020, 'n_tokens_neox': 4229, 'n_words': 726, 'path': 'LAMAE.ipynb', 'repo_name': 'fzjcdt/LAMAE', 'size': '10377'}