Example #1
0
    def get_metrics_for_model(self):

        if self.model_name == 'MLP':
            self.model = MultiLayerPerceptron(self.x_train, self.y_train,
                                              self.x_test, self.y_test,
                                              self.x_val, self.y_val)
            self.model.train_model()
            self.model.predict_test()
            self.accuracy_test, self.accuracy_val = self.model.get_accuracy()
            self.recall_val, self.recall_test = self.model.get_out_of_scope_recall()

        elif self.model_name == 'MLP_keras':

            self.model = MultiLayerPerceptron_Keras(self.x_train, self.y_train,
                                              self.x_test, self.y_test,
                                              self.x_val, self.y_val)
            self.model.train_model()
            self.model.predict_test()
            self.accuracy_test, self.accuracy_val = self.model.get_accuracy()
            self.recall_val, self.recall_test = self.model.get_out_of_scope_recall()

        elif self.model_name == 'BERT':
            pass

        elif self.model_name == 'SVM':
            self.model = SupportVectorMachine(self.x_train, self.y_train,
                                              self.x_test, self.y_test,
                                              self.x_val, self.y_val)
            self.model.train_model()
            self.model.predict_test()
            self.accuracy_test, self.accuracy_val = self.model.get_accuracy()
            self.recall_val, self.recall_test = self.model.get_out_of_scope_recall()


        elif self.model_name == 'CNN':
            pass

        elif self.model_name == 'DialogFlow':
            pass

        return self.accuracy_val,self.accuracy_test, self.recall_val, self.recall_test
Example #2
0
    def gradient(self, x):
        return 1 / (1 + torch.exp(-x))


if __name__ == '__main__':
    data = load_digits()
    X = normalization(torch.tensor(data.data, dtype=torch.double))
    y = torch.tensor(data.target)

    # Convert the nominal y values to binary
    y = to_categorical(y)

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.4,
                                                        random_state=1)
    # MLP
    clf = MultiLayerPerceptron(n_hidden=16,
                               n_iterations=1000,
                               learning_rate=0.01,
                               activation_function_hidden_layer=Sigmoid(),
                               activation_function_output_layer=Softmax())

    clf.fit(X_train, y_train)
    y_pred = torch.argmax(clf.predict(X_test), dim=1)
    y_test = torch.argmax(y_test, dim=1)

    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)
Example #3
0
from MLP import MultiLayerPerceptron
from fileLoader import load_file
import numpy as np

df = load_file('mnist_data.npz')
X_train, y_train, X_test, y_test = df.get_data()

mlp = MultiLayerPerceptron(hidden_units=100,
                           L2=.01,
                           epochs=200,
                           lr=.0005,
                           shuffle=True,
                           mini_batch_size=100,
                           seed=1)

mlp.fit(X_train[:55000], y_train[:55000])
Example #4
0
axes[0].plot(x, y, '.')
axes[1].plot(*(sample_noise(100, dim)).T, '*')
plt.show()
"""

# hyperparameters
G_learning_rate = 0.1
D_learning_rate = 0.1
batch_size = 1000
input_size = dim
output_size = input_size

# build graph
Generator = MultiLayerPerceptron(batch_size,
                                 [input_size, 128, 128, output_size],
                                 [tf.tanh, tf.tanh, tf.tanh],
                                 G_learning_rate,
                                 scope_name="Generator")
G_output = Generator.get_output_layer_tensor()
# feed G_output as extra input tensor for Discriminator
Discriminator = MultiLayerPerceptron(batch_size, [input_size, 128, 128, 1],
                                     [tf.tanh, tf.tanh, tf.sigmoid],
                                     D_learning_rate,
                                     scope_name="Discriminator",
                                     extra_tensor_input=G_output)
Discriminator.creat_label_placeholder()

# G loss
ones_label = tf.ones((G_output.shape[0], 1))
# Generator's output label are all ones, in order to fake Discriminator
#G_loss = Discriminator.get_softmax_cross_entropy_loss(extra_data_label = ones_label)
Example #5
0
import MLP
from MLP import MultiLayerPerceptron
import tkinter as tk
import numpy as np

sdrnn = MultiLayerPerceptron(layers=[7, 7, 1])  # MLP
tepochs = 0


def update_a(event):
    r = int(slider_a.get() * (255 - offset)) + offset
    g = offset - int(slider_a.get() * (offset))
    b = offset - int(slider_a.get() * (offset))
    slider_a.config(background="#{0:02x}{1:02x}{2:02x}".format(r, g, b),
                    troughcolor="#{0:02x}{1:02x}{2:02x}".format(r, g, b))
    run_ann()


def update_b(event):
    r = int(slider_b.get() * (255 - offset)) + offset
    g = offset - int(slider_b.get() * (offset))
    b = offset - int(slider_b.get() * (offset))
    slider_b.config(background="#{0:02x}{1:02x}{2:02x}".format(r, g, b),
                    troughcolor="#{0:02x}{1:02x}{2:02x}".format(r, g, b))
    run_ann()


def update_c(event):
    r = int(slider_c.get() * (255 - offset)) + offset
    g = offset - int(slider_c.get() * (offset))
    b = offset - int(slider_c.get() * (offset))
Example #6
0
def train():
    train_dataset = AsciiSketchDataset("train")
    valid_dataset = AsciiSketchDataset("valid")
    train_data_loader = torch.utils.data.DataLoader(train_dataset,
                                                    128,
                                                    shuffle=True,
                                                    drop_last=False)
    valid_data_loader = torch.utils.data.DataLoader(valid_dataset,
                                                    128,
                                                    shuffle=True,
                                                    drop_last=False)

    model = MultiLayerPerceptron()
    optimizer = torch.optim.SGD(model.parameters(), lr=1)
    criterion = torch.nn.CrossEntropyLoss()
    lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                   step_size=10,
                                                   gamma=0.2)
    for epoch in range(100):
        for inputs, target in train_data_loader:
            optimizer.zero_grad()
            output = model(inputs)
            loss = criterion(output.double(), target)

            loss.backward()
            optimizer.step()

            _, predict = torch.max(output, 1)
            num_data = torch.numel(predict)

            # print("train>>> lr:", lr_scheduler.get_lr()[0], "; loss:", loss.item(), "; acc:", acc)

        model.eval()
        torch.save(
            {
                'model': model.state_dict(),
                'optimizer': optimizer.state_dict(),
                'lr_scheduler': lr_scheduler.state_dict()
            }, os.path.join("save", 'checkpoint-{}.bin'.format(epoch)))

        acc = 0
        for data in valid_data_loader:
            inputs, target = data
            output = model(inputs)
            _, predict = torch.max(output, 1)
            acc += (predict == target).sum().item()

        acc /= len(valid_dataset)

        if epoch % 10 == 0:
            perm = torch.randperm(valid_dataset.__len__())
            idx = perm[:9]
            inputs, targets = valid_dataset[idx]
            inputs = inputs.view(9, -1)

            outputs = model(inputs)
            _, predict = torch.max(outputs, 1)

            for i in range(9):
                plt.subplot(3, 3, i + 1)
                plt.imshow(inputs[i].reshape(16, 8), cmap=plt.cm.Greys)
                plt.title('target:{}. predict:{}.'.format(
                    label_to[targets[i].item()],
                    (label_to[predict[i].item()])))
                plt.xticks([])
                plt.yticks([])

            plt.show()

        # print("!!!!!!!!!!!!!!!!! VALID:", acc)

        model.train()

    idx = torch.tensor([i for i in range(26)])
    inputs, targets = valid_dataset[idx]
    inputs = inputs.view(26, -1)

    outputs = model(inputs)
    _, predict = torch.max(outputs, 1)

    for i in range(26):
        plt.imshow(inputs[i].reshape(16, 8), cmap=plt.cm.Greys)
        plt.title('target:{}. predict:{}.'.format(
            label_to[targets[i].item()], (label_to[predict[i].item()])))
        plt.xticks([])
        plt.yticks([])

        plt.savefig("model_cmp/{}.png".format(i))
        plt.close()