labels = le.fit_transform(data["names"])
num_classes = len(np.unique(labels))
labels = labels.reshape(-1, 1)
one_hot_encoder = OneHotEncoder(categorical_features=[0])
labels = one_hot_encoder.fit_transform(labels).toarray()

embeddings = np.array(data["embeddings"])

# Initialize Softmax training model arguments
BATCH_SIZE = 32
EPOCHS = 20
input_shape = embeddings.shape[1]

# Build sofmax classifier
softmax = SoftMax(input_shape=(input_shape, ), num_classes=num_classes)
model = softmax.build()

# Create KFold
cv = KFold(n_splits=5, random_state=42, shuffle=True)
history = {'acc': [], 'val_acc': [], 'loss': [], 'val_loss': []}
# Train
for train_idx, valid_idx in cv.split(embeddings):
    X_train, X_val, y_train, y_val = embeddings[train_idx], embeddings[
        valid_idx], labels[train_idx], labels[valid_idx]
    his = model.fit(X_train,
                    y_train,
                    batch_size=BATCH_SIZE,
                    epochs=EPOCHS,
                    verbose=1,
                    validation_data=(X_val, y_val))
    print(his.history['acc'])
Exemplo n.º 2
0
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.20, random_state=123)

X_train, X_test, y_train__, y_test__ = train_test_split(X,y__, test_size = 0.20, random_state=123) # softmax


model = []

input_shape = X.shape[1]

model_svm = pickle.loads(open(args.folder + "svm.pickle", "rb").read())
model_rf = pickle.loads(open(args.folder + "rf.pickle", "rb").read())
model_gb = pickle.loads(open(args.folder + "gb.pickle", "rb").read())
model_vt = pickle.loads(open(args.folder + "vt.pickle", "rb").read())

softmax = SoftMax(input_shape=(input_shape,), num_classes=num_classes)
models = softmax.build()

model_s1 = load_model(args.folder + "best_fold_1.hdf5")
model_s2 = load_model(args.folder + "best_fold_2.hdf5")
model_s3 = load_model(args.folder + "best_fold_3.hdf5")
model_s4 = load_model(args.folder + "best_fold_4.hdf5")
model_s5 = load_model(args.folder + "best_fold_5.hdf5")

model.append(model_s1)
model.append(model_s2)
model.append(model_s3)
model.append(model_s4)
model.append(model_s5)

model.append(model_svm)
model.append(model_rf)