在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
sigmoid.m文件 function g = sigmoid(z) g = zeros(size(z)); 初始化g ,z可以是一个数,一个向量或者一个矩阵 % ====================== YOUR CODE HERE ====================== 当z=0时,g=0.5; 当z<0时,g<0.5;当z>0时,g>0.5; 当z->-∞时,g->0; 当z->+∞时,g->1 z可以是一个数,一个向量或者是一个矩阵 % ============================================================= end
costFunction.m function [J, grad] = costFunction(theta, X, y) % Initialize some useful values % You need to return the following variables correctly % ====================== YOUR CODE HERE ====================== J(θ)的表达式 grad的表达式 J = 1/m*(-1*y'*log(sigmoid(X*theta)) - (ones(1,m)-y')*log(ones(m,1)-sigmoid(X*theta))); %logM是对矩阵的每个元素都是求log, exp(M)同样是表示对矩阵的每 个元素求e的底 调用的函数参见上述函数sigmoid.m grad = 1/m * (X' * (sigmoid(X*theta) - y));, % ============================================================= end
%% ============ Part 2: Compute Cost and Gradient ============ % Setup the data matrix appropriately, and add ones for the intercept term % Add intercept term to x and X_test % Initialize fitting parameters % Compute and display initial cost and gradient fprintf('Cost at initial theta (zeros): %f\n', cost); fprintf('\nProgram paused. Press enter to continue.\n'); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论