使用优化(最好是使用梯度)优化R中的多重输出函数 [英] Optimizing a multiple output function in R using optim, preferably with gradient

查看:79
本文介绍了使用优化(最好是使用梯度)优化R中的多重输出函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从matlab切换到R,我想运行一个优化方案.

I recently switched to R from matlab and I want to run an optimization scenario.

在matlab中,我能够:

In matlab I was able to:

options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

这里相当于costFunctionReg(在这里我称之为logisticRegressionCost)

Here is the equivalent of costFunctionReg (here I call it logisticRegressionCost)

logisticRegressionCost <- function(theta, X, y) {
    J = 0;
    theta = as.matrix(theta);
    X = as.matrix(X);
    y = as.matrix(y);   

    rows = dim(theta)[2];
    cols = dim(theta)[1];
    grad = matrix(0, rows, cols);

    predicted = sigmoid(X %*% theta);
    J = (-y) * log(predicted) - (1 - y) * log(1 - predicted);

    J = sum(J) / dim(y)[1];

    grad = t(predicted - y);
    grad = grad %*% X;
    grad = grad / dim(y)[1];

    return(list(J = J, grad = t(grad)));    
}

但是,当我尝试对其进行优化时:

However when I try to run an optimization on it like:

o = optim(theta <- matrix(0, dim(X)[2]), fn = logisticRegressionCost, X = X, y = y, method="Nelder-Mead")

由于列表返回,我得到了一个错误. (当我只返回J时,它可以工作)

I get an error due of the list return. (When I just only return J it works)

错误:

(list)对象不能被强制键入"double"

(list) object cannot be coerced to type 'double'

Q1:有没有一种方法可以指定乐观者应使用哪种收益最小化? (如fn $ J)

Q1: Is there a way to specify which return should the optim use for the minimization? (like fn$J)

Q2:是否有解决方案可以使用在logisticRegressionCost中计算出的梯度?

Q2: Is there a solution where I can use the gradient I calculate in the logisticRegressionCost?

推荐答案

我不认为您可以这样做,因为optim的文档说fn应该返回标量结果.

I don't think you can do this because the documentation for optim says fn should return a scalar result.

也许您只需要编写一个辅助函数即可进行优化.像这样:

Perhaps you could just write a helper function to optimize. Something like:

logisticRegressionCost.helper <- function(theta, X, y) {
   logisticRegressionCost(theta, X, y)$J
}

此外,您不需要使用半冒号来抑制R中的输出.当我也从MatLab切换时,我也有同样的习惯:)

Also, you don't need the semi colons to suppress output in R. I had that same habit when I switched from MatLab too :)

这篇关于使用优化(最好是使用梯度)优化R中的多重输出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆