Example #1
0
def softmax_sgd(X_train, y_train, X_val, y_val, X_test, y_test):
    #W = np.random.randn(10,3073)*0.0001
    #loss, grad = softmax_loss_vectorized(W, X_train,y_train,0.0)
    #print 'loss: %f' % loss
    #print 'sanity check %f' % (-np.log(0.1))

    #f = lambda w: softmax_loss_vectorized(w, X_train,y_train,0.0)[0]
    #grad_check_sparse(f,W,grad,10)

    learning_rates = [1e-7, 1e-6, 5e-5]
    regs = [5e-4, 5e-5, 1e-5]
    results = {}
    best_val = -1
    bets_soft = None

    for l in learning_rates:
        for r in regs:
            softmax = Softmax()
            loss_hist = softmax.train(X_train,
                                      y_train,
                                      learning_rate=l,
                                      reg=r,
                                      num_iters=500,
                                      verbose=True)
            y_train_pred = softmax.predict(X_train)
            train_acc = np.mean(y_train == y_train_pred)
            y_val_pred = softmax.predict(X_val)
            val_acc = np.mean(y_val == y_val_pred)
            if best_val < val_acc:
                best_val = val_acc
                best_soft = softmax
            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)
Example #2
0
results = {}
best_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-7]
regularization_strengths = [5e4, 1e8]

################################################################################
# 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 softmax classifer in best_softmax.                          #
################################################################################
for lr in learning_rates:
    for rs in regularization_strengths:
        sm = Softmax()
        loss_hist = sm.train(X_train,
                             y_train,
                             learning_rate=lr,
                             reg=rs,
                             num_iters=1500,
                             verbose=True)
        y_train_pred = sm.predict(X_train)
        acc_tr = np.mean(y_train == y_train_pred)
        y_val_pred = sm.predict(X_val)
        acc_val = np.mean(y_val == y_val_pred)

        results[(lr, rs)] = (acc_tr, acc_val)
        if acc_val > best_val:
            best_val = acc_val
            best_softmax = sm
Example #3
0
from cs231n.classifiers.linear_classifier import Softmax
results = {}
best_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-7, 2e-6]
regularization_strengths = [1e4, 3e4, 5e4]

################################################################################
# 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 softmax classifer in best_softmax.                          #
################################################################################
for l_r_cv in learning_rates:
    for r_s_cv in regularization_strengths:
        softm = Softmax()
        softm.train(X_train,
                    y_train,
                    learning_rate=l_r_cv,
                    reg=r_s_cv,
                    num_iters=1500,
                    verbose=True)
        y_train_pred = softm.predict(X_train)
        t_ac = np.mean(y_train == y_train_pred)
        y_val_pred = softm.predict(X_val)
        v_ac = np.mean(y_val == y_val_pred)
        if v_ac > best_val:
            best_val = v_ac
            best_softmax = softm
        results[(l_r_cv, r_s_cv)] = (t_ac, v_ac)
################################################################################
Example #4
0
results = {}
best_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-5]
regularization_strengths = [5e4, 1e5]

################################################################################
# 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 softmax classifer in best_softmax.                          #
################################################################################
iters = 1500
for lr in learning_rates:
    for rs in regularization_strengths:
        softmax = Softmax()
        softmax.train(X_train,
                      y_train,
                      learning_rate=lr,
                      reg=rs,
                      num_iters=iters)
        y_train_pred = softmax.predict(X_train.T)
        accu_train = np.mean(y_train == y_train_pred)
        y_val_pred = softmax.predict(X_val.T)
        accu_val = np.mean(y_val == y_val_pred)

        results[(lr, rs)] = (accu_train, accu_val)

        if best_val < accu_val:
            best_val = accu_val
            best_softmax = softmax
Example #5
0
# Use the Frobenius norm to compare the two versions of the gradient.
grad_difference = np.linalg.norm(grad_naive - grad_vectorized, ord='fro')
print('Loss difference: ', np.abs(loss_naive - loss_vectorized))
print('Gradient difference: ', grad_difference)
''' Use the validation set to tune hyperparams -
        regularization strength and learning rate '''
results = {}  # {(lr,reg) : (acc_tr, acc_val)}
best_acc_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-7]
regularization_strengths = [2.5e4, 5e4]

for i in range(len(learning_rates)):
    for j in range(len(regularization_strengths)):
        sm = Softmax()
        sm.train(X_val,
                 y_val,
                 learning_rate=learning_rates[i],
                 reg=regularization_strengths[j],
                 num_iters=1000,
                 verbose=True)
        y_v_pred = sm.predict(X_val)
        y_tr_pred = sm.predict(X_train)
        acc_val = np.mean(y_val == y_v_pred)
        acc_tr = np.mean(y_train == y_tr_pred)
        results[(learning_rates[i], regularization_strengths[j])] = (acc_tr,
                                                                     acc_val)
        if acc_val > best_acc_val:
            best_acc_val = acc_val
            best_softmax = sm
Example #6
0
 ################################################################################
 # Write code that chooses the best hyper-parameters by tuning on the validation#
 # set. For each combination of hyper-parameters, train a linear Softmax 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 Softmax object that achieves this    #
 # accuracy in best_Softmax.                                                    #
 #                                                                              #
 # Hint: You should use a small value for num_iters as you develop your         #
 # validation code so that the Softmaxs 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:
         softmax = Softmax()
         lost_history = softmax.train(x_train, y_train, rate,
                                      regularization, 2000)
         y_train_pred = softmax.predict(x_train)
         accuracy_train = np.mean(y_train == y_train_pred)
         y_val_pred = softmax.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_softmax = softmax
 ################################################################################
 #                              END OF YOUR CODE                                #
 ################################################################################
 # 打印结果
 for lr, reg in sorted(results):
Example #7
0
    loss_vectorized, grad_vectorized = softmax_loss_vectorized(
        W, X_train, y_train, 0.00001)
    toc = time.time()
    print('vectorized loss: %e computed in %fs' % (loss_vectorized, toc - tic))

    # Use the Frobenius norm to compare the two versions of the gradient.
    grad_difference = np.linalg.norm(grad_naive - grad_vectorized, ord='fro')
    print('Loss difference: %f' % np.abs(loss_naive - loss_vectorized))
    print('Gradient difference: %f' % grad_difference)

    #######################################################################################
    #                           Stochastic Gradient Descent                               #
    #######################################################################################

    # Now implement SGD in Softmax.train() function and run it with the code below
    softmax = Softmax()
    tic = time.time()
    loss_hist = softmax.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))

    plt.plot(loss_hist)
    plt.xlabel('Iteration number')
    plt.ylabel('Loss value')
    plt.show()
Example #8
0
from cs231n.classifiers.linear_classifier import Softmax
results = {}
best_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-7]
regularization_strengths = [2.5e4, 5e4]

################################################################################
# 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 softmax classifer in best_softmax.                          #
################################################################################
for lr in learning_rates:
    for reg in regularization_strengths:
        softm = Softmax()
        softm.train(X_train,
                    y_train,
                    learning_rate=lr,
                    reg=reg,
                    num_iters=1500,
                    verbose=False)
        y_train_pred = softm.predict(X_train)
        y_val_pred = softm.predict(X_val)
        acc_train = np.mean(y_train == y_train_pred)
        acc_val = np.mean(y_val == y_val_pred)
        results[(lr, reg)] = (acc_train, acc_val)
        if acc_val > best_val:
            best_val = acc_val
            best_softmax = softm
################################################################################
Example #9
0
results = {}
best_val = -1
best_softmax = None
learning_rates = [1e-7, 5e-7, 2e-6]
regularization_strengths = [1e4, 3e4, 5e4]

################################################################################
# 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 softmax classifer in best_softmax.                          #
################################################################################
for l_r_cv in learning_rates:
    for r_s_cv in regularization_strengths:
        softm = Softmax()
        softm.train(X_train, y_train, learning_rate=l_r_cv, reg=r_s_cv, num_iters=1500, verbose=True)
        y_train_pred = softm.predict(X_train)
        t_ac = np.mean(y_train == y_train_pred)
        y_val_pred = softm.predict(X_val)
        v_ac = np.mean(y_val == y_val_pred)
        if v_ac > best_val:
            best_val = v_ac
            best_softmax = softm
        results[(l_r_cv, r_s_cv)] = (t_ac, v_ac)
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

# Print out results.
for lr, reg in sorted(results):