如何在 MATLAB 中为神经网络提供测试样本?
How to provide the test samples to neural network in MATLAB?
在 MATLAB 经典的螃蟹分类问题中,神经网络仅从提供的样本中选择测试样本。假设,如果我向神经网络提供 30 个样本,它会随机抽取其中的 5 个样本作为测试样本。
是否可以从用户端指定测试样本?
fid = fopen('/featureValues.csv');
C = textscan(fid,'%f%f%f%f%s','delimiter',','); % Import data
fclose(fid);
%%
% The first 4 columns of data represent the image's features
% The 5th column represents the category of the image.
features = [C{1} C{2} C{3} C{4}]; % inputs to neural network
tiger = strncmpi(C{5}, 'tiger', 1);
lion = strncmpi(C{5}, 'lion', 1);
dino = strncmpi(C{5}, 'Dino', 1);
%Encoding the image categories
imCat = double([tiger lion dino]); % targets for neural network
% The next step is to preprocess the data into a form that can be used with
% a neural network.
%
% The neural network object in the toolbox expects the samples along
% columns and its features along rows. Our dataset has its samples along
% rows and its features along columns. Hence the matrices have to be
% transposed.
features = features';
imCat = imCat';
%% Building the Neural Network Classifier
% The next step is to create a neural network that will learn to identify
% the class of the images.
%
% Since the neural network starts with random initial weights, the results
% of this demo will differ slightly every time it is run. The random seed
% is set to avoid this randomness. However this is not necessary for your
% own applications.
rand('seed', 491218382)
%%
% A 1-hidden layer feed forward network is created with 20 neurons in the
% hidden layer.
%
net = newff(features,imCat,20); % Create a new feed forward network
% Now the network is ready to be trained.
[net,tr] = train(net,features,imCat);
%% Testing the Classifier
testInputs = features(:,tr.testInd);
testTargets = imCat(:,tr.testInd);
out = net(testInputs); % Get response from trained network
[y_out,I_out] = max(out);
[y_t,I_t] = max(testTargets);
N = size(testInputs,2); % Number of testing samples
fprintf('Total testing samples: %d\n', N);
Read here first where they explain how to use different data division patterns. From the question it seems that you are looking specifically for this approach 以便您可以按索引指定训练、验证和测试数据。
在 MATLAB 经典的螃蟹分类问题中,神经网络仅从提供的样本中选择测试样本。假设,如果我向神经网络提供 30 个样本,它会随机抽取其中的 5 个样本作为测试样本。
是否可以从用户端指定测试样本?
fid = fopen('/featureValues.csv');
C = textscan(fid,'%f%f%f%f%s','delimiter',','); % Import data
fclose(fid);
%%
% The first 4 columns of data represent the image's features
% The 5th column represents the category of the image.
features = [C{1} C{2} C{3} C{4}]; % inputs to neural network
tiger = strncmpi(C{5}, 'tiger', 1);
lion = strncmpi(C{5}, 'lion', 1);
dino = strncmpi(C{5}, 'Dino', 1);
%Encoding the image categories
imCat = double([tiger lion dino]); % targets for neural network
% The next step is to preprocess the data into a form that can be used with
% a neural network.
%
% The neural network object in the toolbox expects the samples along
% columns and its features along rows. Our dataset has its samples along
% rows and its features along columns. Hence the matrices have to be
% transposed.
features = features';
imCat = imCat';
%% Building the Neural Network Classifier
% The next step is to create a neural network that will learn to identify
% the class of the images.
%
% Since the neural network starts with random initial weights, the results
% of this demo will differ slightly every time it is run. The random seed
% is set to avoid this randomness. However this is not necessary for your
% own applications.
rand('seed', 491218382)
%%
% A 1-hidden layer feed forward network is created with 20 neurons in the
% hidden layer.
%
net = newff(features,imCat,20); % Create a new feed forward network
% Now the network is ready to be trained.
[net,tr] = train(net,features,imCat);
%% Testing the Classifier
testInputs = features(:,tr.testInd);
testTargets = imCat(:,tr.testInd);
out = net(testInputs); % Get response from trained network
[y_out,I_out] = max(out);
[y_t,I_t] = max(testTargets);
N = size(testInputs,2); % Number of testing samples
fprintf('Total testing samples: %d\n', N);
Read here first where they explain how to use different data division patterns. From the question it seems that you are looking specifically for this approach 以便您可以按索引指定训练、验证和测试数据。