斯坦福大學(吳恩達) 機器學習課後習題詳解 第四周 程式設計題 多分類和神經網路
作業下載地址:https://download.csdn.net/download/wwangfabei1989/10300890
1. 邏輯迴歸代價函式 lrCostFuction
function [J, grad] = lrCostFunction(theta, X, y, lambda)
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with
%regularization
% J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Hint: The computation of the cost function and gradients can be
% efficiently vectorized. For example, consider the computation
%
% sigmoid(X * theta)
%
% Each row of the resulting matrix will contain the value of the
% prediction for that example. You can make use of this to vectorize
% the cost function and gradient computations.
%
% Hint: When computing the gradient of the regularized cost function,
% there're many possible vectorized solutions, but one solution
% looks like:
% grad = (unregularized gradient for logistic regression)
% temp = theta;
% temp(1) = 0; % because we don't add anything for j = 0
% grad = grad + YOUR_CODE_HERE (using the temp variable)
%
theta_t=theta;
theta_t(1)=0;
J=(1/m)*(-y'*log(sigmoid(X*theta))-(1-y')*log(1-sigmoid(X*theta)))+(lambda/(2*m))*sum(theta_t.^2);
grad=(1/m)*X'*(sigmoid(X*theta)-y)+(lambda/m)*theta_t;
% =============================================================
grad = grad(:);
end
2. 多分類器 oneVsAll
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta
%corresponds to the classifier for label i
% [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
% logistic regression classifiers and returns each of these classifiers
% in a matrix all_theta, where the i-th row of all_theta corresponds
% to the classifier for label i
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
% ====================== YOUR CODE HERE ======================
% Instructions: You should complete the following code to train num_labels
% logistic regression classifiers with regularization
% parameter lambda.
%
% Hint: theta(:) will return a column vector.
%
% Hint: You can use y == c to obtain a vector of 1's and 0's that tell you
% whether the ground truth is true/false for this class.
%
% Note: For this assignment, we recommend using fmincg to optimize the cost
% function. It is okay to use a for-loop (for c = 1:num_labels) to
% loop over the different classes.
%
% fmincg works similarly to fminunc, but is more efficient when we
% are dealing with large number of parameters.
%
% Example Code for fmincg:
%
% % Set Initial theta
% initial_theta = zeros(n + 1, 1);
%
% % Set options for fminunc
% options = optimset('GradObj', 'on', 'MaxIter', 50);
%
% % Run fmincg to obtain the optimal theta
% % This function will return theta and the cost
% [theta] = ...
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
% initial_theta, options);
%
initial_theta = zeros(n + 1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 50);
for c = 1:num_labels
theta=fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options)
all_theta(c,:)=theta'
end
% =========================================================================
end
3. 多分類器預測 predictOneVsAll
function p = predictOneVsAll(all_theta, X)
%PREDICT Predict the label for a trained one-vs-all classifier. The labels
%are in the range 1..K, where K = size(all_theta, 1).
% p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
% for each example in the matrix X. Note that X contains the examples in
% rows. all_theta is a matrix where the i-th row is a trained logistic
% regression theta vector for the i-th class. You should set p to a vector
% of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
% for 4 examples)
m = size(X, 1);
num_labels = size(all_theta, 1);
% You need to return the following variables correctly
p = zeros(size(X, 1), 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned logistic regression parameters (one-vs-all).
% You should set p to a vector of predictions (from 1 to
% num_labels).
%
% Hint: This code can be done all vectorized using the max function.
% In particular, the max function can also return the index of the
% max element, for more information see 'help max'. If your examples
% are in rows, then, you can use max(A, [], 2) to obtain the max
% for each row.
%
[maxnum, p]=max(sigmoid(X*all_theta'),[],2)
% =========================================================================
end
4. 神經網路預測predict
function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
% trained weights of a neural network (Theta1, Theta2)
% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);
% You need to return the following variables correctly
p = zeros(size(X, 1), 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned neural network. You should set p to a
% vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. In particular, the max
% function can also return the index of the max element, for more
% information see 'help max'. If your examples are in rows, then, you
% can use max(A, [], 2) to obtain the max for each row.
%
%先將X 新增 一個單位列
X=[ones(m,1) X]
%第二層神經網路
secondLayer=sigmoid(X*Theta1')
%輸出層
%將第二層神經網路 新增一個 單位列
outputLayer=sigmoid([ones(m,1) secondLayer]*Theta2')
[maxnum,p]=max(outputLayer,[],2)
% =========================================================================
end
相關推薦
斯坦福大學(吳恩達) 機器學習課後習題詳解 第四周 程式設計題 多分類和神經網路
作業下載地址:https://download.csdn.net/download/wwangfabei1989/103008901. 邏輯迴歸代價函式 lrCostFuctionfunction [J, grad] = lrCostFunction(theta, X, y,
斯坦福大學(吳恩達) 機器學習課後習題詳解 第二週 程式設計題 線性迴歸
習題可以去這個地址下載 http://download.csdn.net/download/wwangfabei1989/102654071. warmUpExercise.m的實現如下: function A = warmUpExercise()%WARMUPEXERCI
斯坦福大學(吳恩達) 機器學習課後習題詳解 第七週程式設計題 SVM
作業下載地址:https://download.csdn.net/download/wwangfabei1989/103046171. 高斯核函式gaussianKernelfunction sim = gaussianKernel(x1, x2, sigma)%RBFKER
斯坦福大學(吳恩達) 機器學習課後習題詳解 第六週 程式設計題 正則化線性迴歸以及方差與偏差
作業下載地址:https://download.csdn.net/download/wwangfabei1989/103031341. 正則化線性迴歸代價函式 linearRegCostFunctionfunction [J, grad] = linearRegCostFun
斯坦福大學(吳恩達) 機器學習課後習題詳解 第九周 程式設計題 異常檢測與推薦系統
作業 下載 地址:https://download.csdn.net/download/wwangfabei1989/103261751. estimateGaussian.m function [mu sigma2] = estimateGaussian(X)%ESTIMA
斯坦福大學(吳恩達) 機器學習課後習題詳解 第八週 程式設計題 k-means and PCA
程式設計作業下載地址:https://download.csdn.net/download/wwangfabei1989/103181651. PCA.mfunction [U, S] = pca(X)%PCA Run principal component analysis
吳恩達機器學習筆記(二)(附程式設計作業連結)
吳恩達機器學習筆記(二) 標籤: 機器學習 一.邏輯迴歸(logistic regression) 1.邏輯函式&&S型函式(logistic function and sigmoid function) 線性迴歸的假設表示
Coursera 斯坦福吳恩達機器學習課程筆記 (1)
看了課程一週後發現忘光了,決定做一個筆記用作複習。如果涉及到侵權問題請聯絡我,我會立馬刪除並道歉。同時,禁止任何形式的轉載,包括全文轉載和部分轉載。如需使用請聯絡本人 [email protected]。如若發現侵權行為,我學過智慧財產權法的,嘿嘿第一週:基礎概念和
吳恩達機器學習總結:第十一 降維(PCA)(大綱摘要及課後作業)
為了更好的學習,充分複習自己學習的知識,總結課內重要知識點,每次完成作業後都會更博。總結1.動機I:資料壓縮(1)壓縮 a.加速演算法 b.減小資料空間 c.2維降為1維例子 d.3維降為2維例子 e.在實際中,我們正常會將1000維將為1
吳恩達“機器學習”——學習筆記二
最大似然 數據 learning 模型 ima 我們 回歸 eps 而是 定義一些名詞 欠擬合(underfitting):數據中的某些成分未被捕獲到,比如擬合結果是二次函數,結果才只擬合出了一次函數。 過擬合(overfitting):使用過量的特征集合,使模型過於復雜。
吳恩達“機器學習”——學習筆記八
包含 找到 trade 經驗 這也 ... info 算法 不等式 偏差方差權衡(bias variance trade off) 偏差:如果說一個模型欠擬合,也可以說它的偏差很大。 方差:如果說一個模型過擬合,也可以說它的方差很大。 訓練誤差 經驗風險最小化(ERM)
【吳恩達機器學習】學習筆記——1.5無監督學習
分類 哪些 rep epm 朋友 工作 style class 客戶 1 無監督學習:在不知道數據點的含義的情況下,從一個數據集中找出數據點的結構關系。 2 聚類算法:相同屬性的數據點會集中分布,聚集在一起,聚類算法將數據集分成不同的聚類。也就是說,機器不知道這些數據點具體
【吳恩達機器學習】學習筆記——2.1單變量線性回歸算法
工作方式 樣本 body 聚類 屬性 bsp 定義 算法 信息 1 回顧1.1 監督學習定義:給定正確答案的機器學習算法分類:(1)回歸算法:預測連續值的輸出,如房價的預測(2)分類算法:離散值的輸出,如判斷患病是否為某種癌癥1.2 非監督學習定義:不給定數據的信息的情況下
【吳恩達機器學習】學習筆記——代價函數
info alt 學習 ima 代價函數 png 線性回歸 gpo mage 單變量線性回歸函數 hθ(x) = θ0 + θ1x 為了使線性回歸函數對數據有較好的預測性,即y到h(x)的距離都很小。 【吳恩達機器學習】學習筆記——代價函數
【吳恩達機器學習】學習筆記——梯度下降
得到 向導 bubuko gpo 思路 pos 方向導數 ... image 梯度下降算法能夠幫助我們快速得到代價函數的最小值 算法思路: 以某一參數為起始點 尋找下一個參數使得代價函數的值減小,直到得到局部最小值 梯度下降算法: 重復下式直至收斂,其中α為學習速
【吳恩達機器學習】學習筆記——2.7第一個學習算法=線性回歸+梯度下降
com 梯度 .com 局部最優 alt ima 實現 梯度下降 width 梯度下降算法: 線性回歸模型: 線性假設: 平方差成本函數: 將各個公式代入,對θ0、θ1分別求偏導得: 再將偏
Coursera-AndrewNg(吳恩達)機器學習筆記——第三周
訓練 ros 方便 font 就是 梯度下降 全局最優 用法 郵件 一.邏輯回歸問題(分類問題) 生活中存在著許多分類問題,如判斷郵件是否為垃圾郵件;判斷腫瘤是惡性還是良性等。機器學習中邏輯回歸便是解決分類問題的一種方法。二分類:通常表示為y?{0,1},0:“Negat
吳恩達機器學習第5周Neural Networks(Cost Function and Backpropagation)
and div bsp 關於 邏輯回歸 info src clas 分享 5.1 Cost Function 假設訓練樣本為:{(x1),y(1)),(x(2),y(2)),...(x(m),y(m))} L = total no.of layers in network
吳恩達機器學習筆記 —— 5 多變量線性回歸
擬合 進行 image 價格 常用 從表 cnblogs 優化 深度 本篇主要講的是多變量的線性回歸,從表達式的構建到矩陣的表示方法,再到損失函數和梯度下降求解方法,再到特征的縮放標準化,梯度下降的自動收斂和學習率調整,特征的常用構造方法、多維融合、高次項、平方根,最後基
吳恩達機器學習筆記 —— 9 神經網絡學習
滿了 線性回歸 復雜 amp 技術分享 tps 機器 神經網絡 前饋型神經網絡 本章講述了神經網絡的起源與神經元模型,並且描述了前饋型神經網絡的構造。 更多內容參考 機器學習&深度學習 在傳統的線性回歸或者邏輯回歸中,如果特征很多,想要手動組合很多有效的特征是不