iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 9
0
自我挑戰組

R語言與演算法自學系列 第 9

Day 9 Machine Learning筆記與作業分享-3

今天要分享的是ML作業的某部分,這個function作用是建立一個Neuron model,用來預測。其中使用weights權重來調整出現的狀況。

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.
%

a1 = [ones(m, 1) X];   %加上bias unit:ones(m, 1)
z2 = a1*Theta1';

a2 = [ones(size(z2, 1), 1) sigmoid(z2)];    %同樣加上bias unit
z3 = a2*Theta2';

a3 = sigmoid(z3);
h = a3;

[p_max, p] = max(h, [], 2);

% =========================================================================


end

sigmoid(logistic) activation function

Notation:
ai(j) - activation of unit i(subscript下標) in layer j(superscript上標)
theta(j) - matrix of weights controlling function mapping from layer j to layer j+1

example:
Let x = a(1);
Let z(2) = theta(1) * a(1);
a(2) = g(z(2)) belongs to R3;
add a0(2) = 1 to a(2), then a(2) belongs to R4;
z(3) = theta(2) * a(2);
h(x) = a(3) = g(z(3));

theta(1) = [theta10(1) theta11(1) theta12(1) theta13(1);
theta20(1) theta21(1) theta22(1) theta23(1);
theta30(1) theta31(1) theta32(1) theta33(1)]
x = transpose([x0 x1 x2 x3])

其實上述的例子和程式碼一樣,改用數學式寫一次。


上一篇
Day 8 德語學習小分享 - 2
下一篇
Day 10
系列文
R語言與演算法自學12

尚未有邦友留言

立即登入留言