【深度学习】深度学习门前徘徊——正向传播
正向传播fc_c.mfc_v.mmain_c.m本博客代码为本人选修《深度学习引论》课程所用,源代码属于Zhang Yi教授。github地址fc_c.mfunction a_next = fc_c(w, a)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% implement forward computation from layer
·
本博客代码为本人选修《深度学习引论》课程所用,源代码属于Zhang Yi教授。
github地址
1.原理
2.code
fc_c.m
function a_next = fc_c(w, a)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% implement forward computation from layer l to layer l+1
% in component form
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define the activation function
f = @(s) s >= 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your code BELOW
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. add external inputs with value 1
a = [a; 1];
% for each neuron located in layer l+1
for i=1:size(w,1)
% 2. calculate net input
a_next(i) = w(i,:)*a;
% 3. calculate activation
a_next(i) = f( a_next(i) );
end
a_next = a_next';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your code ABOVE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
fc_v.m
function a_next = fc_v(w, a)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% implement forward computation from layer l to layer l+1
% in vector form
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define the activation function
f = @(s) s >= 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your code BELOW
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. add external inputs with value 1
a = [a; 1];
% 2. calculate net input
a_next = w * a;
% 3. calculate activation
a_next = f(a_next);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your code ABOVE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
main_c.m
% prepare the samples
data = [1 0 0 1
0 1 0 1]; % samples
labels = [1 1 0 0]; % labels
m = size(data, 2); % number of samples
% assign connection weight
w1 = [ 2 2 -1
-1 -1 1.5]; % connection from layer 1 to layer 2
w2 = [ 1 1 -1.5 ]; % connection from layer 2 to layer 3
% for each sample
for i = 1:m
x = data(:, i); % retrieve the i-th column of data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your code BELOW
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% layer 1 - input layer
a1 = x;
% layer 2 - hidden layer
a2 = [];
% 1. calculate the activation a2, call function fc_c
a2 = fc_c(w1, a1);
% layer 3 - output layer
a3 = [];
% 2. calculate the activation a3, call function fc_c
a3 = fc_c(w2, a2);
% collect result
y = a3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Your code ABOVE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% display the result
fprintf('Sample [%i %i] is classified as %i.\n', x(1), x(2), y);
end
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献2条内容
所有评论(0)