关于One vs. All回归中的函数句柄的困惑 [duplicate]

我在Coursera上学习Andrew Ng的机器学习课程,对于One vs. All分类中的一个特定例子为什么能工作感到非常困惑:

function [all_theta] = oneVsAll(X, y, num_labels, lambda)%ONEVSALL trains multiple logistic regression classifiers and returns all%the classifiers in a matrix all_theta, where the i-th row of all_theta %corresponds to the classifier for label i%   [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels%   logisitc regression classifiers and returns each of these classifiers%   in a matrix all_theta, where the i-th row of all_theta corresponds %   to the classifier for label i% Some useful variablesm = size(X, 1);n = size(X, 2);% You need to return the following variables correctly all_theta = zeros(num_labels, n + 1);% Add ones to the X data matrixX = [ones(m, 1) X];% ====================== YOUR CODE HERE ======================% Instructions: You should complete the following code to train num_labels%               logistic regression classifiers with regularization%               parameter lambda. %% Hint: theta(:) will return a column vector.%% Hint: You can use y == c to obtain a vector of 1's and 0's that tell use %       whether the ground truth is true/false for this class.%% Note: For this assignment, we recommend using fmincg to optimize the cost%       function. It is okay to use a for-loop (for c = 1:num_labels) to%       loop over the different classes.%%       fmincg works similarly to fminunc, but is more efficient when we%       are dealing with large number of parameters.%% Example Code for fmincg:%%     % Set Initial theta%     initial_theta = zeros(n + 1, 1);%     %     % Set options for fminunc%     options = optimset('GradObj', 'on', 'MaxIter', 50);% %     % Run fmincg to obtain the optimal theta%     % This function will return theta and the cost %     [theta] = ...%         fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...%                 initial_theta, options);%initial_theta = zeros(n + 1, 1);options = optimset('GradObj', 'on', 'MaxIter', 50);for i = 1:num_labels    c = i * ones(size(y));    fprintf('valores')    [theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);    all_theta(i,:) = theta;end% =========================================================================end

我特别对这一行感到困惑: [theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);lrCostFunction 定义为有参数 theta, X, y, lambda,所以我不明白 t 在这里的作用。

另外,包裹 theta 的括号有什么用: [theta]

任何关于逐步解析这段代码的帮助将不胜感激。谢谢。


回答:

您正在查看定义匿名函数的一行。匿名函数就像是函数的简写定义,以 @ 开头,后跟函数的参数(在您的例子中是 t)。这个参数 t 被传递给函数 lrCostFunction() 作为第一个参数,实际上就是 theta 参数。也就是说,您要求函数 fmincg() 最小化这个匿名函数的输出,这个匿名函数是 lrCostFunction() 的包装器,以便您可以对 theta 进行最小化,同时使用在匿名函数定义之外定义的 Xylambda

为了更好地理解匿名函数,您可以将代码拆分:

 func_handle = @(t)(lrCostFunction(t, X, (y == c), lambda)) % 匿名函数 func_handle(initial_theta); % 返回在 initial_theta 处的成本 [theta] = fmincg(func_handle, initial_theta, options);

有关匿名函数的更多详细信息,请参阅官方 Matlab文档

包裹 theta 的括号是多余的。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注