encoded_x = Onehot_encoder.fit_transform(all_data) x_train, x_test, y_train, y_test = train_test_split(encoded_x, y, test_size=0.15, random_state=21) 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))
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))
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)