问题描述
我正在尝试使用单个输出来训练MNIST数据集.这意味着当我输入28 * 28的输入(图像)时,模型会给我们一个公正的数字.例如,我给"5",模型给我的结果是4.9,5,5.002或接近5.所以我有一些文件红色.人们告诉我们,softmaxlayer必须通过回归层进行更改.为此,请执行此操作.我正在使用matconvnet库及其mnist示例.我更改了网络并编写了回归图层损失函数.这些是我的代码:
I am trying to train MNIST data set with single output. It means when i give an 28*28 input (image) the model gives us a just number. For example i give '5', the model give me as a result 4.9,5, 5.002 or close to 5. So I have red some documents. People tells softmaxlayer have to be changed with regression layer. For doing do this. I am using matconvnet library and its mnist example. I have changed my network and written regression layer loss function. these are my codes:
net.layers = {} ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(5,5,1,20, 'single'), zeros(1, 20, 'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(5,5,20,50, 'single'),zeros(1,50,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'pool', ...
'method', 'max', ...
'pool', [2 2], ...
'stride', 2, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(4,4,50,500, 'single'), zeros(1,500,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'relu') ;
net.layers{end+1} = struct('type', 'conv', ...
'weights', {{f*randn(1,1,500,1, 'single'), zeros(1,1,'single')}}, ...
'stride', 1, ...
'pad', 0) ;
net.layers{end+1} = struct('type', 'normloss');
这是回归损失函数:
function Y = vl_normloss(X,c,dzdy)
size(X)%1 1 1 100
size(c)%1 100
if nargin <= 2
Y = 0.5*sum((squeeze(X)'-c).^2);
size(Y)%1 1
Y % 1.7361e+03
else
size(Y)
Y = +((squeeze(X)'-c))*dzdy;
Y = reshape(Y,size(X));
end
我将opts.errorFunction = 'multiclass' ;
更改为'none'
我还要添加
I changed opts.errorFunction = 'multiclass' ;
to 'none'
Also i add
case 'normloss'
res(i+1).x = vl_normloss(res(i).x,l.class) ;
到vl_simplenn脚本
to vl_simplenn script
但是当我跑步训练时会发生此错误
But when i run train this error occurs
vl_simplenn中的错误(第415行) [res(i).dzdx,dzdw {1},dzdw {2}] = ...
Error in vl_simplenn (line 415) [res(i).dzdx, dzdw{1}, dzdw{2}] = ...
解决这个问题我需要做什么?谢谢
what i have to do for solving this problem? thank you
推荐答案
我找到了解决方案.我弄错了.在vl_simplenn脚本中必须更改2行,但是我只更改了一行.该代码有效.
I found the solution. I have made a mistake. There are 2 different line have to be changed in vl_simplenn script but i changed only one line. This code works.
这篇关于如何在matconvnet中通过回归更改softmaxlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!