-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainDesignModel.jl
401 lines (339 loc) · 12.7 KB
/
trainDesignModel.jl
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#####################
## Dependencies
#####################
using Dates
using Random
using Plots
using StatsBase
using DataFrames
using JLD2
using BSON
using CUDA
using MLDataUtils
using Flux
using Flux: @epochs
using Logging
using Printf: @printf
using NumericIO
using Chain
######################
## Setup
######################
# File System
paramName = "L-vdsat";
deviceName = "ptmp45";
deviceType = :p;
timeStamp = string(Dates.now());
modelDir = "./model/$(paramName)-$(deviceName)-$(timeStamp)";
dataDir = "../data";
mosFile = "$(dataDir)/$(deviceName).jld";
modelFile = "$(modelDir)/$(paramName)-$(deviceName).bson";
Base.Filesystem.mkdir(modelDir);
# Don't allow scalar operations on GPU
CUDA.allowscalar(false);
# Set Plot Backend
#unicodeplots();
inspectdr();
# The Seed of RNGesus
#rngSeed = deviceType == :n ? 666 : 999;
rngSeed = 666;
######################
## Data
######################
# Box-Cox ↔ Cox-Box Transformation
boxCox(yᵢ; λ = 0.2) = λ != 0 ? (((yᵢ.^λ) .- 1) ./ λ) : log.(yᵢ);
coxBox(y′; λ = 0.2) = λ != 0 ? exp.(log.((λ .* y′) .+ 1) / λ) : exp.(y′);
# Handle JLD2 dataframe
dataFrame = jldopen((f) -> f["database"], mosFile, "r");
# Processing, Fitlering, Sampling and Shuffling
dataFrame.QVgs = dataFrame.Vgs.^2.0;
dataFrame.EVds = ℯ.^(dataFrame.Vds);
dataFrame.RVbs = abs.(dataFrame.Vbs).^0.5;
dataFrame.gmid = dataFrame.gm ./ dataFrame.id;
dataFrame.A0 = dataFrame.gm ./ dataFrame.gds;
dataFrame.Jd = dataFrame.id ./ dataFrame.W;
dataFrame.Veg = dataFrame.Vgs .- dataFrame.vth;
dataFrame.Lgm = log10.(dataFrame.gm);
dataFrame.Lgds = log10.(dataFrame.gds);
dataFrame.Lid = log10.(dataFrame.id);
mask = @chain dataFrame begin
(isinf.(_) .| isnan.(_))
Matrix(_)
sum(_ ; dims = 2)
(_ .== 0)
vec()
collect()
end;
mdf = dataFrame[mask, : ];
### GOOD STUFF (L,vdsat) #####
paramsX = [ "L", "vdsat", "id", "Vds", "Vbs" ];
paramsY = [ "fug", "gmid", "gm", "gds", "Vgs", "Jd", "vth" ];
maskBCX = paramsX .∈([ "id" ],);
maskBCY = paramsY .∈([ "fug", "gmid", "gm", "gds", "Jd" ],);
###################
### GOOD STUFF (L,gmid) #####
#paramsX = [ "L", "gmid", "id", "Vds", "Vbs" ];
#paramsY = [ "fug", "vdsat", "gm", "gds", "Vgs", "Jd", "vth" ];
#maskBCX = paramsX .∈([ "gmid", "id" ],);
#maskBCY = paramsY .∈([ "fug", "gm", "gds", "Jd" ],);
###################
### GOOD STUFF (fug,vdsat) #####
#paramsX = [ "fug", "vdsat", "id", "Vds", "Vbs" ];
#paramsY = [ "L", "gmid", "gm", "gds", "Vgs", "Jd", "vth" ];
#maskBCX = paramsX .∈([ "fug", "id" ],);
#maskBCY = paramsY .∈([ "gm", "gmid", "gds", "Jd" ],);
###################
### GOOD STUFF (fug,gmid) #####
#paramsX = [ "fug", "gmid", "id", "Vds", "Vbs" ];
#paramsY = [ "L", "vdsat", "gm", "gds", "Vgs", "Jd", "vth" ];
#maskBCX = paramsX .∈([ "fug", "gmid", "id" ],);
#maskBCY = paramsY .∈([ "gm", "gds", "Jd" ],);
###################
# Number of In- and Outputs, for respective NN Layers
numX = length(paramsX);
numY = length(paramsY);
## Sample Data for appropriate distribution
# Random Uniform with Probability Weights on gds
#df = mdf[ StatsBase.sample( MersenneTwister(rngSeed)
# , 1:size(mdf, 1)
# , StatsBase.pweights(mdf.gds)
# , 4000000
# ; replace = false
# , ordered = false )
# , : ];
# When VGS > Vth and VDS ≥ (VGS – Vth):
satMask = ((mdf.Vds .>= (mdf.Vgs .- mdf.vth)) .& (mdf.Vgs .> mdf.vth));
# Sample 3/4ths of Data in Saturation Region with probability weighted Id
#sdf = mdf[ ifelse( deviceType == :n
# , mdf.Vds .>= (mdf.Vgs .- mdf.vth)
# , mdf.Vds .<= (mdf.Vgs .+ mdf.vth) )
# , :];
sdf = mdf[satMask, : ];
sSamp = sdf[ StatsBase.sample( MersenneTwister(rngSeed)
, 1:size(sdf, 1)
, StatsBase.pweights(sdf.gds)
, 3000000
; replace = false
, ordered = false )
, : ];
# Sample 1/3rd of Data in Triode Region without weights
#tdf = mdf[ ifelse( deviceType == :n
# , mdf.Vds .<= (mdf.Vgs .- mdf.vth)
# , mdf.Vds .>= (mdf.Vgs .+ mdf.vth) )
# , :];
#tdf = mdf[((mdf.Vds .>= (mdf.Vgs .- mdf.vth)) .& (mdf.Vgs .> mdf.vth)), : ];
tdf = mdf[.!satMask, : ];
tSamp = tdf[ StatsBase.sample( MersenneTwister(rngSeed)
, 1:size(tdf, 1)
, StatsBase.pweights(tdf.gds)
, 1000000
; replace = false
, ordered = false )
, : ];
# Join samples and shuffle all observations
df = shuffleobs(vcat(tSamp, sSamp));
# Convert to Matrix for Flux
rawX = Matrix(df[ : , paramsX ])';
rawY = Matrix(df[ : , paramsY ])';
# Transform according to mask
λ = 0.2;
rawX[maskBCX,:] = boxCox.(abs.(rawX[maskBCX,:]); λ = λ);
rawY[maskBCY,:] = boxCox.(abs.(rawY[maskBCY,:]); λ = λ);
# Rescale data to [0;1]
utX = StatsBase.fit(UnitRangeTransform, rawX; dims = 2, unit = true);
utY = StatsBase.fit(UnitRangeTransform, rawY; dims = 2, unit = true);
dataX = StatsBase.transform(utX, rawX);
dataY = StatsBase.transform(utY, rawY);
# Split data in training and validation set
splitRatio = 0.8;
trainX,validX = splitobs(dataX, splitRatio);
trainY,validY = splitobs(dataY, splitRatio);
# Create training and validation Batches
batchSize = 2000;
trainSet = Flux.Data.DataLoader( (trainX, trainY)
, batchsize = batchSize
, shuffle = true );
validSet = Flux.Data.DataLoader( (validX, validY)
, batchsize = batchSize
, shuffle = true );
######################
## Model
######################
# Neural Network Architecture
γ = Flux.Chain( Flux.Dense(numX, 128, Flux.relu)
, Flux.Dense(128, 256, Flux.relu)
, Flux.Dense(256, 512, Flux.relu)
, Flux.Dense(512, 1024, Flux.relu)
, Flux.Dense(1024, 512, Flux.relu)
, Flux.Dense(512, 256, Flux.relu)
, Flux.Dense(256, 128, Flux.relu)
, Flux.Dense(128, numY, Flux.relu)
) |> gpu;
# Optimizer Parameters
η = 0.001;
β₁ = 0.9;
β₂ = 0.999;
# ADAM Optimizer
optim = Flux.Optimise.ADAM(η, (β₁, β₂)) |> gpu;
######################
## Training
######################
# Loss/Objective/Cost function for Training and Validation
mse(x, y) = Flux.Losses.mse(γ(x), y, agg = mean);
mae(x, y) = Flux.Losses.mae(γ(x), y, agg = mean);
hub(x, y) = Flux.Losses.huber_loss(γ(x), y, δ = 1, agg = mean);
# Model Parameters (Weights)
θ = Flux.params(γ) |> gpu;
# Training Loop
function trainModel()
trainMSE = map(trainSet) do batch # iterate over batches in train set
gpuBatch = batch |> gpu; # make sure batch is on GPU
error,back = Flux.Zygote.pullback(() -> mse(gpuBatch...), θ);
∇ = back(one(error |> cpu)) |> gpu; # gradient based on error
Flux.update!(optim, θ, ∇); # update weights
return error; # return MSE
end;
validMAE = map(validSet) do batch # no gradients required
gpuBatch = batch |> gpu;
error,back = Flux.Zygote.pullback(() -> mae(gpuBatch...), θ);
return error; # iterate over validation data set
end;
meanMSE = mean(trainMSE); # get mean training error over epoch
meanMAE = mean(validMAE); # get mean validation error over epoch
@printf( "[%s] MSE = %s and MAE = %s\n"
, Dates.format(now(), "HH:MM:SS")
, formatted(meanMSE, :ENG, ndigits = 4)
, formatted(meanMAE, :ENG, ndigits = 4) )
if meanMAE < lowestMAE # if model has improved
bson( modelFile # save the current model (cpu)
, name = deviceName
, type = deviceType
, parameter = paramName
, model = (γ |> cpu)
, paramsX = paramsX
, paramsY = paramsY
, utX = utX
, utY = utY
, maskX = maskBCX
, maskY = maskBCY
, lambda = λ );
global lowestMAE = meanMAE; # update previous lowest MAE
@printf( "\tNew Model Saved with MAE: %s\n"
, formatted(meanMAE, :ENG, ndigits = 4) )
end
return [meanMSE meanMAE]; # mean of error for all batches
end
### Run Training
numEpochs = 50; # total number of epochs
lowestMAE = Inf; # initialize MAE with ∞
errs = []; # Array of training and validation losses
@epochs numEpochs push!(errs, trainModel()) # Run Training Loop for #epochs
######################
## Evaluation
######################
# Reshape errors for a nice plot
losses = hcat( map((e) -> Float64(e[1]), errs)
, map((e) -> Float64(e[2]), errs) );
# Plot Training Process
plot( 1:numEpochs, losses; lab = ["MSE" "MAE"]
, xaxis = ("# Epoch", (1,numEpochs))
, yaxis = ("Error", (0.0, ceil( max(losses...)
, digits = 3 )) )
, w = 2 )
## Use Current model ##
#γ = γ |> cpu;
## Load specific model ##
#modelFile = "./model/vdsat-ptmn90-2021-02-22T14:14:31.445/ptmn90.bson"
model = BSON.load(modelFile);
γ = model[:model];
paramsX = model[:paramsX];
paramsY = model[:paramsY];
utX = model[:utX];
utY = model[:utY];
maskBCX = model[:maskX];
maskBCY = model[:maskY];
λ = model[:lambda];
param = model[:parameter];
device = model[:name];
## Reload DB ##########
#dataFrame = jldopen("../data/$(device).jld", "r") do file
# file["database"];
#end;
#dataFrame.QVgs = dataFrame.Vgs.^2.0;
#dataFrame.EVds = exp.(dataFrame.Vds);
#dataFrame.RVbs = sqrt.(abs.(dataFrame.Vbs));
#dataFrame.gmid = dataFrame.gm ./ dataFrame.id;
#dataFrame.A0 = dataFrame.gm ./ dataFrame.gds;
#dataFrame.Jd = dataFrame.id ./ dataFrame.W;
#msk = @chain dataFrame begin
# (isinf.(_) .| isnan.(_))
# Matrix(_)
# sum(_ ; dims = 2)
# (_ .== 0)
# vec()
# collect()
# end;
#mdf = dataFrame[msk, : ];
#boxCox(yᵢ; λ = 0.2) = λ != 0 ? (((yᵢ.^λ) .- 1) ./ λ) : log.(yᵢ);
#coxBox(y′; λ = 0.2) = λ != 0 ? exp.(log.((λ .* y′) .+ 1) / λ) : exp.(y′);
#inspectdr();
########################
mdf.Vgs = round.(mdf.Vgs, digits = 2);
mdf.Vds = round.(mdf.Vds, digits = 2);
mdf.Vbs = round.(mdf.Vbs, digits = 2);
### γ evaluation function for prediction characteristics
function predict(X)
X[maskBCX,:] = boxCox.(abs.(X[maskBCX,:]); λ = λ);
X′ = StatsBase.transform(utX, X);
Y′ = γ(X′);
Y = StatsBase.reconstruct(utY, Y′);
Y[maskBCY,:] = coxBox.(Y[maskBCY,:]; λ = λ);
return DataFrame(Float64.(Y'), String.(paramsY))
end;
L = rand(filter(l -> l < 1.0e-6, unique(mdf.L)));
W = rand(filter(w -> w < 2.0e-6, unique(mdf.W)));
fugs = exp10.(range(8, stop = 10, length = 10));
Id = 10e-6;
Vbs = 0.0;
Vds = 0.6;
param1,param2 = split(param, "-");
traceT = sort( mdf[ ( (mdf[:,"L"] .== L)
.& (mdf[:,"W"] .== W)
.& (mdf[:,"Vbs"] .== Vbs)
.& (mdf[:,"Vds"] .== Vds) )
, : ]
, [param2] );
len = length(traceT[:,param2]);
p1 = traceT[:,param1];
p2 = traceT[:,param2];
#id = fill(Id, len);
id = traceT.id;
lid = log10.(id);
vds = traceT[:,"Vds"];
evds = exp.(vds);
vbs = traceT[:,"Vbs"];
rvbs = sqrt.(abs.(vbs));
x = [ p1 p2 id vds vbs ]';
y = predict(x);
plot( p2, y.gds; yscale = :log10
, lab = "Aprx", w = 2, xaxis = param);
plot!(p2, traceT.gds; yscale = :log10, lab = "True", w = 2)
plot( para, y.A0; yscale = :log10
, lab = "Aprx", w = 2, xaxis = param, yaxis = "gds" );
#plot!(para, y.gm ./ y.gds; lab = "Aprx", w = 2)
plot!(para, traceT.A0; lab = "True", w = 2)
#plot!(para, traceT.gm ./ traceT.gds; lab = "True", w = 2)
plot(para, y.gm; lab = "Aprx", w = 2);
plot!(para, traceT.gm; lab = "True", w = 2)
plot(para, y.gds; lab = "Aprx", w = 2);
plot!(para, traceT.gds; lab = "True", w = 2)
#plot!(para, y.gm ./ y.gds; lab = "Aprx", w = 2);
#plot!(para, traceT.gm ./ traceT.gds; lab = "True", w = 2);
#plot!(para, y.A0; lab = "Aprx", w = 2);
#plot!(para, traceT.A0; lab = "True", w = 2)
traceO = sort( mdf[ ( (mdf[:,"L"] .== L)
.& (mdf[:,"W"] .== W)
.& (mdf[:,"Vbs"] .== Vbs)
.& (mdf[:,"Vgs"] .== 0.6) )
, : ]
, ["Vds" ] );