How to format the inputs and targets for a feedforward neural network?

조회 수: 6 (최근 30일)
Stefan Holst-Roness
Stefan Holst-Roness 2021년 3월 1일
답변: Swetha Polemoni 2021년 3월 29일
I have to create a feedforward neural netweok in order to classify some signal data in matlab. Below is the dataset I have been given. I'm not sure what parameters from this dataset I should use as my inputs and also how to create the targets from this as well. I am using MatLab 2020a and the deep learning toolbox.
rng(2);
sample_rate = 6700; % Samples measured per second
T = 1; % Time in seconds
num_classes = 3;
tt = 0:1/sample_rate:T-1/sample_rate;
num_samples = length(tt);
m = 1;
k = 400:50:90000;
num_freq = length(k);
A = randperm(num_freq) / num_freq;
phi = 0;
% Generate U1ndamped (c = 0)
U1 = zeros([num_samples, 1, 1, num_freq]);
c = 0;
gamma = c / (2 * m);
omega0 = sqrt(k / m);
omega1 = sqrt(omega0.^2 - gamma^2);
for i = 1:num_freq
U1(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1(i) * tt - phi) + 0.01*randn(1,num_samples);
end
figure;
plot(tt,U1(:,:,:,1));
hold on;
plot(tt,U1(:,:,:,end));
% Generate D1amped
D1 = zeros([num_samples, 1, 1, num_freq]);
for i = 1:num_freq
maxx_c = 2*sqrt(k(i));
c = 2*sqrt(k(i))- maxx_c + 10;
gamma = c / (2 * m);
omega0 = sqrt(k(i) / m);
omega1 = sqrt(omega0.^2 - gamma^2);
D1(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1 * tt - phi) + 0.01*randn(1,num_samples);
end
figure;
plot(tt,D1(:,:,:,1));
hold on;
plot(tt,D1(:,:,:,end));
% Generate C1ritically D1amped
C1 = zeros([num_samples, 1, 1, num_freq]);
for i = 1:num_freq
maxx_c = 2*sqrt(k(i));
c = maxx_c;
gamma = c / (2 * m);
omega0 = sqrt(k(i) / m);
omega1 = sqrt(omega0.^2 - gamma^2);
C1(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1 * tt - phi) + 0.01*randn(1,num_samples);
end
figure;
plot(tt,C1(:,:,:,1));
hold on;
plot(tt,C1(:,:,:,end));
% Generate U1ndamped (c = 0)
U2 = zeros([num_samples, 1, 1, num_freq]);
c = 0;
gamma = c / (2 * m);
omega0 = sqrt(k / m);
omega1 = sqrt(omega0.^2 - gamma^2);
for i = 1:num_freq
U2(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1(i) * tt - phi) + 0.05*randn(1,num_samples);
end
figure;
plot(tt,U2(:,:,:,1));
hold on;
plot(tt,U2(:,:,:,end));
% Generate D1amped
D2 = zeros([num_samples, 1, 1, num_freq]);
for i = 1:num_freq
maxx_c = 2*sqrt(k(i));
c = 2*sqrt(k(i))- maxx_c + 10;
gamma = c / (2 * m);
omega0 = sqrt(k(i) / m);
omega1 = sqrt(omega0.^2 - gamma^2);
D2(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1 * tt - phi) + 0.05*randn(1,num_samples);
end
figure;
plot(tt,D2(:,:,:,1));
hold on;
plot(tt,D2(:,:,:,end));
% Generate C1ritically D1amped
C2 = zeros([num_samples, 1, 1, num_freq]);
for i = 1:num_freq
maxx_c = 2*sqrt(k(i));
c = maxx_c;
gamma = c / (2 * m);
omega0 = sqrt(k(i) / m);
omega1 = sqrt(omega0.^2 - gamma^2);
C2(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1 * tt - phi) + 0.05*randn(1,num_samples);
end
figure;
plot(tt,C2(:,:,:,1));
hold on;
plot(tt,C2(:,:,:,end));
% Generate U1ndamped (c = 0)
U3 = zeros([num_samples, 1, 1, num_freq]);
c = 0;
gamma = c / (2 * m);
omega0 = sqrt(k / m);
omega1 = sqrt(omega0.^2 - gamma^2);
for i = 1:num_freq
U3(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1(i) * tt - phi) + 0.1*randn(1,num_samples);
end
figure;
plot(tt,U3(:,:,:,1));
hold on;
plot(tt,U3(:,:,:,end));
% Generate D1amped
D3 = zeros([num_samples, 1, 1, num_freq]);
for i = 1:num_freq
maxx_c = 2*sqrt(k(i));
c = 2*sqrt(k(i))- maxx_c + 10;
gamma = c / (2 * m);
omega0 = sqrt(k(i) / m);
omega1 = sqrt(omega0.^2 - gamma^2);
D3(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1 * tt - phi) + 0.1*randn(1,num_samples);
end
figure;
plot(tt,D3(:,:,:,1));
hold on;
plot(tt,D3(:,:,:,end));
% Generate C1ritically D1amped
C3 = zeros([num_samples, 1, 1, num_freq]);
for i = 1:num_freq
maxx_c = 2*sqrt(k(i));
c = maxx_c;
gamma = c / (2 * m);
omega0 = sqrt(k(i) / m);
omega1 = sqrt(omega0.^2 - gamma^2);
C3(:,:,:,i) = A(i) * exp(-1 * gamma * tt) .* cos(omega1 * tt - phi) + 0.1*randn(1,num_samples);
end
figure;
plot(tt,C3(:,:,:,1));
hold on;
plot(tt,C3(:,:,:,end));
% C1ombine D1ata
x_data = cat(4,U1,U2,U3,D1,D2,D3,C1,C2,C3);
y_data = [zeros(1, 3*length(k)) ones(1,3*length(k)) 2*ones(1,3*length(k))];
y_data = categorical(y_data);
p = randperm(num_classes*3*num_freq);
x = x_data(:,:,:,p);
y = y_data(p);
figure;
hist(y);
N = length(y);
train_split = round(0.8 * N, 0);
test_split = round(0.1 * N, 0);
x_train = x(:,:,:,1:train_split);
x_val = x(:,:,:,train_split:train_split+test_split);
x_test = x(:,:,:,train_split+test_split:end);
y_train = y(1:train_split);
y_val = y(train_split:train_split+test_split);
y_test = y(train_split+test_split:end);
figure;
subplot(1,3,1)
hist(y_train);
xlabel('Train');
subplot(1,3,2)
hist(y_val);
xlabel('Val');
subplot(1,3,3)
hist(y_test);
xlabel('Test');
save('ff_cnn_data.mat', 'x_train', 'y_train', 'x_val', 'y_val', 'x_test', 'y_test');

답변 (1개)

Swetha Polemoni
Swetha Polemoni 2021년 3월 29일
Hi
It is my understanding that you are doing classification. As it is clearly mentioned in the code x_train and y_tran are for traning i.e., x_train is input to the network and y_train values are target values. You may find the following link helpful for classification in Matlab.

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by