コード例 #1
0
ファイル: main.py プロジェクト: proman24/neural-networks
def main():
    args = parse_args()

    # dataset
    X_train, y_train, X_val, y_val, X_test, y_test = load_dataset(flatten=True)

    # model layer dimensions
    input_dim = X_train.shape[1]
    num_classes = 10

    # create model
    model = Model()
    model.add(Dense(input_dim, 100), activation='relu')
    model.add(Dense(100, 200), activation='relu')
    model.add(Dense(200, 200), activation='relu')
    model.add(Dense(200, num_classes))

    # train model
    model.fit(X_train,
              y_train,
              val_data=(X_val, y_val),
              verbose=True,
              epochs=args.epochs,
              batch_size=args.batch_size,
              lr=args.lr)

    # evaluate model
    model.eval(X_test, y_test, verbose=True)
コード例 #2
0
ファイル: test_nn.py プロジェクト: CastaChick/Camael
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)))
コード例 #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))
コード例 #4
0

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()
end2 = timer()
elapsed_time = time.process_time() - t

print(end - start, "    ", end1 - start1, "      ", end2 - start2, "     ",
コード例 #5
0
np.random.shuffle(all_data)

split = int(0.8 * all_data.shape[0])
x_train = all_data[:split, :2]
x_test = all_data[split:, :2]
y_train = all_data[:split, -1]
y_test = all_data[split:, -1]


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(2, 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=30,
              n_workers=10)
model.fit(x_train, y_train, 50)
y_hat = model.predict(x_test)

print('Accuracy on test:', accuracy(y_test, y_hat))
コード例 #6
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)