def test_SVM_train(sample_train, sample_test): #this test is designed to verify that input shapes are correct Xtrain, ytrain = sample_train(count=40) Xtest, ytest = sample_test(count=10) with pytest.raises(ValueError): #Xtrain has the wrong shape (that is why there is a value error) lin_svm = LinearSVM() lin_svm.train(Xtrain, ytrain)
def test_SVM_train_reshape_input(sample_train, sample_test): Xtrain, ytrain = sample_train(count=40) Xtest, ytest = sample_test(count=10) Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], -1)) Xtest = np.reshape(Xtest, (Xtest.shape[0], -1)) lin_svm = LinearSVM() lin_svm.train(Xtrain, ytrain)
def test_SVM_train_1(sample_train, sample_test): #this test is designed to verify that input shapes are correct Xtrain, ytrain = sample_train(count=40) Xtest, ytest = sample_test(count=10) Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], -1)) with pytest.raises(ValueError): lin_svm = LinearSVM() lin_svm.train(Xtrain, Xtrain)
def test_SVM_train_2(sample_train, sample_test): #this test is designed to verify that input shapes are correct Xtrain, ytrain = sample_train(count=40) Xtest, ytest = sample_test(count=10) Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], -1)) with pytest.raises(ValueError): #this will catcha ValueError associated with bad unpacking of a tuple lin_svm = LinearSVM() lin_svm.train(ytrain, ytrain)
def test_LinearSVM_train(sample_train, num_iters=200, learning_rate=1e-7, regularization=5e3): Xtrain, ytrain = sample_train() Xtrain = np.reshape(Xtrain, (Xtrain.shape[0], -1)) Xtrain = np.hstack([Xtrain, np.ones((Xtrain.shape[0], 1))]) svm = LinearSVM() loss_hist = svm.train(Xtrain, ytrain, learning_rate=learning_rate, reg=regularization, num_iters=num_iters, verbose=False) assert loss_hist[-1] < loss_hist[0]
def test_LinearSVM_train_no_reshape(sample_train, num_iters=200, learning_rate=1e-7, regularization=5e3): Xtrain, ytrain = sample_train() svm = LinearSVM() #raises a value error because Xtrain is not of the correct input shape # (num_train, num_features) with pytest.raises(ValueError): loss_hist = svm.train(Xtrain, ytrain, learning_rate=learning_rate, reg=regularization, num_iters=num_iters, verbose=False)
X_train_feats = np.hstack([X_train_feats, np.ones((X_train_feats.shape[0], 1))]) X_val_feats = np.hstack([X_val_feats, np.ones((X_val_feats.shape[0], 1))]) X_test_feats = np.hstack([X_test_feats, np.ones((X_test_feats.shape[0], 1))]) from cs231n.classifiers.linear_classifier import LinearSVM learning_rates = [1e-8,3e-8,5e-8,7e-8,9e-8] regularization_strengths = [1e5,3e5,5e5,7e5,9e5] results = {} best_val = -1 best_svm = None for lr in learning_rates: for reg in regularization_strengths: svm = LinearSVM() svm.train(X_train_feats, y_train, learning_rate=lr, reg=reg, num_iters=6000) train_pred = svm.predict(X_train_feats) val_pred = svm.predict(X_val_feats) train_accuracy = np.mean(train_pred == y_train) val_accuracy = np.mean(val_pred == y_val) if val_accuracy > best_val: best_val = val_accuracy best_svm = svm results[(lr, reg)] = (train_accuracy, val_accuracy) for lr, reg in sorted(results): train_accuracy, val_accuracy = results[(lr, reg)]
def svm_sgd(X_train, y_train, X_val, y_val, X_test, y_test): #svm = LinearSVM() #tic = time.time() #loss_hist = svm.train(X_train, y_train, learning_rate=1e-7, reg=5e-4,num_iters=1500,verbose=True) #toc = time.time() #print 'That took %fs' % (toc-tic) #plt.plot(loss_hist) #plt.xlabel('Iteration number') #plt.ylabel('Loss value') #plt.savefig('figures/svm_loss_iteration.png') #y_train_pred = svm.predict(X_train) #print 'training accuracy: %f' %(np.mean(y_train == y_train_pred),) #y_val_pred = svm.predict(X_val) #print 'validation accuracy: %f' %(np.mean(y_val == y_val_pred),) learning_rates = [1e-7, 1e-6, 5e-5] regs = [5e-4, 5e-5, 1e-5] results = {} best_val = -1 bets_svm = None for l in learning_rates: for r in regs: svm = LinearSVM() loss_hist = svm.train(X_train, y_train, learning_rate=l, reg=r, num_iters=500, verbose=True) y_train_pred = svm.predict(X_train) train_acc = np.mean(y_train == y_train_pred) y_val_pred = svm.predict(X_val) val_acc = np.mean(y_val == y_val_pred) if best_val < val_acc: best_val = val_acc best_svm = svm results[(l, r)] = (train_acc, val_acc) for lr, reg in sorted(results): train_accuracy, val_accuracy = results[(lr, reg)] print 'lr %e reg %e train accuracy: %f val accuracy: %f' % ( lr, reg, train_accuracy, val_accuracy) return import math x_scatter = [math.log10(x[0]) for x in results] y_scatter = [math.log10(x[1]) for x in results] # plot training accuracy sz = [results[x][0] * 1500 for x in results] # default size of markers is 20 plt.subplot(1, 2, 1) plt.scatter(x_scatter, y_scatter, sz) plt.xlabel('log learning rate') plt.ylabel('log regularization strength') plt.title('CIFAR-10 training accuracy') # plot validation accuracy sz = [results[x][1] * 1500 for x in results] # default size of markers is 20 plt.subplot(1, 2, 2) plt.scatter(x_scatter, y_scatter, sz) plt.xlabel('log learning rate') plt.ylabel('log regularization strength') plt.title('CIFAR-10 validation accuracy') print 'best validation accuracy achieved during cross-validation: %f' % best_val plt.savefig('./figures/learn_rate_reg_SVM.png') plt.close() # Evaluate the best svm on test set y_test_pred = best_svm.predict(X_test) test_accuracy = np.mean(y_test == y_test_pred) print 'linear SVM on raw pixels final test set accuracy: %f' % test_accuracy w = best_svm.W[:, :-1] # strip out the bias w = w.reshape(10, 32, 32, 3) w_min, w_max = np.min(w), np.max(w) classes = [ 'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] for i in xrange(10): plt.subplot(2, 5, i + 1) # Rescale the weights to be between 0 and 255 wimg = 255.0 * (w[i].squeeze() - w_min) / (w_max - w_min) plt.imshow(wimg.astype('uint8')) plt.axis('off') plt.title(classes[i]) plt.savefig('./figures/visulize_Wg_SVM.png')
################################################################################### # Use the validation set to tune the learning rate and regularization strength from cs231n.classifiers.linear_classifier import LinearSVM learning_rates = [1e-7, 1.5e-7] regularization_strengths = [3e5, 5e5, 8e5] results = {} best_val = -1 best_svm = None for learning_rate in learning_rates: for reg in regularization_strengths: model = LinearSVM() losses = model.train(X_train_feats, y_train, learning_rate=learning_rate, reg=reg, verbose=False) train_result = (model.predict(X_train_feats)==y_train).mean() val_result = (model.predict(X_val_feats)==y_val).mean() results[(learning_rate, reg)] = [train_result, val_result] print('Learning rate: %f, Regularization: %f, Validation acc: %f' %(learning_rate, reg, val_result)) if val_result > best_val: best_val=val_result best_svm=model ################################################################################ # TODO: # # Use the validation set to set the learning rate and regularization strength. # # This should be identical to the validation that you did for the SVM; save # # the best trained classifer in best_svm. You might also want to play # # with different numbers of bins in the color histogram. If you are careful #
if (tune_SVM != 0): ################################################################################ # TODO: # # Use the validation set to set the learning rate and regularization strength. # # This should be identical to the validation that you did for the SVM; save # # the best trained classifer in best_svm. You might also want to play # # with different numbers of bins in the color histogram. If you are careful # # you should be able to get accuracy of near 0.44 on the validation set. # ################################################################################ print '-------------_*****--------------' print 'Tuning Parameters for SVM: ' for lr in np.arange(0.0001, 0.001, 0.0001): #(0.0000001, 0.000001, 0.0000001) for reg in np.arange(0.1, 1, 0.1): # (1e4, 1e5, 20000) print 'LR: %f and REG: %d' % (lr, reg) svm_iter = LinearSVM() svm_iter.train(X_train_feats, y_train, learning_rate=lr, reg=reg, num_iters=3000, verbose=True) y_val_pred_iter = svm_iter.predict(X_val_feats) y_train_pred_iter = svm_iter.predict(X_train_feats) val_acc = np.mean(y_val == y_val_pred_iter) train_acc = np.mean(y_train == y_train_pred_iter) results[(lr, reg)] = (train_acc, val_acc) # Turple Mapping print 'Validation accuracy: %f' % (val_acc) if (val_acc > best_val): best_val = val_acc best_svm = svm_iter