首页 > 解决方案 > mfunction 和 elmul 有什么用?

问题描述

我正在将程序从 Octave 转换为 Python,为此我使用 OMPC 来尝试看看它是否有效,当我在顶部获得 Python 代码的转换时,有一行说@mfunction("")我不确定是什么是的,我还没有找到很多关于它的信息。另外elmul,我也不知道它是做什么用的,也没有找到有关它的信息。

这是 Octave 上的代码

function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
%   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
%   parameter for logistic regression and the gradient of the cost
%   w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
% Use sigmoid function previously programed 
hypothesis = sigmoid(X*theta); % Hypothesis for logistic regression 
Weight = 1/m;


J = -Weight*sum( ( y.*log(hypothesis) + (1 - y).*log(1 - hypothesis) ) );


for i = 1 : m
    grad = grad + (hypothesis(i) - y(i)) * X(i,:)'; % X must be transposed
end

grad = Weight*grad;


% =============================================================

end

这是 OMPC 生成的代码:

@mfunction("J, grad")
def costFunction(theta=None, X=None, y=None):
    #COSTFUNCTION Compute cost and gradient for logistic regression
    #   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
    #   parameter for logistic regression and the gradient of the cost
    #   w.r.t. to the parameters.

    # Initialize some useful values
    m = length(y)  # number of training examples

    # You need to return the following variables correctly 
    J = 0
    grad = zeros(size(theta))

    # ====================== YOUR CODE HERE ======================
    # Instructions: Compute the cost of a particular choice of theta.
    #               You should set J to the cost.
    #               Compute the partial derivatives and set grad to the partial
    #               derivatives of the cost w.r.t. each parameter in theta
    #
    # Note: grad should have the same dimensions as theta
    # Use sigmoid function previously programed 
    hypothesis = sigmoid(X * theta)# Hypothesis for logistic regression
    Weight = 1 / m


    J = -Weight * sum((y *elmul* log(hypothesis) + (1 - y) *elmul* log(1 - hypothesis)))


    for i in mslice[1:m]:
        grad = grad + (hypothesis(i) - y(i)) * X(i, mslice[:]).cT    # X must be transposed
        end

        grad = Weight * grad


        # =============================================================

        end

标签: pythonoctave

解决方案


@mfunction和都是elmulOMPC 定义的函数。第一个是正确处理marrayOMPC 定义的特殊类型,第二个是对上述marrays 进行逐元素乘法。它们旨在利用 NumPy 和 MATLAB 之间的语义差异。然而,这些差异很难完全概括,尤其是因为 MATLAB 代码通常依赖于这些语义细节。

这就是为什么不能在生产代码中可靠地使用 MATLAB 到 Python 转换器的原因。

我会将上面的 Octave 代码翻译成类似的东西(假设 NumPy 数组输入):

def cost_function(theta, x, y):
    m = np.size(y)
    grad = np.zeros(theta.shape)

    hypothesis = sigmoid(x @ theta)

    j = -np.sum((y * np.log(hypothesis) + (1 - y) * np.log(1 - hypothesis))) / m

    for i in range(m):
        grad = grad + (hypothesis[i] - y[i]) @ x[i, :].transpose().conjugate()

    grad = grad / m
    return j, grad

请仔细检查x[i, :].transpose().conjugate()实际需要的内容(这取决于实际需要x的内容hypothesis)。


推荐阅读