这是我写的代码,目的是训练一个机器人的力矩控制器,我之前没学过Matlab,这是按照上课最简单的一个例题照猫画虎做的,但是结果是会显示‘错误使用 network/sim (第 270 行)
Input data sizes do not match net.inputs{1}.size.’ 虽然会进行一下训练,但是我设置的epoch是5000,可每一次都是在20到40之间就停止了训练。这是为什么呢?
补充一下,因为上课老师是默认大家之前学过这个,所以没有很仔细的教,我写的就是依葫芦画瓢做的,我的想法就是一开始输入10个都是随机变量的值(题目给出变量范围),然后输入机器人的动力学公式(已知的),再设置训练参数,到保存那一步就是之前就是这个NNC完成了,然后后面是进行测试。不知道我的理解对不对。
% clear workspace
clear all
close all
clc
% Generating training patterns
number=5000;
% generating 1000 random inputs
th1d=2*pi*rand(1,number)-pi;;
th2d=10*rand(1,number)-5;
th1dotd=20*rand(1,number)-10;
th2dotd=2*pi*rand(1,number)-pi;
th1ddotd=10*rand(1,number)-5;
th2ddotd=20*rand(1,number)-10;
th1a=2*pi*rand(1,number)-pi;
th2a=10*rand(1,number)-5;
th1dota=20*rand(1,number)-10;
th2dota=2*pi*rand(1,number)-pi;
% CALCULATE 'H' PARAMETERS
m1 = 10;
m2 = 5.6;
l1 = 0.5;
l2 = 0.25;
g=9.81;
% generating the output according to the equations
cth1=cos(th1d);
sth2 = sin(th2d);
cth2 = cos(th2d);
h11=1/3*m1*l1*l1+m2*l1*l1+1/3*m2*l2*l2+m2*l1*l2*cth2;
h12=1/3*m2*l2*l2+1/2*m2*l1*l2*cth2;
h22=1/3*m2*l2*l2;
h=1/2*m2*l1*l2*sth2;
h1=-(2*h.*th1dotd.*th2dotd+h.*th2dotd.*th2dotd);
h2=h.*th1dotd.*th1dotd;
c1=(0.5*m1+m2)*g*l1*cth1+0.5*m2*g*l2*cos(th1d+th2d);
c2=0.5*m2*g*l2*cos(th1d+th2d);
torq1=h11.*th1ddotd+h12.*th2ddotd+h1+c1;
torq2=h12.*th1ddotd+h22.*th2ddotd+h2+c2;
torq=[torq1;torq2];
% set training parameters
net=feedforwardnet(10);
net.trainParam.show=50;
net.trainParam.lr=0.05;
net.trainParam.mc=0.95;
net.trainParam.epochs=5000;
net.trainParam.goal=1e-10;
net.trainParam.max_fail=20;
net=train(net,torq1,torq2);
save NNC
% Test the training results on 20 randomly generated points
th1dt=2*pi*rand(1,20)-pi;;
th2dt=10*rand(1,20)-5;
th1dotdt=20*rand(1,20)-10;
th2dotdt=2*pi*rand(1,20)-pi;
th1ddotdt=10*rand(1,20)-5;
th2ddotdt=20*rand(1,20)-10;
th1at=2*pi*rand(1,20)-pi;
th2at=10*rand(1,20)-5;
th1dotat=20*rand(1,20)-10;
th2dotat=2*pi*rand(1,20)-pi;
cth1t=cos(th1dt);
sth2t = sin(th2dt);
cth2t = cos(th2dt);
h11t=1/3*m1*l1*l1+m2*l1*l1+1/3*m2*l2*l2+m2*l1*l2*cth2t;
h12t=1/3*m2*l2*l2+1/2*m2*l1*l2*cth2t;
h22t=1/3*m2*l2*l2;
ht=1/2*m2*l1*l2*sth2t;
h1t=-(2*ht.*th1dotdt.*th2dotdt+ht.*th2dotdt.*th2dotdt);
h2t=ht.*th1dotdt.*th1dotdt;
c1t=(0.5*m1+m2)*g*l1*cth1t+0.5*m2*g*l2*cos(th1dt+th2dt);
c2t=0.5*m2*g*l2*cos(th1dt+th2dt);
torq1t=h11t.*th1ddotdt+h12t.*th2ddotdt+h1t+c1t;
torq2t=h12t.*th1ddotdt+h22t.*th2ddotdt+h2t+c2t;
torqt=[torq1t;torq2t];
a=sim(net,torqt);
用于训练神经网络的输入的大小与用于训练后模拟网络的输入的大小必须匹配。
74行改成a=sim(net,torqt');试试