-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraining2D.py
More file actions
61 lines (53 loc) · 2.32 KB
/
Copy pathTraining2D.py
File metadata and controls
61 lines (53 loc) · 2.32 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 30 11:53:38 2019
@author: yizhouqian
"""
import matplotlib.pyplot as plt
import numpy as np
import scipy.io
import tensorflow as tf
from tensorflow import keras
import Data_Generation
import time
class Trainer2D:
def __init__(self, x, y, sample, ny_s, nx_s, r, rivers, H):
self.num_x=x
self.num_y=y
self.num_sample=sample
self.ny_s=ny_s
self.nx_s=nx_s
self.rivers=rivers
self.xx1,self.yy1,self.xx2,self.yy2=Data_Generation.mesh_grid(self.num_x, self.num_y, self.ny_s, self.nx_s)
self.x_coord, self.y_coord=np.meshgrid(self.xx2,self.yy2, sparse=False)
self.x_coord=np.reshape(self.x_coord, (-1, 1))
self.y_coord=np.reshape(self.y_coord, (-1, 1))
self.covar= lambda x : np.exp(-(x**2)/(r**2))
self.cov=0.15 * Data_Generation.Cov(self.x_coord, self.y_coord, self.covar)
self.L = np.linalg.cholesky(self.cov + 0.000001 * np.identity(self.num_y * self.num_x))
self.model=dict()
self.H = H
return
def GenerateData(self, number):
y, x=Data_Generation.data_generation(self.num_x, self.num_y, self.num_sample, self.ny_s, self.nx_s, self.rivers, self.L, self.H)
np.save("Data/traininginput"+str(number), y)
np.save("Data/trainingoutput"+str(number), x)
return
def LoadData(self, number):
self.y=np.load("Data/traininginput"+str(number)+".npy")
self.x=np.load("Data/trainingoutput"+str(number)+".npy")
return
def CreateNetwork(self, Hidden_layer, fname, Regularizer, name):
self.T=int(self.y.shape[0]*9/10)
self.T=((int)(self.T/32))*32
Model=keras.Sequential()
for i in range(len(Hidden_layer)):
Model.add(keras.layers.Dense(Hidden_layer[i], kernel_initializer='random_uniform',kernel_regularizer=Regularizer, activation=fname))
Model.add(keras.layers.Dense(self.x.shape[1], activation='linear'))
self.model[name]=Model
return
def TrainNetwork(self, Optimizer, ep, name, los='mse'):
self.model[name].compile(optimizer=Optimizer, loss=los, metrics=['mae'])
self.model[name].fit(self.y[:self.T,:], self.x[:self.T,:], epochs=ep, batch_size=16, validation_data=(self.y[self.T:,:], self.x[self.T:,:]))
return