-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainDeviceModel.jl
370 lines (315 loc) · 11.3 KB
/
trainDeviceModel.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
#####################
## Dependencies
#####################
using Dates
using Random
using Statistics
using Plots
using StatsBase
using Distributions
using DataFrames
using JLD2
using BSON
using CUDA
using MLDataUtils
using Zygote
using Flux
using Flux: @epochs
using Logging
using Printf: @printf
using NumericIO
using Chain
#using Lazy
#using OhMyREPL
######################
## Setup
######################
# File System
timeStamp = string(Dates.now());
dataDir = "../data";
deviceType = :p;
deviceName = "ptmp45";
modelDir = "./model/op-$(deviceName)-$(timeStamp)";
mosFile = "$(dataDir)/$(deviceName).jld";
modelFile = "$(modelDir)/$(deviceName).bson";
Base.Filesystem.mkdir(modelDir);
# Don't allow scalar operations on GPU
CUDA.allowscalar(false);
# Set Plot Backend
#unicodeplots();
inspectdr();
# RNGesus Seed
rngSeed = 666;
######################
## Data
######################
# 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;
# Define Features and Outputs
paramsX = ["W", "L", "Vgs", "QVgs", "Vds", "EVds", "Vbs", "RVbs"];
paramsY = [ "vth", "vdsat", "id", "gm", "gmb", "gds", "fug"
, "cgd", "cgb", "cgs", "cds", "csb", "cdb" ];
# Box-Cox Transformation Mask
maskBCX = paramsX .∈([],);
maskBCY = paramsY .∈([ "id", "gm", "gmb", "gds", "fug"
, "cgd", "cgb", "cgs", "cds", "csb", "cdb" ],);
# Number of In- and Outputs, for respective NN Layers
numX = length(paramsX);
numY = length(paramsY);
## Sample Data for appropriate distribution
# Single weighted Sample
#idxSamples = StatsBase.sample( MersenneTwister(rngSeed)
# , 1:size(dataFrame, 1)
# , StatsBase.pweights(dataFrame.id)
# , 666666
# ; replace = false
# , ordered = false );
#df = dataFrame[idxSamples, : ];
# Sample 3/4ths of Data in Saturation Region with probability weighted Id
sdf = dataFrame[ ifelse( deviceType == :n
, (dataFrame.Vds .>= (dataFrame.Vgs .- dataFrame.vth))
, (dataFrame.Vds .<= (dataFrame.Vgs .+ dataFrame.vth)) )
, :];
sSamp = sdf[ StatsBase.sample( MersenneTwister(rngSeed)
, 1:size(sdf, 1)
, pweights(sdf.id)
, 3000000
; replace = false
, ordered = false )
, : ];
# Sample 1/3rd of Data in Triode Region without weights
tdf = dataFrame[ ifelse( deviceType == :n
, (dataFrame.Vds .<= (dataFrame.Vgs .- dataFrame.vth))
, (dataFrame.Vds .>= (dataFrame.Vgs .+ dataFrame.vth)) )
, : ];
tSamp = tdf[ StatsBase.sample( MersenneTwister(rngSeed)
, 1:size(tdf, 1)
#, pweights(tdf.id)
, 1000000
; replace = false
, ordered = false )
, : ];
# Join samples and shuffle all observations
df = shuffleobs(vcat(tSamp, sSamp));
# Box-Cox-Transformation Functions
boxCox(yᵢ; λ = 0.2) = λ != 0 ? (((yᵢ.^λ) .- 1) ./ λ) : log.(yᵢ);
coxBox(y′; λ = 0.2) = λ != 0 ? exp.(log.((λ .* y′) .+ 1) / λ) : exp.(y′);
# Split into Features and Outputs
rawX = Matrix(df[:, paramsX ])';
rawY = Matrix(df[:, paramsY ])';
# Apply Box Cox transformation to outputs (negative C's don't make sense)
λ = 0.2;
rawX[maskBCX,:] = boxCox.(abs.(rawX[maskBCX,:]); λ = λ);
rawY[maskBCY,:] = boxCox.(abs.(rawY[maskBCY,:]); λ = λ);
# Scale all observations to unit range
utX = StatsBase.fit(UnitRangeTransform, rawX; dims = 2, unit = true);
utY = StatsBase.fit(UnitRangeTransform, rawY; dims = 2, unit = true);
# Transform all data to be ∈ [0;1]
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
φ = 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);
# 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
, 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 = 100; # 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 plotting
losses = @chain errs begin
vcat(_...)
Float64.()
end;
# Plot Training Process
plot( 1:numEpochs, losses
; lab = ["MSE" "MAE"]
, title = "$(deviceName) Training"
, xaxis = "# Epoch", yaxis = "Error"
, w = 2 )
## Use current model ###
φ = φ |> cpu;
######################
## Load specific model ##
#modelFile = "./model/ptmp90-2021-02-12T08:49:20.636/ptmp90.bson"
model = BSON.load(modelFile);
φ = model[:model];
paramsX = model[:paramsX];
paramsY = model[:paramsY];
numX = length(paramsX);
numY = length(paramsY);
utX = model[:utX];
utY = model[:utY];
maskBCX = model[:maskX];
maskBCY = model[:maskY];
λ = model[:lambda];
devName = model[:name];
devType = model[:type];
######################
## Load DB in case ##
if !@isdefined(dataFrame)
deviceName = model[:name];
dataFrame = jldopen( f -> f["database"]
, "../data/" * deviceName * ".jld"
, "r" );
end
boxCox(yᵢ; λ = 0.2) = λ != 0 ? (((yᵢ.^λ) .- 1) ./ λ) : log.(yᵢ);
coxBox(y′; λ = 0.2) = λ != 0 ? exp.(log.((λ .* y′) .+ 1) / λ) : exp.(y′);
inspectdr();
######################
# Round DB for better Plotting
dataFrame.Vgs = round.(dataFrame.Vgs, digits = 2);
dataFrame.Vds = round.(dataFrame.Vds, digits = 2);
dataFrame.Vbs = round.(dataFrame.Vbs, digits = 2);
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;
# Arbitrary Operating Point and sizing
vgs = 0.0:0.01:1.2;
qvgs = vgs.^2.0;
vds = 0.0:0.01:1.2;
evds = exp.(vds);
len = length(vgs);
W = rand(filter(w -> w > 2.0e-6, unique(dataFrame.W)));
L = rand(filter(l -> l < 1.0e-6, unique(dataFrame.L)));
VG = 0.6;
VD = 0.6;
VB = 0.0;
vbc = zeros(len);
w = fill(W, len);
l = fill(L, len);
vgc = fill(VG, len);
qvgc = vgc.^2.0;
vdc = fill(VD, len);
evdc = exp.(vdc);
rvbc = sqrt.(abs.(vbc));
## Transfer Characterisitc
# Input matrix for φ according to paramsX
xt = [ w l vgs qvgs vdc evdc vbc rvbc ]';
# Ground Truth from original Database
idtTrue = @chain dataFrame begin
_[ ( (_.W .== W)
.& (_.L .== L)
.& (_.Vbs .== VB)
.& (_.Vds .== VD))
, ["id" , "Vgs"] ]
sort(:Vgs)
_[:, :id]
end;
# Prediction from φ
yt = predict(xt);
idtPred = yt.id;
## Output Characterisitc
# Input matrix for φ according to paramsX
xo = [ w l vgc qvgc vds evds vbc rvbc ]';
# Ground Truth from original Database
idoTrue = @chain dataFrame begin
_[ ( (_.W .== W)
.& (_.L .== L)
.& (_.Vbs .== VB)
.& (_.Vgs .== VG))
, ["id" , "Vds"] ]
sort(:Vds)
_[:, :id]
end;
# Prediction from φ
yo = predict(xo);
idoPred = yo.id;
## Plot Results
Wf = formatted(W, :ENG, ndigits = 2);
Lf = formatted(L, :ENG, ndigits = 2);
plot( plot( collect(vgs), [idtTrue idtPred]
; title = "TFC @ Vds = $(VD) W = $(Wf), L = $(Lf)"
, xaxis = "Vgs [V]", yaxis = "Id [A]"
, lab = ["Truth" "Pred" ]
, yscale = :log10 )
, plot( collect(vds), [ idoTrue idoPred ]
; title = "OPC @ Vgs = $(VG) W = $(Wf), L = $(Lf)"
, lab = ["Truth" "Pred" ]
, xaxis = "Vds [V]", yaxis = "Id [A]" )
, layout = (2,1), w = 2 )