def main(): Xtrain, ytrain = utils.readTrainingDigitRecognizer( './data/digit-recognizer/train.csv', limit=1000) s = NeuralNetwork(M=7) # plot score with different learning rates l_cost_0, l_iterations_0, l_score_0 = s.fit(Xtrain, ytrain, epoch=1000, learning_rate=0.0001, L2_regulation=0) plt.plot(l_iterations_0, l_score_0, label='learning_rate=0.0001') l_cost_1, l_iterations_1, l_score_1 = s.fit(Xtrain, ytrain, epoch=1000, learning_rate=0.00001, L2_regulation=0) plt.plot(l_iterations_1, l_score_1, label='learning_rate=0.00001') l_cost_2, l_iterations_2, l_score_2 = s.fit(Xtrain, ytrain, epoch=1000, learning_rate=0.000001, L2_regulation=0) plt.plot(l_iterations_2, l_score_2, label='learning_rate=0.000001') plt.xlabel('Epoch') plt.ylabel('Score') plt.title('Learning rate comparison (data = digit-recognizer, size = ' + str(len(Xtrain)) + ")") plt.grid(True) plt.legend() plt.show()
def main(): X, y = utils.readTrainingDigitRecognizer( './data/digit-recognizer/train.csv', limit=2000) TRAIN = 1500 Xtrain = X[:TRAIN] ytrain = y[:TRAIN] Xval = X[TRAIN:] yval = y[TRAIN:] accuracies = dict() K = 10 # num of nearest neighbours for k in range(1, K): knn = KNN(k) knn.fit(Xtrain=Xtrain, ytrain=ytrain) yhat = knn.predict(Xtest=Xval) accuracy = knn.score(yhat=yhat, y=yval) print("k = " + str(k) + ": accuracy = " + str(accuracy)) accuracies[k] = accuracy # Plot lists = sorted( accuracies.items()) # sorted by key, return a list of tuples x, y = zip(*lists) # unpack a list of pairs into two tuples plt.plot(x, y, label='K-nearest neighbour') plt.xlabel('number of nearest points') plt.ylabel('accuracy') plt.show()
def main(): np.set_printoptions(threshold=sys.maxsize) X, Y = utils.readTrainingDigitRecognizer( './data/digit-recognizer/train.csv', limit=2000) pca = PCA(X) # do not apply PCA on labels pca.transform()
def main(): X, y = utils.readTrainingDigitRecognizer( '../data/digit-recognizer/train.csv', limit=2000) TRAIN = 1500 Xtrain = X[:TRAIN] ytrain = y[:TRAIN] Xval = X[TRAIN:] yval = y[TRAIN:] s = NeuralNetwork(hiddenLayersSize=[64, 32]) s.fit(Xtrain, ytrain, Xval, yval, epoch=30, learning_rate=0.01, batch_size=50)
def main(): X, y = utils.readTrainingDigitRecognizer( '../data/digit-recognizer/train.csv', limit=2000) TRAIN = 1500 Xtrain = X[:TRAIN] ytrain = y[:TRAIN] Xval = X[TRAIN:] yval = y[TRAIN:] s = NeuralNetwork(hiddenLayersSize=[64, 32], pkeep=[0.8, 0.5, 0.5]) #s = NeuralNetwork(hiddenLayersSize=[64, 32], pkeep=[1, 1, 1]) # no dropout s.fit(Xtrain, ytrain, Xval, yval, epoch=20, batch_size=30, learning_rate=0.001)
def main(): X, Y = utils.readTrainingDigitRecognizer( './data/digit-recognizer/train.csv', limit=2000) X, Y = filter(X, Y) TRAIN = int(len(X) / 2) Xtrain = X[:TRAIN] ytrain = Y[:TRAIN] Xtest = X[TRAIN:] ytest = Y[TRAIN:] p = Perceptron() p.fit(Xtrain, ytrain, epoch=5000) print("Training done.") yhat = p.predict(Xtest) print("Use test data. Prediction done!") score = p.score(yhat, ytest) print("Accuracy = " + str(score))
def main(): # build model Xtrain, ytrain = utils.readTrainingDigitRecognizer( '../data/digit-recognizer/train.csv') D = Xtrain[0].shape[0] ae = ANN(D=D, hiddenLayersSize=[64, 32, 16], K=10) ae.fit(Xtrain, ytrain) # Prediction Xtest = utils.readTestingDigitRecognizer( '../data/digit-recognizer/test.csv') yhat = ae.predict(Xtest) print('Prediction: ' + str(yhat)) # Export to csv import csv with open('../data/digit-recognizer/submission.csv', 'w') as csvFile: writer = csv.writer(csvFile) writer.writerow(['ImageId', 'Label']) for idx, row in enumerate(yhat): writer.writerow([idx + 1, row]) csvFile.close()
def main(): X_train, y_train = utils.readTrainingDigitRecognizer( '../data/digit-recognizer/train.csv', limit=1000) s = NeuralNetwork([10, 5]) s.fit(X_train, y_train)
def main(): ANALYZED_OBSERVATIONS = 1000 Xtrain, ytrain = utils.readTrainingDigitRecognizer( './data/digit-recognizer/train.csv', limit=ANALYZED_OBSERVATIONS) s = Neural_Network(M=7) # global configuration LEARNING_RATE = 0.01 EPOCH = 2 strategies = { 'strategy_0': { 'name': 'STEP_DECAY', 'factor': 2, 'step': 1000, 'learning_rate': LEARNING_RATE, 'epoch': EPOCH, 'L2_regulation': 0 }, 'strategy_1': { 'name': 'ADAGRAD', 'epsilon': 10e-8, 'learning_rate': LEARNING_RATE, 'epoch': EPOCH, 'L2_regulation': 0 }, 'strategy_2': { 'name': 'CONSTANT', 'learning_rate': LEARNING_RATE, 'epoch': EPOCH, 'L2_regulation': 0 }, 'strategy_3': { 'name': 'RMSPROP', 'epsilon': 10e-8, 'decay_rate': 0.99, 'learning_rate': LEARNING_RATE, 'epoch': EPOCH, 'L2_regulation': 0 }, 'strategy_4': { 'name': 'MOMENTUM', 'mu': 0.9, 'learning_rate': LEARNING_RATE, 'epoch': EPOCH, 'L2_regulation': 0 } } for _, strategy in strategies.items(): l_cost, l_iterations, l_score = s.fit(Xtrain, ytrain, strategy=strategy) plt.plot(l_iterations, l_score, label=str(strategy)) plt.xlabel('Iteration') plt.ylabel('Score') plt.title( 'Gradient descent variants comparison \n(data = digit-recognizer, ' + 'observations = ' + str(ANALYZED_OBSERVATIONS) + ' )') plt.grid(True) plt.legend() plt.show()
def main(): Xtrain, ytrain = utils.readTrainingDigitRecognizer( '../data/digit-recognizer/train.csv') Ytrain = utils.convert2indicator(ytrain) build_model(Xtrain, Ytrain)
def main(): X, y = utils.readTrainingDigitRecognizer( '../data/digit-recognizer/train.csv') ae = RBM(M=300) ae.fit(X)