예제 #1
0
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)]
    print('lr %e reg %e train accuracy: %f val accuracy: %f' % (lr, reg, train_accuracy, val_accuracy))

print('best validation accuracy achieved during cross-validation: %f' % best_val)
best_svm = None

svm=LinearSVM()
################################################################################
# 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.       #
################################################################################
for lr in learning_rates:
    for reg in regularization_strengths:
        loss_hist = svm.train(X_train_feats, y_train, learning_rate=lr, reg=reg,
                      num_iters=2000, verbose=True)
        y_val_pred = svm.predict(X_val_feats)
        y_train_pred = svm.predict(X_train_feats)
        tmp_train_accuracy=np.mean(y_train == y_train_pred)
        tmp_val_accuracy=np.mean(y_val == y_val_pred)
        results[(lr,reg)]=[tmp_train_accuracy,tmp_val_accuracy]
        if tmp_val_accuracy>best_val:
            best_val=tmp_val_accuracy
            best_svm=copy.deepcopy(svm)
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
for lr, reg in sorted(results):
    train_accuracy, val_accuracy = results[(lr, reg)]
    print 'lr %e reg %e train accuracy: %f val accuracy: %f' % (
예제 #3
0
# 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.       #
################################################################################
for rate in learning_rates:
    for strength in regularization_strengths:
        svm = LinearSVM()
        svm.train(X_train_feats,
                  y_train,
                  learning_rate=rate,
                  reg=strength,
                  num_iters=1500,
                  verbose=False)
        learning_accuracy = np.mean(svm.predict(X_train_feats) == y_train)
        validation_accuracy = np.mean(svm.predict(X_val_feats) == y_val)
        if validation_accuracy > best_val:
            best_val = validation_accuracy
            best_svm = svm
        results[(rate, strength)] = (learning_accuracy, validation_accuracy)
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
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)
예제 #4
0
파일: ex_svm.py 프로젝트: icodingc/CS231n
svm = LinearSVM()
tic = time.time()
loss_hist = svm.train(X_train, y_train, learning_rate=1e-7, reg=5e4,
                      num_iters=1500, verbose=True)
toc = time.time()
print 'That took %fs' % (toc - tic)

# A useful debugging strategy is to plot the loss as a function of
# iteration number:
plt.plot(loss_hist)
plt.xlabel('Iteration number')
plt.ylabel('Loss value')
# plt.show()
# Write the LinearSVM.predict function and evaluate the performance on both the
# training and validation set
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), )

# Use the validation set to tune hyperparameters (regularization strength and
# learning rate). You should experiment with different ranges for the learning
# rates and regularization strengths; if you are careful you should be able to
# get a classification accuracy of about 0.4 on the validation set.
learning_rates = [1e-7, 3e-7, 5e-7]
regularization_strengths = [1e4, 3e4, 5e4]

# results is dictionary mapping tuples of the form
# (learning_rate, regularization_strength) to tuples of the form
# (training_accuracy, validation_accuracy). The accuracy is simply the fraction
# of data points that are correctly classified.
예제 #5
0
                          verbose=True)
    toc = time.time()

    print('That took %fs' % (toc - tic))

    # A useful debugging strategy is to plot the loss as a function of
    # iteration number:

    plt.plot(loss_hist)
    plt.xlabel('Iteration number')
    plt.ylabel('Loss value')
    plt.show()

    # Write the LinearSVM.predict function and evaluate the performance on both
    # training and validation set
    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)))

    # use the validation set to tune hyperparameter (regularization strength and
    # learning rate). You should experiment with different ranges for the learning
    # rates and regularization strengths

    learning_rates = [1e-7, 2e-7, 5e-7, 1e-6]
    regularization_strengths = [1e4, 2e4, 5e4, 1e5, 5e5, 1e6]

    # results are dictionary mapping tuples of the form
    # (learning_rate, regularization_strength) to tuples of the form
    # (training_accuracy, validation_accuracy). The accuracy is simply the fration
    # of data points that are correctly classified.
예제 #6
0
파일: myFeatures.py 프로젝트: wycjl/CS231n
# 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.       #
################################################################################
for li in learning_rates:
    for regi in regularization_strengths:
        svm = LinearSVM()
        loss_hist = svm.train(X_train_feats,
                              y_train,
                              learning_rate=li,
                              reg=regi,
                              num_iters=1500,
                              verbose=True)
        y_train_predict = svm.predict(X_train_feats)
        y_train_acc = np.mean(y_train_predict == y_train)
        y_val_predict = svm.predict(X_val_feats)
        y_val_acc = np.mean(y_val_predict == y_val)
        results[(li, regi)] = (y_train_acc, y_val_acc)
        if y_val_acc > best_val:
            best_val = y_val_acc
            best_svm = svm
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
for lr, reg in sorted(results):
    train_accuracy, val_accuracy = results[(lr, reg)]
    print('lr %e reg %e train accuracy: %f val accuracy: %f' %
# 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.       #
################################################################################
for l in learning_rates:
    for r in regularization_strengths:
        svm = LinearSVM()
        svm.train(X_train_f,
                  y_train,
                  learning_rate=l,
                  reg=r,
                  num_iters=1500,
                  batch_size=200)
        y_train_pred = svm.predict(X_train_f)
        y_val_pred = svm.predict(X_val_f)
        training_accuracy = np.mean(y_train == y_train_pred)
        validation_accuracy = np.mean(y_val == y_val_pred)
        results[(l, r)] = (training_accuracy, validation_accuracy)
        if validation_accuracy > best_val:
            best_val = validation_accuracy
            best_svm = svm
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
for lr, reg in sorted(results):
    train_accuracy, val_accuracy = results[(lr, reg)]
    print 'lr %e reg %e train accuracy: %f val accuracy: %f' % (
예제 #8
0
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')
예제 #9
0
 # training set, compute its accuracy on the training and validation sets, and  #
 # store these numbers in the results dictionary. In addition, store the best   #
 # validation accuracy in best_val and the LinearSVM object that achieves this  #
 # accuracy in best_svm.                                                        #
 #                                                                              #
 # Hint: You should use a small value for num_iters as you develop your         #
 # validation code so that the SVMs don't take much time to train; once you are #
 # confident that your validation code works, you should rerun the validation   #
 # code with a larger value for num_iters.                                      #
 ################################################################################
 for rate in learning_rates:
     for regularization in regularization_strengths:
         svm = LinearSVM()
         lost_history = svm.train(x_train, y_train, rate, regularization,
                                  2000)
         y_train_pred = svm.predict(x_train)
         accuracy_train = np.mean(y_train == y_train_pred)
         y_val_pred = svm.predict(x_val)
         accuracy_val = np.mean(y_val == y_val_pred)
         results[(rate, regularization)] = (accuracy_train, accuracy_val)
         if best_val < accuracy_val:
             best_val = accuracy_val
             best_svm = svm
 ################################################################################
 #                              END OF YOUR CODE                                #
 ################################################################################
 # 打印结果
 for lr, reg in sorted(results):
     training_accuracy, validation_accuracy = results[(lr, reg)]
     print('lr %e reg %e training_accuracy: %f validation_accuracy %f ' %
           (lr, reg, training_accuracy, validation_accuracy))
예제 #10
0
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    #
# you should be able to get accuracy of near 0.44 on the validation set.       #
################################################################################
pass
예제 #11
0
    # 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
                print 'Best so far: %f' % (val_acc)
    ################################################################################
    #                              END OF YOUR CODE                                #
    ################################################################################

    # Print out results.
    for lr, reg in sorted(results):
예제 #12
0
pass
################################################################################
# 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.       #
################################################################################
pass
import itertools
for lr, reg in itertools.product(learning_rates, regularization_strengths):
    svm = LinearSVM()
    svm.train(X_train_feats, y_train, lr, reg, num_iters=1000, verbose=True)

    y_pred_train = svm.predict(X_train_feats)
    acc_train = np.mean(y_pred_train == y_train)
    y_pred_val = svm.predict(X_val_feats)
    acc_val = np.mean(y_pred_val == y_val)
    results[(lr, reg)] = (acc_train, acc_val)
    if acc_val > best_val:
        best_val = acc_val
        best_svm = svm
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
for lr, reg in sorted(results):
    train_accuracy, val_accuracy = results[(lr, reg)]
    print 'lr %e reg %e train accuracy: %f val accuracy: %f' % (