首页 > 其他分享 >CS231N Assignment1 softmax 笔记

CS231N Assignment1 softmax 笔记

时间:2023-10-05 22:44:24浏览次数:37  
标签:loss CS231N Assignment1 score softmax exp np accuracy

  • -为Softmax分类器实现完全矢量化的损失函数
  • -实现解析梯度完全矢量化的表达式
  • 使用数值梯度检查实现结果
  • 使用验证集调整学习率和正则化强度
  • 使用SGD优化损失函数
  • 可视化最终学习的权重

softmax.ipynb

库、绘图设置和数据的导入和SVM一样

Train data shape:  (49000, 3073)
Train labels shape:  (49000,)
Validation data shape:  (1000, 3073)
Validation labels shape:  (1000,)
Test data shape:  (1000, 3073)
Test labels shape:  (1000,)
dev data shape:  (500, 3073)
dev labels shape:  (500,)

Softmax Classifier

 `cs231n/classifiers/softmax.py` 首先完成带嵌套循环的softmax_loss_naive
def softmax_loss_naive(W, X, y, reg):
    # Initialize the loss and gradient to zero.
    loss = 0.0
    dW = np.zeros_like(W) #创建一个与W具有相同形状的全零数组。

    N = X.shape[0]
    for i in range(N):
        score = X[i].dot(W) #长度为C?
        exp_score = np.exp(score - np.max(score)) #防止溢出
        loss += -np.log(exp_score[y[i]]/np.sum(exp_score)) / N #复刻公式
        #loss += (-np.log(exp_score[y[i]])+ np.log(np.sum(exp_score))) / N #展开
        dexp_score = np.zeros_like(exp_score)
        dexp_score[y[i]] -= 1/exp_score[y[i]]/N
        dexp_score += 1 /np.sum(exp_score) / N
        dscore = dexp_score *exp_score 
        dW += X[[i]].T.dot([dscore])
    loss +=reg*np.sum(W**2)
    dW += 2*reg*W
    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
   
    return loss, dW

注意使用exp避免数值溢出之后要用本地梯度乘上游梯度得到梯度值。

向量化的softmax_loss_vectorized

def softmax_loss_vectorized(W, X, y, reg):

    # Initialize the loss and gradient to zero.
    loss = 0.0
    dW = np.zeros_like(W)
                                                      #
   
    scores = X.dot(W)
    #exp_score = np.exp(score - np.max(score))
    scores -= np.max(scores, axis=1, keepdims=True)#保持dim
    exp_scores = np.exp(scores)

    probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)

    # Compute the loss
    N = X.shape[0]  #有点不熟悉这个维度012的顺序
    loss = np.sum(-np.log(probs[np.arange(N), y])) / N
    loss +=  reg * np.sum(W * W) #正则化强度的系数其实无所谓?只要不太小应该效果都差不多

    # Compute the gradient
    dscores = probs
    dscores[np.arange(N), y] -= 1
    dscores /= N

    dW = X.T.dot(dscores)
    dW += reg * W


    return loss, dW

超参数调试

# 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 over 0.35 on the validation set.

from cs231n.classifiers import Softmax
results = {}
best_val = -1
best_softmax = 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 softmax classifer in best_softmax.                          #
################################################################################

# Provided as a reference. You may or may not want to change these hyperparameters
learning_rates = [3e-7,4e-7,5e-7]
regularization_strengths = [0.5e4, 1e4,1.5e4,2e4]

# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

# Iterate over all hyperparameter combinations
for lr in learning_rates:
    for reg in regularization_strengths:
        # Create a new Softmax classifier
        softmax = Softmax()
        
        # Train the classifier on the training set
        softmax.train(X_train, y_train, learning_rate=lr, reg=reg, num_iters=1000)
        
        # Evaluate the classifier on the training and validation sets
        train_accuracy = np.mean(softmax.predict(X_train) == y_train)
        val_accuracy = np.mean(softmax.predict(X_val) == y_val)
        
        # Save the results for this hyperparameter combination
        results[(lr, reg)] = (train_accuracy, val_accuracy)
        
        # Update the best validation accuracy and best classifier
        if val_accuracy > best_val:
            best_val = val_accuracy
            best_softmax = softmax

# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    
# 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)

目前调出来比较好一点的是

lr 5.000000e-07 reg 5.000000e+03 train accuracy: 0.386000 val accuracy: 0.392000

最后看看在test上的准确率

# evaluate on test set
# Evaluate the best softmax on test set
y_test_pred = best_softmax.predict(X_test)
test_accuracy = np.mean(y_test == y_test_pred)
print('softmax on raw pixels final test set accuracy: %f' % (test_accuracy, ))
softmax on raw pixels final test set accuracy: 0.384000

对比一下不同步数的权重图像差异

 

 

  

 

 

 

 

 

 

100 500 1000

 

 

 

 

 

1500 3000 5000

(多整了一些)噪点的减少还是非常明显的,虽然1500之后准确率没太大区别

标签:loss,CS231N,Assignment1,score,softmax,exp,np,accuracy
From: https://www.cnblogs.com/sihangao/p/17742714.html

相关文章

  • CS231N Assignment1 SVM 笔记(更新中)
    svm.ipynb为SVM实现一个完全矢量化的损失函数为其解析梯度实现完全矢量化表达式使用数值梯度检查您的实现使用验证集调整学习率和正则化使用 SGD优化损失函数可视化最终学习权重第一部分1.一些配置和库的导入#Runsomesetupcodeforthisnotebook.importrand......
  • pytorch(2) softmax回归
    https://tangshusen.me/Dive-into-DL-PyTorch/#/chapter03_DL-basics/3.4_softmax-regression '''softmax将未规范化的预测变换为非负数并且总和为1我们首先对每个未规范化的预测求幂,这样可以保证输出非负。同时令模型可保持导的性质为了保证最终输出的概率值总和为1......
  • softmax,logsumexp, softmax的上溢(overflow)或下溢
    LSE:logsumexp   ......
  • CS231n: Convolutional Neural Networks for Visual Recognition
    CS231n:ConvolutionalNeuralNetworksforVisualRecognitionEventTypeDateDescriptionCourseMaterialsLecture1Tuesday April4CourseIntroduction Computervisionoverview Historicalcontext Courselogistics[slides] [video]Lecture2Thursday April6Image......
  • 特征学习——特征工程自动化,无非类似CNN最后一层softmax前的输出层就是特征表征层,但那
    通过representationlearning,我们可以把一些抽象的知识转化为具体的数值的形式,例如我们使用word2vec对“上下文”的模糊的概念进行了具象的表达,生成的wordvector包含了这种先验知识(具体的表现形式就是常出现在上下文里的单词其向量的距离很接近,实际上理解word2vec是基于embedding......
  • 【机器学习】softmax回归
    SoftmaxRegression(多标签分类)将多输入的分类值转化为[0,1]的概率分布,进而进行逻辑回归算法softmax能将差距大的数值距离拉得更大,但是数值可能会溢出SoftmaxFunction数学表达式\[a_j=\frac{e^{z_j}}{\sum_{k=1}^{N}{e^{z_k}}}\]代码defmy_softmax(z):ez=n......
  • Attention机制竟有bug?Softmax是罪魁祸首,影响所有Transformer
    前言 「大模型开发者,你们错了。」本文转载自机器之心仅用于学术分享,若侵权请联系删除欢迎关注公众号CV技术指南,专注于计算机视觉的技术总结、最新技术跟踪、经典论文解读、CV招聘信息。CV各大方向专栏与各个部署框架最全教程整理【CV技术指南】CV全栈指导班、基础入门班、论......
  • softmax回归模型——pytroch版
    importtorchfromIPythonimportdisplayfromd2limporttorchasd2l#fromd2l.mxnetimportAccumulatorbatch_size=256#每次读256张图片,返回训练iter和测试itertrain_iter,test_iter=d2l.load_data_fashion_mnist(batch_size)num_inputs=784num_outputs......
  • softmax回归模型simple——pytroch版
    importtorchfromtorchimportnnfromd2limporttorchasd2lbatch_size=256train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size)#PyTorch不会隐式地调整输入的形状。因此,#我们在线性层前定义了展平层(flatten),来调整网络输入的形状net=nn.Sequenti......
  • softmax回归
    前面我们学习了线性回归,线性回归主要用于对于问题的预测,输出一个结果值,但问题往往不止这一种,我们每天也在处理很多分类的问题,要的结果是哪一种。所以本节学习softmax回归模型分类问题对于分类问题,我们要的结果是输出一个类别统计学家很早以前就发明了一种表示分类数据的简单方法:独......