9038

Logisitic Regression Cost Function

Question:

function [J, grad] = costFunction(theta, X, y) m = length(y); h = sigmoid(X*theta); sh = sigmoid(h); grad = (1/m)*X'*(sh - y); J = (1/m)*sum(-y.*log(sh) - (1 - y).*log(1 - sh)); end

I'm trying to compute the cost function for logistic regression. Can someone please tell me why this isn't accurate?

<a href="https://i.stack.imgur.com/LLDby.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/LLDby.png" data-original="https://i.stack.imgur.com/LLDby.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Update: Sigmoid function

function g = sigmoid(z) g = zeros(size(z)); g = 1./(1 + exp(1).^(-z)); end

Answer1:

As Dan stated, your costFunction calls sigmoid twice. First, it performs the sigmoid function on X*theta; then it performs the sigmoid function again on the result of sigmoid(X*theta). Thus, sh = sigmoid(sigmoid(X*theta)). Your cost function should only call the sigmoid function once.

See the code below, I removed the sh variable and replaced it with h everywhere else. This causes the sigmoid function to only be called once.

function [J, grad] = costFunction(theta, X, y) m = length(y); h = sigmoid(X*theta); grad = (1/m)*X'*(h - y); J = (1/m)*sum(-y.*log(h) - (1 - y).*log(1 - h)); end

Recommend

  • How to set fetchSize for concrete query?
  • C++ vector storing base class pointers
  • How to resolve scala.MatchError when creating a Data Frame
  • Convert a long list to binary dataframe [duplicate]
  • Installing extensions to the bundled PHP in OS X
  • How to access a PHP resource
  • Computing Moving Average in Haskell
  • Should I declare system call functions in C?
  • Passing parameters to JasperReports SQLl statement from Java
  • angular 2 pass array to custom validator (Template driven form)
  • How should we integrate PayPal adaptive (and IPN) API in a Java webserver environment?
  • Escape space in filepath
  • how to parse kotlin code?
  • Why are d3's select() and selectAll() behaving differently here?
  • Having a dynamic View folder path during runtime in laravel 5.3
  • how to assign value in location array for google map?
  • Binding without XAML [WPF]
  • error using selenium chromedriver on windows 7 64 bit
  • mat tab inside tab selected index not working
  • Setting headers for CakePHP Controller unit tests
  • Inno Setup: Checking existence of a file in 32-bit System32 (Sysnative) folder
  • Does OAuth “state” mitigate any genuinely dangerous attacks?
  • Login working for Iphone but not android
  • How to parse JSON in Spring Boot Application automatically using Jackson
  • PushPlugin not found, or is not a CDVPlugin
  • pass sessionid through jquery ajax call to php
  • Rust lifetime error
  • 'float' object cannot be interpreted as an integer
  • How to use array in autohotkey?
  • Generate and export point cloud from Project Tango
  • Problems to understand DXGI DirectX 11 Desktop Duplication to get a Buffer or Array
  • C++ STL stack pop operation giving segmentation fault
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • VSTS work items list through REST API
  • Can someone explain this Java code (formatting the output using System.out.format) to me?