def main(): train_loader = Data(train_path, shuffle=True, split=0.2) pca = train_loader.dataloading(batch_size=batch_size, pca=PCA(n_components=pca_components)) test_loader = Data(test_path, shuffle=False, test=True) test_loader.dataloading(pca=pca) model = NN(in_dim=pca_components + 1, n_cls=3, neurons=[256], lr=lr, hidden_activation='sigmoid', load_weight=False, save_weight=False) info = { 'train_loss': [], 'train_acc': [], 'val_loss': [], 'val_acc': [], 'EPOCHS': EPOCHS } for epoch in range(EPOCHS): cache = np.zeros(4) itr = 0 for train_data in train_loader: x_train, y_train, x_val, y_val = train_data y_train = to_onehot(y_train, 3) y_val = to_onehot(y_val, 3) train_loss, train_acc = model.train(x_train, y_train) val_loss, val_acc, pred = model.predict(x_val, y_val) cache[0] += train_loss cache[1] += train_acc cache[2] += val_loss cache[3] += val_acc itr += 1 cache /= itr info['train_loss'].append(cache[0]) info['train_acc'].append(cache[1]) info['val_loss'].append(cache[2]) info['val_acc'].append(cache[3]) print( 'EPOCH:{:05d}/{:05d} train_loss: {:.5f} train_acc: {:.4f} val_loss: {:.5f} val_acc: {:.4f}' .format(epoch + 1, EPOCHS, *cache.tolist())) plot_info(**info) train_loader.set_batch_size(2000) for train_data in train_loader: x_train, y_train, x_val, y_val = train_data y_train = to_onehot(y_train, 3) plot_decision_region(x_train, y_train, model, train_loader.CLASSES, title='training') for test_data in test_loader: x_test, y_test = test_data y_test = to_onehot(y_test, 3) plot_decision_region(x_test, y_test, model, train_loader.CLASSES, title='testing') test_loss, test_acc, pred = model.predict(x_test, y_test) print('test_loss: {:.5f} test_acc: {:.4f}'.format(test_loss, test_acc))
# Plot the training points plt.scatter(X[:, 0], X[:, 1], c=t.flatten()) plt.xlabel('Petal length') plt.ylabel('Petal width') plt.title('Real Data', fontsize=16) plt.show() # Build neural network model = NN() model.add_layer(units=10, activation=np.tanh, initialization=np.random.normal) model.add_layer(units=5, activation=np.tanh, initialization=np.random.normal) model.add_layer(units=2, activation=np.tanh, initialization=np.random.normal) model.add_layer(1, activation=model.sigmoid) model.train(X, t, iterations=5000, learn_rate=0.0001) # 5000 prediction = model.predict(X) # Plot error plt.plot(model.history['epoch'], model.history['error_train'], label='Training error') plt.xlabel('Epoch') plt.ylabel('Error') plt.legend() plt.show() # Plot prediction plt.scatter(X[:, 0], X[:, 1], c=np.round(prediction, 0).flatten()) plt.xlabel('Petal length') plt.ylabel('Petal width')