为什么我的传递函数一直返回 'logsig'?
Why my transfer function keep turn back in to 'logsig'?
我尝试使用 patternnet 命令构建一个基本的前馈系统,该系统可以识别来自 MNIST 数据集的数据。这是我的代码
one = [1];
one = repelem(one,100);
%%%%%%%%%%%%%%%Create Neural network%%%%%%%%%%%%%%%%%%%%%
nn = patternnet([100 100]);
nn.numInputs = 1;
nn.inputs{1}.size = 784;
nn.layers{1}.transferFcn = 'logsig';
nn.layers{2}.transferFcn = 'logsig';
nn.layers{3}.transferFcn = 'softmax';
nn.trainFcn = 'trainscg';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
%%%%%%%%%%%%%%%%Dealing with data%%%%%%%%%%%%%%%%%%%%%%%%%%
mnist_in = csvread('mnist_train_100.csv');
mnist_test_in = csvread('mnist_test_10.csv');
[i,j] = size(mnist_in);
data_in = mnist_in(:,2:785);
data_in = data_in';
target_in = mnist_in(:,1);
target_in = target_in';
nn = train(nn,data_in,target_in);
问题是我在构建这个系统时输出层的传递函数设置为softmax函数。不知何故,当我训练我的系统时,传递函数变成了 'logsig' 函数,并且一直保持这种状态,直到我清除我的工作空间。我什至尝试在代码中设置输出层的传递函数,程序仍然找到一种方法将其更改为 logsig。那我有什么可以做的吗
PS。我什至尝试使用 network() 构建这个系统,使程序从 scrath 开始,仍然将我的传输函数从 softmax 改回 logsig。
如我所见,divideParam
参数有误。您将神经网络创建为 nn
,但您更改的参数属于名为 net
的变量。除此之外,创建神经网络部分正常
我认为问题出在数据准备部分。
您的训练目标 target_in
的维度为 1 x <样本数>。因此,train
函数将 'softmax' 替换为 'logsig' 以适应输出。
softmax 的输出数据应为 <结果数> x <样本数>
例如,输出为1,2或3。那么输出数组不应该是
[1 2 1 3 3 1 ...]
但应该是
[1 0 1 0 0 1 ...;
0 1 0 0 0 0 ...;
0 0 0 1 1 0 ...]
希望这对您有所帮助。
编辑:将单数组 (1 x <样本数>) 转换为多数组 (< 结果数> x < 样本数sample>), 单个数组中的数据将被索引映射。例如,单个数组中的 11 个样本:
[-1 -5.5 4 0 3.3 4 -1 0 0 0 -1]
检查所有唯一编号并排序。现在每个数字都有它的索引。
[-5.5 -1 0 3.3 4] #index table
遍历单个数组,将每个数字放在正确的索引中。基本上,-1 将具有索引 2,因此我将在第二行中出现 -1 的任何列中勾选 1。最后,
[ 0 1 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 1; #there are three -1 in the single array
0 0 0 1 0 0 0 1 1 1 0;
0 0 0 0 1 0 0 0 0 0 0;
0 0 1 0 0 1 0 0 0 0 0]
这是它的代码:
idx = sort(unique(target_in));
number_of_result = size(idx,2);
number_of_sample = size(target_in,2);
target_softmax = zeros(number_of_result,number_of_sample);
for i = 1:number_of_sample
place = find(idx == target_in(i)); % find the index of the value
target_softmax(place,i) = 1; % tick 1 at the row
end
我尝试使用 patternnet 命令构建一个基本的前馈系统,该系统可以识别来自 MNIST 数据集的数据。这是我的代码
one = [1];
one = repelem(one,100);
%%%%%%%%%%%%%%%Create Neural network%%%%%%%%%%%%%%%%%%%%%
nn = patternnet([100 100]);
nn.numInputs = 1;
nn.inputs{1}.size = 784;
nn.layers{1}.transferFcn = 'logsig';
nn.layers{2}.transferFcn = 'logsig';
nn.layers{3}.transferFcn = 'softmax';
nn.trainFcn = 'trainscg';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
%%%%%%%%%%%%%%%%Dealing with data%%%%%%%%%%%%%%%%%%%%%%%%%%
mnist_in = csvread('mnist_train_100.csv');
mnist_test_in = csvread('mnist_test_10.csv');
[i,j] = size(mnist_in);
data_in = mnist_in(:,2:785);
data_in = data_in';
target_in = mnist_in(:,1);
target_in = target_in';
nn = train(nn,data_in,target_in);
问题是我在构建这个系统时输出层的传递函数设置为softmax函数。不知何故,当我训练我的系统时,传递函数变成了 'logsig' 函数,并且一直保持这种状态,直到我清除我的工作空间。我什至尝试在代码中设置输出层的传递函数,程序仍然找到一种方法将其更改为 logsig。那我有什么可以做的吗
PS。我什至尝试使用 network() 构建这个系统,使程序从 scrath 开始,仍然将我的传输函数从 softmax 改回 logsig。
如我所见,divideParam
参数有误。您将神经网络创建为 nn
,但您更改的参数属于名为 net
的变量。除此之外,创建神经网络部分正常
我认为问题出在数据准备部分。
您的训练目标 target_in
的维度为 1 x <样本数>。因此,train
函数将 'softmax' 替换为 'logsig' 以适应输出。
softmax 的输出数据应为 <结果数> x <样本数>
例如,输出为1,2或3。那么输出数组不应该是
[1 2 1 3 3 1 ...]
但应该是
[1 0 1 0 0 1 ...;
0 1 0 0 0 0 ...;
0 0 0 1 1 0 ...]
希望这对您有所帮助。
编辑:将单数组 (1 x <样本数>) 转换为多数组 (< 结果数> x < 样本数sample>), 单个数组中的数据将被索引映射。例如,单个数组中的 11 个样本:
[-1 -5.5 4 0 3.3 4 -1 0 0 0 -1]
检查所有唯一编号并排序。现在每个数字都有它的索引。
[-5.5 -1 0 3.3 4] #index table
遍历单个数组,将每个数字放在正确的索引中。基本上,-1 将具有索引 2,因此我将在第二行中出现 -1 的任何列中勾选 1。最后,
[ 0 1 0 0 0 0 0 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 1; #there are three -1 in the single array
0 0 0 1 0 0 0 1 1 1 0;
0 0 0 0 1 0 0 0 0 0 0;
0 0 1 0 0 1 0 0 0 0 0]
这是它的代码:
idx = sort(unique(target_in));
number_of_result = size(idx,2);
number_of_sample = size(target_in,2);
target_softmax = zeros(number_of_result,number_of_sample);
for i = 1:number_of_sample
place = find(idx == target_in(i)); % find the index of the value
target_softmax(place,i) = 1; % tick 1 at the row
end