UFLDL 教程学习笔记(一)
? ? ?ufdl的新教程,從基礎(chǔ)學(xué)起。第一節(jié)講的是線性回歸。主要目的是熟悉目標(biāo)函數(shù),計(jì)算梯度和優(yōu)化。
? ? ?按著教程寫(xiě)完代碼后,總是編譯出錯(cuò),一查是mex的原因,實(shí)在不想整了。
? ? ?這位博主用的是向量,比較簡(jiǎn)潔:http://blog.csdn.net/lingerlanlan/article/details/38377023
? ? ? 這位博主用的是循環(huán),更好理解:http://www.cnblogs.com/william7neral/p/4448566.html
? ? ? 接下來(lái)是logistic regression和向量化,沒(méi)什么好說(shuō)的:http://blog.csdn.net/lingerlanlan/article/details/38390085
? ? ??
? ? ? 這節(jié)主要是完成liner_regression.m,包括計(jì)算目標(biāo)函數(shù)f和梯度g,需要注意的是要分清變量是列向量還是行向量或是矩陣。
? ? ? ?對(duì)matlab不太熟,所以我稍微注釋了下
? ? ??
function [f,g] = linear_regression(theta, X,y)%% Arguments:% theta - A vector containing the parameter values to optimize.列向量% X - The examples stored in a matrix.% X(i,j) is the i'th coordinate of the j'th example.% y - The target value for each example. y(j) is the target for% example j.行向量%%size(a,1)求矩陣的行數(shù) size(a,2)求矩陣的列數(shù),相當(dāng)于length(a)%size(a)同時(shí)求矩陣的行和列數(shù)% m=size(X,2);m = size(X,2);n=size(X,1);f=0;g=zeros(size(theta));%% TODO: Compute the linear regression objective by looping over the examples in X.% Store the objective function value in 'f'.%% TODO: Compute the gradient of the objective with respect to theta by looping over% the examples in X and adding up the gradient for each example. Store the% computed gradient in 'g'.%%% YOUR CODE HERE %%% h = theta' * X f = (1/2)* (h-y)' * (h-y) g = X * (h - y)' View Code?
? ? ??
?
? ? ??
轉(zhuǎn)載于:https://www.cnblogs.com/573177885qq/p/4783465.html
總結(jié)
以上是生活随笔為你收集整理的UFLDL 教程学习笔记(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 分期手续费怎么算 只需计算每期金额与费率
- 下一篇: 钱袋宝扣钱了怎么办