I'm using a simple XOR input and output data set in order to train a neural network before attempting anything harder but for some reason it won't work.
may someone please explain what I am doing wrong please?
This is my code:
%user specified values
hidden_neurons = 3;
epochs = 10000;
t_input = [1 1; 1 0; 0 1; 0 0];
t_output = [1; 0; 0; 1];
te_input = [1 1; 1 0; 0 1; 0 0];
net = newff(t_input, t_output, 1);
net = init(net);
net = train(net, t_input, t_output);
net.trainParam.show = 50;
net.trainParam.lr = 0.25;
net.trainParam.epochs = epochs;
net.trainParam.goal = 1e-5;
net = train(net, t_input, t_output);
out = sim(net, te_input);
THis is my error message:
??? Error using ==> network.train at 145 Targets are incorrectly sized for
network. Matrix must have 2 columns.
Error in ==> smallNN at 11 net =
train(net, t_input, t_output);
You must have your samples on columns and not rows (like all the NN software in the world do), so change the data sets creation lines in:
t_input = [1 1; 1 0; 0 1; 0 0]';
t_output = [1; 0; 0; 1]';
te_input = [1 1; 1 0; 0 1; 0 0]';
Now it works.
Related
I seem to get the error "Warning: Matrix is singular to working precision." when trying to get delta_x. It should be using 5x1 and 5x5 matrices.
clc; close all; clear all;
phi = 1;
delta_x = 1;
error = 10e-15;
x = [ 0; 0; 0; 0; 0];
n =1;
B =0.025;
while norm(phi)>= error && norm(delta_x) >= error
G = [ 40e3 -20e3 -20e3 0 1; -20e3 20e3 0 0 0; -20e3 0 20e3 0 0; 0 0 0 0 0; 0 0 0 0 0];
fx = [ 0;
B*((-x(4)-0.7)*(x(2)-x(4))-(((x(2)-x(4))^2)/2));
B*((-x(4)-0.7)*(x(3)-x(4))-(((x(3)-x(4))^2)/2));
-B*((-x(4)-0.7)*(x(2)-x(4))-(((x(2)-x(4))^2)/2))- B*((-x(4)-0.7)*(x(3)-x(4))-(((x(3)-x(4))^2)/2));
0];
b = [ 0; 0; 0; 200e-6; 2.5];
dfx = [ 0 0 0 0 0;
0 -B*(0.7+x(2)) 0 B*(0.7+x(4)) 0;
0 0 -B*(0.7+x(3)) B*(0.7+x(4)) 0;
0 B*(0.7+x(2)) B*(0.7+x(3)) -2*B*(0.7+x(2)) 0;
0 0 0 0 0];
phi = G*x + fx - b;
m = G + dfx;
delta_x = -m\phi;
x = x+delta_x;
norm_delta_x(n) = norm(delta_x);
norm_phi(n) = norm(phi);
n = n+1;
end
The dimensions of matrices 5x1 and 5x5 are fine, but what you are doing in the step delta_x = -m\phi is solving for an inverse of matrix m. Since m is a matrix that is singular (try running det(m) and you will get a zero), an inverse matrix does not exist. Matlab sees this and notifies you by telling you "Matrix is singular to working precision".
Bottomline:
Matlab throws the errors below and it is not obvious to me what is the root cause. The problem seems to reside in the input arguments, but I cannot figure out exactly what it is. I would greatly appreciate any help in finding it.
Index exceeds matrix dimensions.
Error in eliminatevariables (line 42)
aux(model.precalc.index2) = value(model.precalc.jj2);
Error in optimizer/subsref (line 276)
[self.model,keptvariablesIndex] =
eliminatevariables(self.model,self.model.parameterIndex,thisData(:),self.model.parameterIndex);
Error in SCMv0_justrun (line 68)
[solutions,diagnostics] = controller{inputs};
Background:
I am trying to program a Model Predictive Control but I am not very familiar yet with either Yalmip or mathematical optimization algorithms. I have made sure that the defined inputs and the actual inputs have the same dimensions, hence why I am surprised that the error has to do with matrix dimensions.
The error originates in when my code calls the optimizer.
My code is based on: https://yalmip.github.io/example/standardmpc/
Here is my code (the first part of the code is only needed to define the optimization problem and is marked between "%%%%%"; the error occurs near the end):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yalmip('clear')
clear all
% Model data
A = eye(3);
B = [1 0 -1 0 0 0 0; 0 1 0 -1 0 0 0; 0 0 0 0 1 -1 -1];
nx = 3; % Number of states
nu = 7; % Number of inputs
% MPC data
Q = [10 10 20]';
R = [10 10 1 1 5 3 3]';
C = [50 0; 0 30];
N = 90;
ny = 2;
E = [0 0 0 0 0 1 0; 0 0 0 0 0 0 1];
u = sdpvar(repmat(nu,1,N),repmat(1,1,N));
x = sdpvar(repmat(nx,1,N+1),repmat(1,1,N+1));
r = sdpvar(repmat(ny,1,N+1),repmat(1,1,N+1));
d = sdpvar(ny,1);
pastu = sdpvar(nu,1);
dx = 0.05;
Gx=[-1*eye(3);eye(3)];
gx = [0 0 0 500 500 1000]';
COVd = [zeros(5,7);0 0 0 0 0 10 0; 0 0 0 0 0 0 10];
COVx = zeros(nx,nx);
auxa = eye(5);
auxb = zeros(5,2);
Gu = [-1*eye(7,7); auxa auxb;0 0 0 0 0 1 1];
gu = [zeros(7,1); 200; 200; 50; 50; 100; 500];
Ga = [0 0 0.5 0.5 -1 0 0];
constraints = [];
objective = 0;
for k = 1:N
r{k} = r{k} + d;
objective = objective + Q'*x{k} + R'*u{k} + (r{k}-E*u{k})'*C*(r{k}-E*u{k});
constraints = [constraints, x{k+1} == A*x{k}+B*u{k}];
COVx = A*COVx*A' + B*COVd*B';
COVGx = Gx*COVx*Gx';
StDevGx = sqrt(diag(COVGx));
chance = gx - norminv(1-dx/(length (gx)*N))*StDevGx;
constraints = [constraints, Ga*u{k}==0, Gu*u{k}<=gu, Gx*x{k}<=gx-chance];
end
objective = objective + Q'*x{N+1};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
parameters_in = {x{1},[r{:}],d,pastu};
solutions_out = {[u{:}], [x{:}]};
controller = optimizer(constraints, objective,[],parameters_in,solutions_out);
x = 100*ones(nx,1);
clf;
disturbance = randn(ny,1)*10;
oldu = zeros(nu,1);
hold on
for i = 1:150
future_r = [4*sin((i:i+N)/40);3*sin((i:i+N)/20)];% match dimensions of r
inputs = {x,future_r,disturbance,oldu};
[solutions,diagnostics] = controller{inputs};
U = solutions{1};oldu = U(1);
X = solutions{2};
if diagnostics == 1
error('The problem is infeasible');
end
x = A*x+B*u;
end
It's a bug in the latest version of YALMIP.
I've a neural network in MATLAB that works well, but it should give me a result of 0 or 1, and it gets +/-0.05 error. (0.02, 0.97, 1.03, ...)
What should I change for the result to be more accurate?
p = [ 0 0 1 1; 0 1 0 1];
t = [0 1 1 0];
net = feedforwardnet(2,'trainlm');
net.trainParam.goal = 0.01*var(t',1);
net.divideFcn = 'dividetrain';
[net,tr] = train(net,p,t);
a = net(p)
For example my vector is
a = [0 1]
I want to add number 0 into vector but in FOR path 3 times. I want to get this vector
a = [0 1 0 0 0]
You don't need a loop to do this. This can be accomplished using concatenation and the zeros function.
nzeros = 3;
a = [0 1];
a = cat(2, a, zeros(1, nzeros));
% or a = [a zeros(1, nzeros)];
Alternatively:
nzeros = 3;
a = [0 1];
a(end+nzeros) = 0;
If you are talking about a for-loop. This will do what you asked for
a = [0 1];
for i=1:3
a = [a,0];
end
I'm trying to plot a frequency characteristic equation using ezplot, but Matlab gives the following warning, "Contour not rendered for non-finite ZData". I have used this command to plot frequency equations previously but now I get a warning and the plot display is empty and it does not change the axis range as well. Can someone please help. Would be much appreciated.
Here's the code i'm using.
% Transfer Matrix for Case-I, thin rotor
clear all;
clc;
EI = 1626;
l = 0.15;
m = 0.44108;
It = 2.178*10^-4;
I_p = 2.205*10^-5;
Itr = 0.24;
I_pr = 0.479;
syms p n;
F = [1 l*1i l^2/(2*EI)*1i l^3/(6*EI);
0 1 l/EI -l^2/(2*EI)*1i;
0 0 1 -l*1i;
0 0 0 l];
P = [ 1 0 0 0;
0 1 0 0;
0 -It*p^2+I_p*n*p 1 0;
-m*p^2 0 0 1];
P_r = [1 0 0 0;
0 1 0 0;
0 -Itr*p^2+I_pr*n*p 1 0;
-m*p^2 0 0 1];
A = F*P*F*P*F*P*F;
B = P_r*F*P*F*P*F;
r = A(1,2)/A(1,4);
a12_p = 0;
a22_p = A(2,2)-r*A(2,4);
a32_p = A(3,2)-r*A(3,4);
a42_p = A(4,2)-r*A(4,4);
Ap(2,2) = a22_p;
Ap(3,2) = a32_p;
Ap(4,2) = a42_p;
Ap(4,4) = 1;
C = B*Ap;
M = [C(3,2) C(3,4);
C(4,2) C(4,4)];
sol = det(M);
ezplot(sol,[-2*10^10 2*10^10]);
The sol is displayed if u ask for it but the plot doesn't display.
Thanks in advance for ur help !! Much appreciated.