-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAE_NLG.py
More file actions
91 lines (80 loc) · 3.09 KB
/
Copy pathFAE_NLG.py
File metadata and controls
91 lines (80 loc) · 3.09 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 09:19:36 2018
@author: GangLi
"""
import keras
import numpy as np
from keras import backend as K
from keras.layers import Input, Dense
from keras.models import Model
from keras.optimizers import RMSprop, Adam
import matplotlib as mpl
#mpl.use('macOsX')
##########################################################
# Generate Data
##########################################################
def DataGenerateFunction(z,mu,m):
x = np.transpose(np.tile(mu,(m,1))) + np.matmul(np.diag(np.power(mu,(3/2))),z)
return x
def model1(m = 10, n = 30):
mu = np.random.uniform(0, 6, n)
z = np.random.normal(0, 1, (n,m))
x = DataGenerateFunction(z,mu,m)
return (m, n, x, z, mu)
(m, n, x, z, mu0) = model1(m = 3, n = 100000)
x=x[:,:,np.newaxis]
z=z[:,:,np.newaxis]
r=0.8 # ratio of train and validation
train_X=x[0:int(n*r),:,:]
train_z=z[0:int(n*r),:,:]
valid_X=x[int(n*r):n,:,:]
valid_z=z[int(n*r):n,:,:]
print(train_X.shape[0], 'train samples')
print(valid_X.shape[0], 'test samples')
######################################################
# Define the FAE
######################################################
inChannel = 1
# this is our input placeholder
x_input = Input(shape = (m, inChannel),name='x')
z_input = Input(shape=(m, inChannel), name='z')
# "encoded" is the encoded representation of the input
encoded=keras.layers.concatenate([x_input,z_input])
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(128, activation='relu')(encoded)
encoded = Dense(512, activation='relu')(encoded)
encoded = Dense(128, activation='relu')(encoded)
encoded = Dense(512, activation='relu')(encoded)
encoded = Dense(128, activation='relu')(encoded)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(128, activation='relu')(encoded)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded)
mu = Dense(1, activation='relu',name='mu_hat')(encoded)
mu1 = keras.layers.AveragePooling1D(pool_size=3, strides=None, padding='valid')(mu)
def dg(ip):
mu1 = ip[0]
z2 = ip[1]
mu2=K.repeat_elements(mu1,3,1)
return mu2 + K.pow(mu2,3/2)*z2
decoded = keras.layers.Lambda(dg)([mu1,z_input])
FAE = Model(inputs=[x_input,z_input], outputs=[decoded,mu1])
FAE.compile( optimizer = Adam(),
loss={'lambda_1': 'mean_squared_error',
'average_pooling1d_1': 'mean_squared_error'},
loss_weights={'lambda_1': 1,
'average_pooling1d_1': 1})
FAE.summary()
###### Model Fitting
batch_size = 250
epochs = 10
FAE_train=FAE.fit({'x': train_X, 'z': train_z},
{'lambda_1': train_X,
'average_pooling1d_1': mu0[0:int(n*r),np.newaxis,np.newaxis] },
batch_size=batch_size,epochs=epochs,verbose=1,
validation_data=({'x': valid_X, 'z': valid_z},
{'lambda_1': valid_X,
'average_pooling1d_1':mu0[int(n*r):n,np.newaxis,np.newaxis] }))
[FAE_pred_X,FAE_pred_mu]=FAE.predict({'x': valid_X, 'z': valid_z})