Skip to content

Commit

Permalink
added fit method for NNsPOD
Browse files Browse the repository at this point in the history
  • Loading branch information
MMRROOO committed Jul 22, 2022
1 parent 1171901 commit a017139
Show file tree
Hide file tree
Showing 2 changed files with 645 additions and 1,838 deletions.
44 changes: 40 additions & 4 deletions ezyrb/nnspod.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def reshape1dto2d(self, snapshots):
"""
return snapshots.reshape(int(np.sqrt(len(snapshots))), int(np.sqrt(len(snapshots))))

def train_interpnet(self,ref_data, interp_layers, interp_function, interp_stop_training, interp_loss, retrain = False, frequency_print = 0):
def train_interpnet(self,ref_data, interp_layers, interp_function, interp_stop_training, interp_loss, retrain = False, frequency_print = 0, save = True):
"""
trains the Interpnet given 1d data:
Expand All @@ -58,10 +58,12 @@ def train_interpnet(self,ref_data, interp_layers, interp_function, interp_stop_t
print("loaded interpnet")
except:
self.interp_net.fit(space, snapshots, frequency_print = frequency_print)
self.interp_net.save_state(self.path)
if save:
self.interp_net.save_state(self.path)
else:
self.interp_net.fit(space, snapshots, frequency_print = frequency_print)
self.interp_net.save_state(self.path)
if save:
self.interp_net.save_state(self.path)

def shift(self, x, y, shift_quantity):
"""
Expand Down Expand Up @@ -187,4 +189,38 @@ def train_shiftnet(self, db, shift_layers, shift_function, shift_stop_training,
shift)[0]
x_ret = x_new.detach().numpy()
return x_ret


def fit(self, db, ref_point):

self.train_interpnet(db[ref_point], [20,20], nn.Sigmoid(), [0.000001], None, retrain = False, frequency_print = 5)
new_x = np.zeros(shape = db.space.shape)
i = 0
while i < db.parameters.shape[0]:
new_x[i] = self.train_shiftnet(db[i], [20,20,20], nn.Tanh(), [1000, 0.00001], db[ref_point], preshift = True, frequency_print = 5).reshape(-1)
i+=1
if i == ref_point:
new_x[ref_point] = db.space[ref_point]
i +=1
db = Database(space = new_x, snapshots = db.snapshots, parameters = db.parameters)

i = 0
new_snapshots = np.zeros(shape = db.snapshots.shape)
new_space = np.zeros(shape = db.space.shape)
while i < db.parameters.shape[0]:
self.train_interpnet(db[i], [20,20], nn.Sigmoid(), [0.00001], None, retrain = True, frequency_print = 50, save = False)
new_snapshots[i] = self.interp_net.model(torch.from_numpy(db.space[ref_point].reshape(-1,1)).float()).detach().numpy().reshape(-1)
new_space[i] = db.space[ref_point]
i+=1
if i == ref_point:
new_snapshots[ref_point] = db.snapshots[ref_point]
new_space[ref_point] = db.space[ref_point]
i +=1

db = Database(space = new_space, snapshots = new_snapshots, parameters = db.parameters)
POD_ = POD(method = 'svd')
return POD_.fit(db.snapshots)

##Ideas for fit:
# create an interpnet for each datapoint, and use to get snapshots
# and parameters at ref data positions
#
Loading

0 comments on commit a017139

Please sign in to comment.