Beispiel #1
0
# Numerically compute the gradient along several randomly chosen dimensions, and
# compare them with your analytically computed gradient. The numbers should match
# almost exactly along all dimensions.
f = lambda w: svm_loss_vectorized(w, X_dev, y_dev, 0.0)[0]
grad_numerical = grad_check_sparse(f, W, grad)

# do the gradient check once again with regularization turned on
# you didn't forget the regularization gradient did you?
loss, grad = svm_loss_vectorized(W, X_dev, y_dev, 5e1)
f = lambda w: svm_loss_vectorized(w, X_dev, y_dev, 5e1)[0]
grad_numerical = grad_check_sparse(f, W, grad)


# In the file linear_classifier.py, implement SGD in the function
# LinearClassifier.train() and then run it with the code below.
svm=LinearSVM()
tic=time.time()
loss_hist = svm.train(X_train, y_train, learning_rate=1e-7, reg=2.5e4,
                      num_iters=1500, verbose=True)
toc=time.time()
print('That tooks %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.savefig('/home/hongyin/file/cs231n-assignment1/picFaster.jpg')
"""
Beispiel #2
0
#_, grad_vectorized = svm_loss_vectorized(W, X_dev, y_dev, 0.000005) 
toc = time.time() 
print('Vectorized loss and gradient: computed in %fs' % (toc - tic)) 

# The loss is a single number, so it is easy to compare the values computed 
# by the two implementations. The gradient on the other hand is a matrix, so 
# we use the Frobenius norm(对应元素的平方和再开方) to compare them. 
#difference = np.linalg.norm(grad_naive - grad_vectorized, ord='fro') 
#print('difference: %f' % difference) 
'''

# In the file linear_classifier.py, implement SGD in the function
# LinearClassifier.train() and then run it with the code below.
from linear_classifier import LinearSVM

svm = LinearSVM()
tic = time.time()
loss_hist = svm.train(X_train,
                      y_train,
                      learning_rate=1e-7,
                      reg=2.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') 
Beispiel #3
0
def cross_validation(X_train, y_train, X_val, y_val):
    #############################################################################################
    # 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, 5e-5]
    regularization_strengths = [5e4, 1e5]

    # 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.
    results = {}
    best_val = -1  # The highest validation accuracy that we have seen so far.
    best_svm = None  # The LinearSVM object that achieved the highest validation rate.

    ################################################################################
    # TODO:                                                                        #
    # Write code that chooses the best hyperparameters by tuning on the validation #
    # set. For each combination of hyperparameters, train a linear SVM on the      #
    # 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.                                      #
    ################################################################################
    iters = 2000  #100
    for lr in learning_rates:
        for rs in regularization_strengths:
            svm = LinearSVM()
            svm.train(X_train,
                      y_train,
                      learning_rate=lr,
                      reg=rs,
                      num_iters=iters)

            y_train_pred = svm.predict(X_train)
            acc_train = np.mean(y_train == y_train_pred)
            y_val_pred = svm.predict(X_val)
            acc_val = np.mean(y_val == y_val_pred)

            results[(lr, rs)] = (acc_train, acc_val)

            if best_val < acc_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' %
              (lr, reg, train_accuracy, val_accuracy))

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

    return results, best_svm
Beispiel #4
0
    loss, grad = svmDevObj1.svm_loss_naive()

    f = lambda w: svmDevObj1.svm_loss_naive()[0]
    grad_numerical = grad_check_sparse(f, W, grad)
    print('grad_numerical: ', grad_numerical)

    # do the gradient check once again with regularization turned on
    # you didn't forget the regularization gradient did you?
    svmDevObj2 = SVM(W, X_dev, y_dev, 5e1)
    loss, grad = svmDevObj2.svm_loss_naive()
    f = lambda w: svmDevObj2.svm_loss_naive()[0]
    grad_numerical = grad_check_sparse(f, W, grad)
    print('grad_numerical: ', grad_numerical)

    #####################
    linearSVM = LinearSVM()
    tic = time.time()
    loss_hist = linearSVM.train(X_train,
                                y_train,
                                learning_rate=1e-7,
                                reg=2.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')
Beispiel #5
0
b1 = np.zeros((train_num, 256))
for i in range(train_num):
    a = np.bincount(train_images[i].astype(int), minlength=256).reshape(1, -1)
    b1[i] = a
b2 = np.zeros((test_num, 256))
for i in range(test_num):
    a = np.bincount(test_images[i].astype(int), minlength=256).reshape(1, -1)
    b2[i] = a
b3 = np.zeros((val_num, 256))
for i in range(val_num):
    a = np.bincount(test_images[i].astype(int), minlength=256).reshape(1, -1)
    b3[i] = a
#=================================================================================
# loss,grad=svm.svm_loss_naive(w,train_images,train_labels,reg)
# print(loss, grad)
svm = LinearSVM()  #创建分类器对象,此时W为空
loss_hist = svm.train(train_images,
                      train_labels,
                      learning_rate=1e-7,
                      reg=2.5e4,
                      num_iters=1500,
                      verbose=True)  #此时svm对象中有W

y_train_pred = svm.predict(train_images)
print('training accuracy: %f' % (np.mean(train_labels == y_train_pred)))
# y_val_pred = svm.predict(val_images)
# print('validation accuracy: %f'%(np.mean(val_labels==y_val_pred)))

# 超参数调优(交叉验证)
learning_rates = [1.4e-7, 1.5e-7, 1.6e-7]
# for循环的简化写法12个
Beispiel #6
0
        x[ix] = oldval + h
        fxph = f(x)
        x[ix] = oldval - h
        fxmh = f(x)
        x[ix] = oldval
        grad_numerical = (fxph - fxmh) / (2 * h)
        grad_analytic = analytic_grad[ix]
        rel_error = abs(grad_numerical - grad_analytic) / (abs(grad_numerical) + abs(grad_analytic))
        print('numerical: %f analytic: %f, relative error: %e' % (grad_numerical, grad_analytic, rel_error))
#现在我们对加入了正则项的梯度进行检验
loss, grad = svm.svm_loss_naive(w,x_dev,y_dev,0.0)
f = lambda w:svm.svm_loss_naive(w,x_dev,y_dev,0.0)[0]
grad_numerical = grad_check_sparse(f,w,grad)

# 模型进行测试
svm = LinearSVM()    #创建对象,此时W为空
tic = time.time()
loss_hist = svm.train(x_train,y_train,learning_rate = 1e-7,reg = 2.5e4,num_iters = 1500,verbose = True)    #此时svm对象中有W
toc = time.time()
print('that took %fs' % (toc -tic))

plt.plot(loss_hist)
plt.xlabel('iteration number')
plt.ylabel('loss value')
plt.show()
#训练完成之后,将参数保存,使用参数进行预测,计算准确率
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)))
'''
 def __init__(self):
     self.svm = LinearSVM()
     self.fit()