def problem_1b(n=500): # change n for two size of datasets algorithmList = [ 'Perceptron', 'Perceptron with margin', 'Winnow', 'Winnow with margin', 'AdaGrad' ] t = Trainer() d = t.data_generator(l=10, m=100, n=n, number_of_instances=50000, noise=False) x, y = d['x'], d['y'] D1_x, D1_y, D2_x, D2_y = d['D1_x'], d['D1_y'], d['D2_x'], d['D2_y'] initDict = initDictGenerator(n=t.n) for algorithm in algorithmList: algorithmInit = initDict[algorithm] learningRateList = algorithmInit['learning rate'] marginList = algorithmInit['margin'] t.learning(algorithm, D1_x, D1_y, initDict=initDict, times=20) for lr in learningRateList: for mg in marginList: err_rate = t.error_estimate(t.D2_x, t.D2_y, lr, mg) mistake = t.mistakeCount(lr, mg) print( 'LR: {0: >6s}, MG: {1: >6s}, ER: {2: >6s}, Mis: {3: >6s}'. format(str(lr), str(mg), str(err_rate), str(mistake)))
def problem_3_tuning(): algorithmList = [ 'Perceptron', 'Perceptron with margin', 'Winnow', 'Winnow with margin', 'AdaGrad' ] for m in [100, 500, 1000]: print() d = problem_3_dataLoader(m, 'train') x, y = d['x'], d['y'] D1_x, D1_y, D2_x, D2_y = d['D1_x'], d['D1_y'], d['D2_x'], d['D2_y'] t = Trainer() t.set_param(l=10, m=m, n=x.shape[1], number_of_instances=x.shape[0]) initDict = initDictGenerator(n=t.n) for algorithm in algorithmList: algorithmInit = initDict[algorithm] learningRateList = algorithmInit['learning rate'] marginList = algorithmInit['margin'] t.learning(algorithm, D1_x, D1_y, initDict=initDict, times=20) for lr in learningRateList: for mg in marginList: err_rate = t.error_estimate(D2_x, D2_y, lr, mg) mistake = t.mistakeCount(lr, mg) print( 'LR: {0: >6s}, MG: {1: >6s}, ER: {2: >6s}, Mis: {3: >6s}' .format(str(lr), str(mg), str(err_rate), str(mistake)))
def problem_3_train_and_evaluate(): algorithmList = [ 'Perceptron', 'Perceptron with margin', 'Winnow', 'Winnow with margin', 'AdaGrad' ] bestParaList = { 100: [(1, 0), (0.005, 1), (1.1, 0), (1.1, 0.3), (1.5, 1)], 500: [(1, 0), (1.5, 1), (1.1, 0), (1.1, 0.3), (1.5, 1)], 1000: [(1, 0), (0.25, 1), (1.1, 0), (1.1, 0.3), (0.25, 1)] } for m in [100, 500, 1000]: print() #plt.figure() d = problem_3_dataLoader(m) x, y = d['x'], d['y'] d = problem_3_dataLoader(m, 'test') xtest, ytest = d['x'], d['y'] t = Trainer() t.set_param(l=10, m=m, n=x.shape[1], number_of_instances=x.shape[0]) initDict = initDictGenerator(n=t.n) #color = 'rgb' for idx in range(len(algorithmList)): algorithm = algorithmList[idx] algorithmInit = initDict[algorithm] if m in bestParaList: lr, mg = bestParaList[m][idx] else: lr, mg = bestParaList[500][idx] algorithmInit['learning rate'] = [lr] algorithmInit['margin'] = [mg] t.learning(algorithm, x, y, initDict={algorithm: algorithmInit}, times=20) err_rate = t.error_estimate(xtest, ytest, lr, mg) print('LR: {0: >6s}, MG: {1: >6s}, ER: {2: >6s}'.format( str(lr), str(mg), str(err_rate)))