深度学习DBN预测/regression

        最近在做关于DBN的数据预测问题,发现网上没有关于这类的程序,所以决定做这个博客加以补充。

在执行以下代码之前,需要植入深度学习工具箱。关于如何植入工具箱可以参看其他博客,这里就不再说了。



clc;clear
%% 产生数据
% Some invented nonlinear relationships to get 3 noisy output targets
% 1000 records with 10 features
all_x = randn(20000, 10);
all_y = randn(20000, 3) * 0.01;
all_y(:,1) = all_y(:,1) + sum( all_x(:,1:5) .* all_x(:, 3:7), 2 );
all_y(:,2) = all_y(:,2) +  sum( all_x(:,5:9) .* all_x(:, 4:8) .* all_x(:, 2:6), 2 );
all_y(:,3) = all_y(:,3) +  log( sum( all_x(:,4:8) .* all_x(:,4:8), 2 ) ) * 3.0;
% input_data 20000*10 output_data 20000*3
% 将前面19000个样本作为训练数据,后面1000个样本作为测试数据
train_x = all_x(1:19000,:);
train_y = all_y(1:19000,:);

test_x = all_x(19001:20000,:);
test_y = all_y(19001:20000,:);

% the constructed data is already normalized, but this is usually best practice:
[train_x, mu, sigma] = zscore(train_x);
test_x = normalize(test_x, mu, sigma);

%% network setup
% 输入的是10维数据,所以为10,后面的DBN层数可以自己设置
dbn.sizes = [10 100 100];
opts.numepochs =   10;
opts.batchsize = 100;
opts.momentum  =   0;
opts.alpha     =   1;
dbn = dbnsetup(dbn, train_x, opts);
dbn = dbntrain(dbn, train_x, opts);

%unfold dbn to nn
% 将DBN网络转化为NN网络,这里参数为3是因为输出维数为3
nn = dbnunfoldtonn(dbn, 3);

nn.activation_function = 'tanh_opt';    %  tanh_opt activation function
nn.output              = 'linear';      %  linear is usual choice for regression problems
nn.learningRate        = 0.001;         %  Linear output can be sensitive to learning rate
nn.momentum            = 0.95;

opts.numepochs =  20;   %  Number of full sweeps through data
opts.batchsize = 100;   %  Take a mean gradient step over this many samples
[nn, L] = nntrain(nn, train_x, train_y, opts);

% nnoutput calculates the predicted regression values
predictions = nnoutput( nn, test_x );%预测结果

[er, bad] = nntest(nn, test_x, test_y);%误差计算
assert(er < 1.5, 'Too big error');








猜你喜欢

转载自blog.csdn.net/qq_18124075/article/details/80914065
今日推荐