Esempio n. 1
0
def load_model(config, n_classes=2):
    model = Model()
    X = model.add(input([config.SEQUENCE_LEN], dtype='int32', name="input"))

    if config.is_use_embedding():
        embedding = model.add(
            embeddings(X,
                       config.WORD_COUNT,
                       config.EMBEDDING_DIM,
                       weights=config.EMBEDDING_MATRIX,
                       input_length=config.SEQUENCE_LEN,
                       frozen=config.is_embedding_trainable()))
    else:
        embedding = model.add(
            embeddings(X,
                       config.WORD_COUNT,
                       config.EMBEDDING_DIM,
                       input_length=config.SEQUENCE_LEN,
                       frozen=config.is_embedding_trainable()))

    dropout_1 = model.add(dropout(embedding, config.DROPOUT_LIST[0]))

    conv_list = []
    for k_size, n_C, k_pool in zip(config.FILTER_SIZE_LIST,
                                   config.FILTERS_PER_LAYER,
                                   config.POOL_SIZE_LIST):
        c = conv1d(dropout_1, k_size, n_C, nonlin='relu')
        p = maxpool(c, k_pool)
        conv_list.append(flatten(p))

    if len(conv_list) > 1:
        conv_out = model.add(concat(conv_list))
    else:
        conv_out = model.add(conv_list[0])

    dense_1 = model.add(dense(conv_out, 150, nonlin='relu'))
    dropout_2 = model.add(dropout(dense_1, config.DROPOUT_LIST[1]))
    out = model.add(dense(dropout_2, n_classes, nonlin='softmax'))
    model.compile(optimizer='rmsprop',
                  loss='softmax_entropy',
                  learning_rate=config.LEARNING_RATE,
                  ckpt_file=config.CKPT_PATH,
                  device=config.DEVICE)

    return model
Esempio n. 2
0
from nn.metrix import accuracy


(X_train, y_train), (X_test, y_test) = load_mnist()
X_train = X_train.reshape((X_train.shape[0], -1)) / 255
X_test = X_test.reshape((X_test.shape[0], -1)) / 255

transformer = MakeOneHot()
y_train = transformer.fit_transform(y_train)
y_test = transformer.transform(y_test)

model = Model()
model.add(FC(500, input_shape=784))
model.add(ReLU())
model.add(Dropout(0.5))
model.add(FC(150))
model.add(ReLU())
model.add(Dropout(0.5))
model.add(FC(50))
model.add(ReLU())
model.add(Dropout(0.5))
model.add(FC(10))
model.add(Softmax())

model.compile(Adam(eta=0.01), cross_entropy, accuracy)

model.fit(X_train, y_train, max_iter=10, batch_size=2000)

print("train acc: {:.2f}%".format(model.score(X_train, y_train)))
print("test acc: {:.2f}%".format(model.score(X_test, y_test)))
Esempio n. 3
0
split = int(0.8 * all_data.shape[0])
x_train = all_data[:split, 1:]
x_test = all_data[split:, 1:]
y_train = all_data[:split, 0]
y_test = all_data[split:, 0]

y_train = one_hot(y_train.astype('int'))
y_test = one_hot(y_test.astype('int'))

def accuracy(y, y_hat):
    y = np.argmax(y, axis=1)
    y_hat = np.argmax(y_hat, axis=1)

    return np.mean(y==y_hat)

def relu(x):
    return np.maximum(x, 0)

model = Model()
model.add_layer(Layer(784, 10, softmax))
#model.add_layer(Layer(64, 64, relu))
#model.add_layer(Layer(64, 10, softmax))

model.compile(CrossEntropyLoss, DataLoader, accuracy,
              batches_per_epoch=x_train.shape[0] // 32 + 1,
              n_workers=50, c1=1., c2=2.)
model.fit(x_train, y_train, 100)
y_hat = model.predict(x_test)

print('Accuracy on test:', accuracy(y_test, y_hat))
def accuracy(y, y_hat):
    y_hat = (y_hat >= 0.5).astype('int')
    y = y.astype('int')
    return np.mean(y_hat[:, 0] == y)


model = Model()
model.add_layer(Layer(965, 10, tanh))
model.add_layer(Layer(10, 10, tanh))
model.add_layer(Layer(10, 10, tanh))
model.add_layer(Layer(10, 10, tanh))
model.add_layer(Layer(10, 1, sigmoid))

model.compile(BinaryCrossEntropyLoss,
              DataLoader,
              accuracy,
              batches_per_epoch=20,
              n_workers=10)
print(x_train.shape, y_train.shape, y_train.shape, y_test.shape)
index_list, cost_list = model.fit(x_train, y_train, 500)
y_hat = model.predict(x_test)
#print(confusion_matrix(y_test, y_hat))

plt.plot(index_list, cost_list)
plt.xticks(index_list, rotation='vertical')
plt.xlabel("Number of Iterarion")
plt.ylabel("Cost")
plt.show()

end = timeit.timeit()
end1 = time.time()
Esempio n. 5
0
x_test = all_data[split:, 0:-1]
y_train = all_data[:split, -1]
y_test = all_data[split:, -1]

def one_hot(y, depth=10):
    y_1hot = np.zeros((y.shape[0], depth))
    y_1hot[np.arange(y.shape[0]), y] = 1
    return y_1hot

y_train = one_hot(y_train.astype('int'), depth=2)
y_test = one_hot(y_test.astype('int'), depth=2)

def accuracy(y, y_hat):
    y = np.argmax(y, axis=1)
    y_hat = np.argmax(y_hat, axis=1)

    return np.mean(y==y_hat)

model = Model()
model.add_layer(Layer(30, 7, relu)))
model.add_layer(Layer(7, 2, softmax))

model.compile(CrossEntropyLoss, DataLoader, accuracy, batches_per_epoch=(x_train.shape[0]//16)+1, n_workers=12)
model.fit(X=x_train, y=y_train, epochs=100)
y_hat = model.predict(x_test)

print('Accuracy on test:', accuracy(y_test, y_hat))

elaped_time = time.process_time() - t 
print("Elapsed Time:", elaped_time)