我是靠谱客的博主 自然身影,最近开发中收集的这篇文章主要介绍cs231n assignment1 features作业,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

# Use the validation set to tune the learning rate and regularization strength

from cs231n.classifiers.linear_classifier import LinearSVM

learning_rates = [1e-9, 1e-8, 1e-7]
regularization_strengths = [5e4, 5e5, 5e6]

results = {}
best_val = -1
best_svm = None

################################################################################
# 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:
        svm = LinearSVM()
        svm.train(X_train_feats, y_train, learning_rate=lr, reg=reg, num_iters=4000,
            batch_size=200, verbose=False)
        
        train_pre = svm.predict(X_train_feats)
        train_acc = np.mean(train_pre == y_train)
        val_pre = svm.predict(X_val_feats)
        val_acc = np.mean(val_pre == y_val)
        
        if val_acc > best_val:
            best_val = val_acc
            best_svm = svm
        
        results[(lr,reg)] = train_acc,val_acc
        
################################################################################
#                              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)

提示准确率能接近0.44,然而我的实验结果只有0.422,至于test_accuracy为0.425

from cs231n.classifiers.neural_net import TwoLayerNet

input_dim = X_train_feats.shape[1]
hidden_dim = 500
num_classes = 10

net = TwoLayerNet(input_dim, hidden_dim, num_classes)
best_net = None

################################################################################
# TODO: Train a two-layer neural network on image features. You may want to    #
# cross-validate various parameters as in previous sections. Store your best   #
# model in the best_net variable.                                              #
################################################################################
best_lr = 0
best_reg = 0
best_accuracy= 0


lr_base =0.01
lr_step =0.002
reg_base =0.005
reg_step =0.001

for lr_num in range(10):
    for reg_num in range(10):

        lr = lr_base + lr_num*lr_step
        reg = reg_base + reg_num*reg_step

        results = net.train(X_train_feats, y_train, X_val_feats, y_val,
                            num_iters=2000, batch_size=200,
                            learning_rate=lr, learning_rate_decay=0.95,
                            reg=reg, verbose=False)
        train_accuracy = np.mean(net.predict(X_train_feats) == y_train)
        val_accuracy = np.mean(net.predict(X_val_feats) == y_val)
        print("lr:%f, reg:%f, train_accuracy:%f, val_accuracy:%f"%( lr, reg, train_accuracy,val_accuracy))
        if val_accuracy > best_accuracy:
            best_accuracy = val_accuracy
            best_net = net
            best_lr = lr
            best_reg = reg
print("best model is:")
print("lr:%f, reg:%f, accuracy:%f"%(best_lr, best_reg, best_accuracy))
################################################################################
#                              END OF YOUR CODE                                #
################################################################################

我在这个实验中准确率为0.599,test_acc为0.547

最后

以上就是自然身影为你收集整理的cs231n assignment1 features作业的全部内容,希望文章能够帮你解决cs231n assignment1 features作业所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部