-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrainRPOS.m
More file actions
172 lines (144 loc) · 5.4 KB
/
trainRPOS.m
File metadata and controls
172 lines (144 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function varargout = trainRPOS(u,y,hiddenSize,trainFcn,trainPar)
%TRAINRPOS train a RPOS neural network
%
% [net, Temp, netPar, gpos,fun] = TRAINRPOS(u,y,hiddenSize,trainFcn,trainPar) takes
% u: MxN input vector,
% y: N output vector,
% hiddenSize: number of nodes in the hidden layer,
% trainFcn: training function,
% trainPar: training parameters,
% and returns
% net: a trained RPOS neural network
% Temp: the temperature parameter of the network
% netPar: the parameters of the network
% gpos: the generalized posynomial
% fun: the functions generating the RPOS
%
% Defaults are used if TRAINRPOS is called with fewer argument:
% hiddenSize = 10
% trainFcn = 'trainlm'
switch nargin
case 0
error 'the function requires the input and output vectors'
case 1
error 'the function requires the input and output vectors'
case 2
if ~isempty(find(u <= 0, 1))
error 'the inputs must be positive'
end
if ~isempty(find(y <= 0, 1))
error 'the outputs must be positive'
end
ut = log(u);
yt = log(y);
hiddenSize = 10;
net = dlsenet(ut, yt);
case 3
if ~isempty(find(u <= 0, 1))
error 'the inputs must be positive'
end
if ~isempty(find(y <= 0, 1))
error 'the outputs must be positive'
end
ut = log(u);
yt = log(y);
net = dlsenet(ut, yt, hiddenSize);
case 4
if ~isempty(find(u <= 0, 1))
error 'the inputs must be positive'
end
if ~isempty(find(y <= 0, 1))
error 'the outputs must be positive'
end
ut = log(u);
yt = log(y);
net = dlsenet(ut, yt, hiddenSize,trainFcn);
case 5
if ~isempty(find(u <= 0, 1))
error 'the inputs must be positive'
end
if ~isempty(find(y <= 0, 1))
error 'the outputs must be positive'
end
ut = log(u);
yt = log(y);
net = dlsenet(ut, yt, hiddenSize,trainFcn);
net.trainParam = trainPar;
end
[net,~,~,~] = train(net, ut, yt);
switch nargout
case 1
varargout{1} = net;
case 2
varargout{1} = net;
Temp = 1/net.outputs{3}.processSettings{1}.gain;
varargout{2} = Temp;
case 3
varargout{1} = net;
Temp = 1/net.outputs{3}.processSettings{1}.gain;
varargout{2} = Temp;
Alpha = net.IW{1,1};
Beta = net.b{1};
uoff = net.inputs{1}.processSettings{1}.xoffset;
ugain = net.inputs{1}.processSettings{1}.gain;
umin = net.inputs{1}.processSettings{1}.ymin;
yoff = net.outputs{3}.processSettings{1}.xoffset;
ygain = net.outputs{3}.processSettings{1}.gain;
ymin = net.outputs{3}.processSettings{1}.ymin;
Alpha1 = Alpha(1:hiddenSize,:);
Alpha2 = Alpha(1+hiddenSize:end,:);
Beta1 = Beta(1:hiddenSize,:);
Beta2 = Beta(1+hiddenSize:end,:);
netPar = struct('Alpha1',Alpha1,'Alpha2',Alpha2,'Beta1',Beta1,...
'Beta2',Beta2,'uoff',uoff,'ugain',ugain,'umin',umin,...
'yoff',yoff,'ygain',ygain,'ymin',ymin);
varargout{3} = netPar;
case 4
varargout{1} = net;
Temp = 1/net.outputs{3}.processSettings{1}.gain;
varargout{2} = Temp;
Alpha = net.IW{1,1};
Beta = net.b{1};
uoff = net.inputs{1}.processSettings{1}.xoffset;
ugain = net.inputs{1}.processSettings{1}.gain;
umin = net.inputs{1}.processSettings{1}.ymin;
yoff = net.outputs{3}.processSettings{1}.xoffset;
ygain = net.outputs{3}.processSettings{1}.gain;
ymin = net.outputs{3}.processSettings{1}.ymin;
Alpha1 = Alpha(1:hiddenSize,:);
Alpha2 = Alpha(1+hiddenSize:end,:);
Beta1 = Beta(1:hiddenSize,:);
Beta2 = Beta(1+hiddenSize:end,:);
netPar = struct('Alpha1',Alpha1,'Alpha2',Alpha2,'Beta1',Beta1,...
'Beta2',Beta2,'uoff',uoff,'ugain',ugain,'umin',umin,...
'yoff',yoff,'ygain',ygain,'ymin',ymin);
varargout{3} = netPar;
varargout{4} = @(x) exp(net(log(x)));
case 5
varargout{1} = net;
Temp = 1/net.outputs{3}.processSettings{1}.gain;
varargout{2} = Temp;
Alpha = net.IW{1,1};
Beta = net.b{1};
uoff = net.inputs{1}.processSettings{1}.xoffset;
ugain = net.inputs{1}.processSettings{1}.gain;
umin = net.inputs{1}.processSettings{1}.ymin;
yoff = net.outputs{3}.processSettings{1}.xoffset;
ygain = net.outputs{3}.processSettings{1}.gain;
ymin = net.outputs{3}.processSettings{1}.ymin;
Alpha1 = Alpha(1:hiddenSize,:);
Alpha2 = Alpha(1+hiddenSize:end,:);
Beta1 = Beta(1:hiddenSize,:);
Beta2 = Beta(1+hiddenSize:end,:);
netPar = struct('Alpha1',Alpha1,'Alpha2',Alpha2,'Beta1',Beta1,...
'Beta2',Beta2,'uoff',uoff,'ugain',ugain,'umin',umin,...
'yoff',yoff,'ygain',ygain,'ymin',ymin);
varargout{3} = netPar;
varargout{4} = @(x) exp(net(log(x)));
Dugain = diag(ugain);
num = @(x) exp(yoff - ymin/ygain)*(sum(exp(Alpha1*(Dugain*(log(x) - uoff) + umin) + Beta1),1)).^(1/ygain);
den = @(x) (sum(exp(Alpha2*(Dugain*(log(x) - uoff) + umin) + Beta2),1)).^(1/ygain);
functs = struct('num',num,'den',den);
varargout{5} = functs;
end
end