机器学习(MACHINE LEARNING)MATLAB三层神经网络的简单应用
项目地址!!!!!!!!!!!!!!!!!!!!!!!!
1 目的
进行1-100以内的质数分类
2 设计思路
1、生成1-100以内的数和对应二进制
2、对质数部分进行label为1,其余为0
3、选择前60组作为training数据,后40组testing
4、选择三层神经网络,其中hidden和output部分使用sigmoid函数
3 代码
1、测试数据生成函数
function f = dataset_generator
bits_num = 7;
prime_table = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97];
prime_numble = 25;
prime_dataset = zeros(100,8);
%Generate prime number dataset 1-100
for count = 1:100
bin_str = dec2bin(count,bits_num);
for i = 1:bits_num
prime_dataset(count,i) = str2num(bin_str(i));
end
for i = 1:prime_numble
if(count == prime_table(i))
prime_dataset(count,bits_num+1) = 1;
end
end
if(prime_dataset(count,bits_num+1)~=1)
prime_dataset(count,bits_num+1) = 0;
end
end
f = prime_dataset;
2、最优学习速率选择函数,当hidden neurons 的数量分别选择8和15时获得alpha 最优为1和0.1,ask why?
function test_script1
%Training Set
data = dataset_generator;
x_train = data(1:60,1:7);
y_train = data(1:60,8);
x_test = data(61:100,1:7);
y_test = data(61:100,8);
for pow_num = 1:5
%Learning Rate
alpha = 10^(-3+pow_num);
%Initialize the network
syn0 = 2*rand(7,15)-1;
syn1 = 2*rand(15,1)-1;
%Training the network
for i = 1:60000
l0 = x_train;
l1 = sigmoid(l0*syn0);
l2 = sigmoid(l1*syn1);
l2_error = l2 - y_train;
if(i==1)
overallerror(i) = mean(abs(l2_error));
end
if(mod(i,10000)==0)
overallerror(i/10000+1) = mean(abs(l2_error));
end
l2_delta = l2_error.*sigmoid_derivation(l2);
l1_error = l2_delta*syn1';
l1_delta = l1_error.*sigmoid_derivation(l1);
syn1 = syn1 - alpha*(l1'*l2_delta);
syn0 = syn0 - alpha*(l0'*l1_delta);
end
alpha
overallerror
end
%Testing progress
%testing_output = sigmoid(sigmoid(x_test*syn0)*syn1)
%testing_error = sum(abs(y_test - testing_output))
function s = sigmoid (x)
[m,n] = size(x);
for i = 1:m
for j = 1:n
s(i,j) = 1/(1+exp(-x(i,j)));
end
end
function s = sigmoid_derivation(x)
s = x.*(1-x);
3、主程序,包括数据生成、训练测试数据选择、网络训练、网络测试、结果比较
function test_script
%Training Set
data = dataset_generator;
x_train = data(1:60,1:7);
y_train = data(1:60,8);
x_test = data(61:100,1:7);
y_test = data(61:100,8);
%According to result of “test_script1.m”
%Learning rate
%“alpha = 1” --------- “number of hidden neurons = 8”
%“alpha = 0.1” --------- “number of hidden neurons = 15”
alpha = 0.1;
%Initialize the network
syn0 = 2rand(7,15)-1;
syn1 = 2rand(15,1)-1;
%Training the network
for i = 1:60000
l0 = x_train;
l1 = sigmoid(l0syn0);
l2 = sigmoid(l1syn1);
l2_error = l2 - y_train;
if(i==1)
overallerror(i) = mean(abs(l2_error));
end
if(mod(i,10000)==0)
overallerror(i/10000+1) = mean(abs(l2_error));
end
l2_delta = l2_error.sigmoid_derivation(l2);
l1_error = l2_deltasyn1’;
l1_delta = l1_error.sigmoid_derivation(l1);
syn1 = syn1 - alpha(l1’l2_delta);
syn0 = syn0 - alpha(l0’*l1_delta);
end
overallerror
%Testing progress
testing_output = sigmoid(sigmoid(x_test*syn0)*syn1);
testing_output = round(testing_output);
testing_error = sum(abs(y_test - testing_output))
for cnt = 61:100
testing_output(cnt-60,2) = cnt;
end
testing_output
function s = sigmoid (x)
[m,n] = size(x);
for i = 1:m
for j = 1:n
s(i,j) = 1/(1+exp(-x(i,j)));
end
end
function s = sigmoid_derivation(x)
s = x.*(1-x);
4 输出
结果分析与后续工作:和之前简单的奇偶数判别结果不同,误差率非常大,是否因为素数的非线性特性决定?神经网络的结果怎么与数学建立联系?
下一篇: mysql数据库中的索引有那些、有什么用