相关疑难解决方法(0)

逻辑回归中的成本函数给出NaN作为结果

我正在使用批量梯度下降实现逻辑回归.输入样本有两类要分类.类是1和0.在训练数据时,我使用以下sigmoid函数:

t = 1 ./ (1 + exp(-z));
Run Code Online (Sandbox Code Playgroud)

哪里

z = x*theta
Run Code Online (Sandbox Code Playgroud)

我使用以下成本函数来计算成本,以确定何时停止培训.

function cost = computeCost(x, y, theta)
    htheta = sigmoid(x*theta);
    cost = sum(-y .* log(htheta) - (1-y) .* log(1-htheta));
end
Run Code Online (Sandbox Code Playgroud)

我在每一步的成本都是NaN,因为htheta在大多数情况下,值为1或0.我该怎么做才能确定每次迭代的成本价值?

这是逻辑回归的梯度下降代码:

function [theta,cost_history] = batchGD(x,y,theta,alpha)

cost_history = zeros(1000,1);

for iter=1:1000
  htheta = sigmoid(x*theta);
  new_theta = zeros(size(theta,1),1);
  for feature=1:size(theta,1)
    new_theta(feature) = theta(feature) - alpha * sum((htheta - y) .*x(:,feature))                         
  end
  theta = new_theta;
  cost_history(iter) = computeCost(x,y,theta);
end
end
Run Code Online (Sandbox Code Playgroud)

matlab classification machine-learning gradient-descent logistic-regression

15
推荐指数
2
解决办法
8782
查看次数

MATLAB中的拉普拉斯图像滤波与锐化图像

我试图"翻译"冈萨雷斯和伍兹(第2版)中关于拉普拉斯滤波器的内容.

我已经阅读了图片并创建了过滤器.但是,当我尝试显示结果时(通过减法,因为-ve中的中心元素),我没有得到教科书中的图像.

我认为主要原因是"缩放".但是,我不确定如何做到这一点.据我所知,一些在线资源表示缩放只是为了使值在0-255之间.从我的代码中,我看到值已经在该范围内.

我真的很感激任何指针.

以下是我使用的原始图像:

在此输入图像描述

下面是我的代码,以及由此产生的锐化图像.

谢谢!

clc;
close all;
a = rgb2gray(imread('e:\moon.png'));
lap = [1 1 1; 1 -8 1; 1 1 1];
resp = uint8(filter2(lap, a, 'same'));
sharpened = imsubtract(a, resp);
figure; 
subplot(1,3,1);imshow(a); title('Original image');
subplot(1,3,2);imshow(resp); title('Laplacian filtered image');
subplot(1,3,3);imshow(sharpened); title('Sharpened image');
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

matlab image image-processing filter

5
推荐指数
1
解决办法
8500
查看次数