每次上组合数学都会想: 是谁这么机智想出这么巧的方法的! 晚上和一个妹子做大数据实验,三个实验,hdfs,hbase,nosql都做了,开心耶,数据库初体验. 我终于知道当时为什么一直找不到命令,就是因为教程上的命令打错了,中文-和英文-是有区别的,啊啊啊啊啊啊啊啊! 大量不cd到文件夹,打多,复制的时候$也复制上了,复制的时候#也复制上了,打错文件名,复制的时候忘改版本号,眼花缭乱,我是不是真的不适合学计算机? 我觉得吧,向量化一个很大的用途就是不用写for loop了. 我感觉这里的x是列向量,写成x矩阵的时候,就转置成传统数据库的行形式. 完了,我已经忘记reg larized和 nreg larized是在哪里出现的了. f nction [j, grad] = lrcostf nction(theta, x, y, lambda) %lrcostfunction comp te cost and gradient for logistic regression with %reg larization % j = lrcostfunction(theta, x, y, lambda) comp tes the cost of sing % theta as the parameter for reg larized logistic regression and the % gradient of the cost w.r.t. to the parameters. % initialize some sef l val es m = length(y); % n mber of training examples % yo need to ret rn the following variables correctly j = 0; grad = zeros(size(theta)); % = your code here = % instr ctions: comp te the cost of a partic lar choice of theta. % yo sho ld set j to the cost. % comp te the partial derivatives and set grad to the partial % derivatives of the cost w.r.t. each parameter in theta % % hint: the comp tation of the cost f nction and gradients can be % efficiently vectorized. for example, consider the comp tation % % sigmoid(x * theta) % % each row of the res lting matrix will contain the val e of the % prediction for that example. yo can make se of this to vectorize % the cost f nction and gradient comp tations. % % hint: when comp ting the gradient of the reg larized cost f nction, % there re many possible vectorized sol tions, b t one sol tion % looks like: % grad = ( nreg larized gradient for logistic regression) % temp = theta; % temp(1) = 0; % beca se we don t add anything for j = 0 % grad = grad + your_code_here ( sing the temp variable) % j=(-y *log(sigmoid(x*theta))-(1-y) *log(1-sigmoid(x*thetam+lambda * s m(theta(2:end).^2)(2*m); %grad=(x *sigmoid(x*theta)-y)m; 括号写着写着就忘了,辣鸡matlab grad=(x *(sigmoid(x*theta)-y))m; %theta_s = [0; theta(2:end)]; 下面用的temp把bias值置为0 temp=theta; temp(1)=0; %grad=grad+lambdam*s m(temp); 这里公式抄错了,是没有sigma的 grad=grad+lambdam*temp; % = grad = grad(:);