python - 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
解决方案
@mfunction
和都是elmul
OMPC 定义的函数。第一个是正确处理marray
OMPC 定义的特殊类型,第二个是对上述marray
s 进行逐元素乘法。它们旨在利用 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
)。
推荐阅读
- javascript - 无法从代码隐藏设置 div 样式
- svn - 如何递归地从基础存储库中删除或清除 svn:ignore 列表。我想清除所有 svn:ignore
- java - 我正在尝试构建强大的数字程序,但我没有得到预期的输出
- c++ - 了解 reinterpret_cast
- google-apps-script - 如何修复谷歌脚本中的“getNextDataCell下一个跳过一个单元格”错误
- amazon-web-services - AWS CloudFormation:AWS::Events::Rule - InputTemplate
- json - CSV 数据(不是文件)到 json
- ios - iOS 13 Public Beta 2 - Combine.Future "dyld: Symbol not found"
- web-component - children 和 parentNode 命令不适用于 Web 组件
- mysql - 如何选择从伦敦向所有项目出售 P1 件的供应商?