Пример #1
0
                      "w"))
            print("\n".join(map(str, history.history["loss"])),
                  file=open(
                      os.path.join(save_path, "train_losses{}.txt".format(i)),
                      "w"))
            print("\n".join(map(str, history.history["val_acc"])),
                  file=open(
                      os.path.join(save_path, "val_accs{}.txt".format(i)),
                      "w"))
            print("\n".join(map(str, history.history["val_loss"])),
                  file=open(
                      os.path.join(save_path, "val_losses{}.txt".format(i)),
                      "w"))

            train_pred = model.predict_classes(X=Xs["train"],
                                               batch_size=batch_size,
                                               verbose=0)
            train_perf = eval_pred(ys["train"], train_pred)
            for met in ["acc", "prec", "rec", "f1"]:
                av_train_perf[met] += float(
                    train_perf[met]) / args.ensemble_size

            val_pred = model.predict_classes(X=Xs["val"],
                                             batch_size=batch_size,
                                             verbose=0)
            val_perf = eval_pred(ys["val"], val_pred)
            for met in ["acc", "prec", "rec", "f1"]:
                av_val_perf[met] += float(val_perf[met]) / args.ensemble_size

        final_train_perfs[params] = av_train_perf
        final_val_perfs[params] = av_val_perf
        history = model.fit(
            X=np.concatenate((Xs["train"], Xs["val"])),
            y=np.concatenate((ys["train"], ys["val"])),
            batch_size=best_batch_size,
            nb_epoch=best_epochs,
            verbose=1,
            shuffle=True,
            show_accuracy=True,
        )

        model.save_weights(os.path.join(save_path, "weights{}.h5".format(i)), overwrite=True)
        print("\n".join(map(str, history.history["acc"])), file=open(os.path.join(save_path, "train_accs{}.txt".format(i)), "w"))
        print("\n".join(map(str, history.history["loss"])), file=open(os.path.join(save_path, "train_losses{}.txt".format(i)), "w"))

        pred = model.predict_classes(X=Xs["test"], batch_size=batch_size, verbose=0)
        preds[:, i] = pred[:, 0]

    final_pred = mode(preds, axis=1).mode
    test_perf = eval_pred(ys["test"], final_pred)
else:
    print("Building model")
    model = ShallowNet(Xs["train"].shape[1], best_dropout, best_dense_layers, best_dense_layer_units, args.weights)
    model.compile(optimizer=Adam(lr=best_lr), loss="binary_crossentropy")
    print("Model built")

    test_perf = eval_model(model, best_batch_size, Xs["test"], ys["test"])

print("Test perf: {}".format(test_perf))

if args.train == "true":
            shuffle=True,
            show_accuracy=True,
        )

        model.save_weights(os.path.join(save_path, "weights{}.h5".format(i)),
                           overwrite=True)
        print("\n".join(map(str, history.history["acc"])),
              file=open(os.path.join(save_path, "train_accs{}.txt".format(i)),
                        "w"))
        print("\n".join(map(str, history.history["loss"])),
              file=open(
                  os.path.join(save_path, "train_losses{}.txt".format(i)),
                  "w"))

        pred = model.predict_classes(X=Xs["test"],
                                     batch_size=batch_size,
                                     verbose=0)
        preds[:, i] = pred[:, 0]

    final_pred = mode(preds, axis=1).mode
    test_perf = eval_pred(ys["test"], final_pred)
else:
    print("Building model")
    model = ShallowNet(Xs["train"].shape[1], best_dropout, best_dense_layers,
                       best_dense_layer_units, args.weights)
    model.compile(optimizer=Adam(lr=best_lr), loss="binary_crossentropy")
    print("Model built")

    test_perf = eval_model(model, best_batch_size, Xs["test"], ys["test"])

print("Test perf: {}".format(test_perf))
Пример #4
0
                y=ys["train"],
                batch_size=batch_size,
                nb_epoch=epochs,
                verbose=1,
                validation_data=(Xs["val"], ys["val"]),
                shuffle=True,
                show_accuracy=True,
                callbacks=[LearningRateScheduler(lambda e: lr_schedule(epochs, lr, e))]
            )

            print("\n".join(map(str, history.history["acc"])), file=open(os.path.join(save_path, "train_accs{}.txt".format(i)), "w"))
            print("\n".join(map(str, history.history["loss"])), file=open(os.path.join(save_path, "train_losses{}.txt".format(i)), "w"))
            print("\n".join(map(str, history.history["val_acc"])), file=open(os.path.join(save_path, "val_accs{}.txt".format(i)), "w"))
            print("\n".join(map(str, history.history["val_loss"])), file=open(os.path.join(save_path, "val_losses{}.txt".format(i)), "w"))

            train_pred = model.predict_classes(X=Xs["train"], batch_size=batch_size, verbose=0)
            train_perf = eval_pred(ys["train"], train_pred)
            for met in ["acc", "prec", "rec", "f1"]:
                av_train_perf[met] += float(train_perf[met]) / args.ensemble_size

            val_pred = model.predict_classes(X=Xs["val"], batch_size=batch_size, verbose=0)
            val_perf = eval_pred(ys["val"], val_pred)
            for met in ["acc", "prec", "rec", "f1"]:
                av_val_perf[met] += float(val_perf[met]) / args.ensemble_size

        final_train_perfs[params] = av_train_perf
        final_val_perfs[params] = av_val_perf
        print("final train perf: acc {}, f1 {}; final val perf: acc {}, f1 {}".format(final_train_perfs[params]["acc"], final_train_perfs[params]["f1"], final_val_perfs[params]["acc"], final_val_perfs[params]["f1"]))

        with open(os.path.join(base_save_dir, "checkpoints", "final_train_perfs_{}.pickle".format(turn)), "wb") as sf:
            cPickle.dump(final_train_perfs, sf)