热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[CS231n@Stanford]Assignment1-Q3(python)Softmax实现

softmax.pyimportnumpyasnpfromrandomimportshuffledefsoftmax_loss_naive(W,X,y,


softmax.py

import numpy as np
from random import shuffle

def softmax_loss_naive(W, X, y, reg):
  """
  Softmax loss function, naive implementation (with loops)

  Inputs have dimension D, there are C classes, and we operate on minibatches
  of N examples.

  Inputs:
  - W: A numpy array of shape (D, C) containing weights.
  - X: A numpy array of shape (N, D) containing a minibatch of data.
  - y: A numpy array of shape (N,) containing training labels; y[i] = c means
    that X[i] has label c, where 0 <= c  
 
linear_classifier.py 的实现参见:http://blog.csdn.net/zzhangjizhi/article/details/52457278


softmax.ipynb的部分代码实现

# 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 linear_classifier import Softmax
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.                          #
################################################################################


iters = 2000  
for lr in learning_rates:  
    for reg in regularization_strengths:  
        softmax = Softmax()  
        softmax.train(X_train, y_train, learning_rate=lr, reg=reg, num_iters=iters)  
          
        y_train_pred = softmax.predict(X_train)  
        acc_train = np.mean(y_train == y_train_pred)  
          
        y_val_pred = softmax.predict(X_val)  
        acc_val = np.mean(y_val == y_val_pred)  
  
        results[(lr, reg)] = (acc_train, acc_val)  
          
        if best_val  
 

lr 1.000000e-07 reg 5.000000e+04 train accuracy: 0.333633 val accuracy: 0.343000
lr 1.000000e-07 reg 1.000000e+08 train accuracy: 0.100265 val accuracy: 0.087000
lr 5.000000e-07 reg 5.000000e+04 train accuracy: 0.326980 val accuracy: 0.341000
lr 5.000000e-07 reg 1.000000e+08 train accuracy: 0.100265 val accuracy: 0.087000
best validation accuracy achieved during cross-validation: 0.343000
softmax on raw pixels final test set accuracy: 0.348000






推荐阅读
author-avatar
高朗1_114
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有