-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainingHelper.py
181 lines (164 loc) · 9.67 KB
/
trainingHelper.py
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
# Copyright 2023 Rene Winchenbach
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# main file that includes all relevant sph functionality
from sph import *
# main file that includes all learning relevant functionality, not necessary to understand
from rbfConv import *
from tqdm.notebook import tqdm
# plotting/UI related imports
import matplotlib as mpl
import copy
cmap = mpl.colormaps['viridis']
def loadBatch(simulationStates, minDomain, maxDomain, particleSupport, bdata, getFeatures, getGroundTruth, stacked):
positions = [simulationStates[i,0,:] for i in bdata]
areas = [simulationStates[i,-1,:] for i in bdata]
velocities = [simulationStates[i,1,:] for i in bdata]
updates = [simulationStates[i,-2,:] for i in bdata]
# compute ghost particles for batch for neighborhood search
ghosts = [createGhostParticles(p, minDomain, maxDomain) for p in positions]
# perform neighborhood search for batch and split the data into 3 separate lists
neighborInformation = [findNeighborhoods(p, g, particleSupport) for p,g in zip(positions, ghosts)]
neighbors = [n[0] for n in neighborInformation]
radialDistances = [n[1] for n in neighborInformation]
distances = [n[2] for n in neighborInformation]
# compute the density on the given batch data
densities = [computeDensity(p, a, particleSupport, r, n) for p,a,r,n in zip(positions,areas,radialDistances, neighbors)]
densities = [simulationStates[i,2,:] for i in bdata]
# all data so far is in lists of equal length, merge lists with special attention to the neighborlist to make sure indices are pointing to the correct particles
stackedPositions = torch.hstack(positions).type(torch.float32)
stackedAreas = torch.hstack(areas).type(torch.float32)
stackedVelocities = torch.hstack(velocities).type(torch.float32)
stackedUpdates = torch.hstack(updates).type(torch.float32)
stackedNeighbors = torch.hstack([i * positions[0].shape[0] + neighbors[i] for i in range(len(neighbors))])
stackedRadialDistances = torch.hstack(radialDistances).type(torch.float32)
stackedDistances = torch.hstack(distances).type(torch.float32)
stackedDensities = torch.hstack(densities).type(torch.float32)
# tensor of ones to make learning easier
ones = torch.ones_like(stackedAreas)
# compute the signed distances needed for the network layer, uses the radialDistances and directions computed before
d = stackedRadialDistances[:,None] * torch.sign(stackedDistances[:,None])
return stackedPositions, getFeatures(stackedPositions, stackedAreas, stackedVelocities, stackedUpdates), getGroundTruth(bdata, stacked, simulationStates), stackedNeighbors, d
# useful function for learning, returns non normalized windows
def getWindowFunction(windowFunction):
windowFn = lambda r: torch.ones_like(r)
if windowFunction == 'cubicSpline':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 3 - 4 * torch.clamp(1/2 - r, min = 0) ** 3
if windowFunction == 'quarticSpline':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 4 - 5 * torch.clamp(3/5 - r, min = 0) ** 4 + 10 * torch.clamp(1/5- r, min = 0) ** 4
if windowFunction == 'quinticSpline':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 5 - 6 * torch.clamp(2/3 - r, min = 0) ** 5 + 15 * torch.clamp(1/3 - r, min = 0) ** 5
if windowFunction == 'Wendland2_1D':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 3 * (1 + 3 * r)
if windowFunction == 'Wendland4_1D':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 5 * (1 + 5 * r + 8 * r**2)
if windowFunction == 'Wendland6_1D':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 7 * (1 + 7 * r + 19 * r**2 + 21 * r**3)
if windowFunction == 'Wendland2':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 4 * (1 + 4 * r)
if windowFunction == 'Wendland4':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 6 * (1 + 6 * r + 35/3 * r**2)
if windowFunction == 'Wendland6':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 8 * (1 + 8 * r + 25 * r**2 + 32 * r**3)
if windowFunction == 'Hoct4':
def hoct4(x):
alpha = 0.0927 # Subject to 0 = (1 − α)** nk−2 + A(γ − α)**nk−2 + B(β − α)**nk−2
beta = 0.5 # Free parameter
gamma = 0.75 # Free parameter
nk = 4 # order of kernel
A = (1 - beta**2) / (gamma ** (nk - 3) * (gamma ** 2 - beta ** 2))
B = - (1 + A * gamma ** (nk - 1)) / (beta ** (nk - 1))
P = -nk * (1 - alpha) ** (nk - 1) - nk * A * (gamma - alpha) ** (nk - 1) - nk * B * (beta - alpha) ** (nk - 1)
Q = (1 - alpha) ** nk + A * (gamma - alpha) ** nk + B * (beta - alpha) ** nk - P * alpha
termA = P * x + Q
termB = (1 - x) ** nk + A * (gamma - x) ** nk + B * (beta - x) ** nk
termC = (1 - x) ** nk + A * (gamma - x) ** nk
termD = (1 - x) ** nk
termE = 0 * x
termA[x > alpha] = 0
termB[x <= alpha] = 0
termB[x > beta] = 0
termC[x <= beta] = 0
termC[x > gamma] = 0
termD[x <= gamma] = 0
termD[x > 1] = 0
termE[x < 1] = 0
return termA + termB + termC + termD + termE
windowFn = lambda r: hoct4(r)
if windowFunction == 'Spiky':
windowFn = lambda r: torch.clamp(1 - r, min = 0) ** 3
if windowFunction == 'Mueller':
windowFn = lambda r: torch.clamp(1 - r ** 2, min = 0) ** 3
if windowFunction == 'poly6':
windowFn = lambda r: torch.clamp((1 - r)**3, min = 0)
if windowFunction == 'Parabola':
windowFn = lambda r: torch.clamp(1 - r**2, min = 0)
if windowFunction == 'Linear':
windowFn = lambda r: torch.clamp(1 - r, min = 0)
return windowFn
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def processDataLoaderIter(pb, iterations, epoch, lr, dataLoader, dataIter, batchSize, model, optimizer, simulationStates, minDomain, maxDomain, particleSupport, lossFunction, getFeatures, getGroundTruth, stacked, train = True, prefix = '', augmentAngle = False, augmentJitter = False, jitterAmount = 0.01):
with record_function("process data loader"):
losses = []
batchIndices = []
weights = []
if train:
model.train(True)
else:
model.train(False)
i = 0
for b in (pbl := tqdm(range(iterations), leave=False)):
# get next batch from dataLoader, if all batches have been processed get a new iterator (which shuffles the batch order)
try:
bdata = next(dataIter)
if len(bdata) < batchSize :
raise Exception('batch too short')
except:
dataIter = iter(dataLoader)
bdata = next(dataIter)
# the actual batch processing step
with record_function("process data loader[batch]"):
# reset optimizer gradients
if train:
optimizer.zero_grad()
# load data for batch
stackedPositions, features, groundTruth, stackedNeighbors, d = loadBatch(simulationStates, minDomain, maxDomain, particleSupport, bdata, getFeatures, getGroundTruth, stacked)
# run the network layer
prediction = model((features[:,None], features[:,None]), stackedNeighbors, d)
# compute the loss
lossTerm = lossFunction(prediction, groundTruth)
loss = torch.mean(lossTerm)
# store the losses for later processing
losses.append(lossTerm.detach().cpu().numpy())
# store the current weights before the update
weights.append(copy.deepcopy({k: v.cpu() for k, v in model.state_dict().items()}))
# update the network weights
if train:
loss.backward()
optimizer.step()
# create some information to put on the tqdm progress bars
batchString = str(np.array2string(np.array(bdata), formatter={'float_kind':lambda x: "%.2f" % x, 'int':lambda x:'%04d' % x}))
pbl.set_description('%8s[gpu %d]: %3d [%1d] @ %1.1e: : %s -> %.2e' %(prefix, 0, epoch, 0, lr, batchString, loss.detach().cpu().numpy()))
pb.set_description('[gpu %d] %90s - Learning: %1.4e' %(0, "", np.mean(np.vstack(losses))))
pb.update()
batchIndices.append(bdata)
# stack the processed batches and losses for further processing
bIndices = np.hstack(batchIndices)
# and return
return bIndices, losses, weights