Skip to content

Commit 9c18df0

Browse files
committed
Heavily Commented model.py
1 parent f813492 commit 9c18df0

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed
-184 Bytes
Binary file not shown.

__pycache__/models.cpython-36.pyc

-3 Bytes
Binary file not shown.

models.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,58 @@
11
import tensorflow as tf
22
import numpy as np
33

4-
# Baseline Model with 3 layer
4+
# Baseline Model(Deterministic) with 3 layer
55
class BaselineModel3Layer:
66
def __init__(self,X,weights,biases):
7+
# X : input X data (shape : [batch_size,height,width,num_channel] type: tensor)
8+
# weights : Weights of deterministic model (type: dictionary)
9+
# biases : Biases of deterministic model (type: dictionary)
710
self.X = X
811
self.weights = weights
912
self.biases = biases
1013

11-
# Define Conv layer
14+
# Define Conv layer (for encoder)
15+
# Activation : ReLU
1216
def conv2d(self,x,W,b,stride):
1317
x = tf.nn.conv2d(x,W,strides=[1,stride,stride,1],padding="SAME")
1418
x = tf.nn.bias_add(x,b)
1519
return tf.nn.relu(x)
1620

21+
# Define Transpose Convolutional layer (for decoder)
22+
# Activation : ReLU
1723
def conv2d_transpose(self,x,W,b,stride):
24+
# Calculate new Output shape for Transpose Convolutional Operation
1825
new_shape = x.get_shape().as_list()
1926
new_shape[1] = new_shape[1]*2
2027
new_shape[2] = new_shape[2]*2
2128
new_shape[3] = b.shape[0].value
29+
# Compute Transpose Convolution
2230
x = tf.nn.conv2d_transpose(x,W,output_shape=new_shape,strides=[1,stride,stride,1],padding="SAME")
2331
x = tf.nn.bias_add(x,b)
2432
return tf.nn.relu(x)
2533

34+
# Feeding Operation of Deterministic Model
2635
def feed(self):
36+
# 1. Encode with 3 Convolutional layer
2737
self.X = self.conv2d(self.X,self.weights['wc1'],self.biases['bc1'],2)
2838
self.X = self.conv2d(self.X,self.weights['wc2'],self.biases['bc2'],2)
2939
self.X = self.conv2d(self.X,self.weights['wc3'],self.biases['bc3'],2)
40+
# 2. Decode with 3 Transpose Convolutional layer
3041
self.X = self.conv2d_transpose(self.X,self.weights['wc4'],self.biases['bc4'],2)
3142
self.X = self.conv2d_transpose(self.X,self.weights['wc5'],self.biases['bc5'],2)
3243
self.X = self.conv2d_transpose(self.X,self.weights['wc6'],self.biases['bc6'],2)
33-
return tf.clip_by_value(self.X,-1,1) # Limit X range to generate valid image
44+
# 3. Return clipped output value for it to range between -1 and 1
45+
# --> for generating valid image
46+
return tf.clip_by_value(self.X,-1,1)
3447

48+
# Latent Residual Model (Phi network Implemented) with 3 layer
3549
class LatentResidualModel3Layer:
3650
def __init__(self,X,Y,g_weights,f_weights,g_biases,f_biases,phi_weights,phi_biases):
51+
# X, Y : input X and Y(target) data
52+
# g_ : stands for G Network (pre-trained deterministic network)
53+
# f_ : stands for F Network (latent variable implemented Latent Residual Network)
54+
# phi_ : stand for Phi Network (Error Encoding Phi Network)
55+
3756
self.X = X
3857
self.Y = Y
3958
self.g_weights = g_weights
@@ -43,22 +62,28 @@ def __init__(self,X,Y,g_weights,f_weights,g_biases,f_biases,phi_weights,phi_bias
4362
self.phi_weights = phi_weights
4463
self.phi_biases = phi_biases
4564

46-
# Define Conv layer
65+
# Define Conv layer (for encoder)
66+
# Activation : ReLU
4767
def conv2d(self,x,W,b,stride):
4868
x = tf.nn.conv2d(x,W,strides=[1,stride,stride,1],padding="SAME")
4969
x = tf.nn.bias_add(x,b)
5070
return tf.nn.relu(x)
5171

72+
# Define Transpose Convolutional layer (for decoder)
73+
# Activation : ReLU
5274
def conv2d_transpose(self,x,W,b,stride):
75+
# Calculate new Output shape for Transpose Convolutional Operation
5376
new_shape = x.get_shape().as_list()
5477
new_shape[1] = new_shape[1]*2
5578
new_shape[2] = new_shape[2]*2
5679
new_shape[3] = b.shape[0].value
80+
# Compute Transpose Convolution
5781
x = tf.nn.conv2d_transpose(x,W,output_shape=new_shape,strides=[1,stride,stride,1],padding="SAME")
5882
x = tf.nn.bias_add(x,b)
5983
return tf.nn.relu(x)
6084

61-
def train(self):
85+
# Feeding Operation of Deterministic Model
86+
def feed(self):
6287
# 1. Get g_network result
6388
g_result = self.g_network()
6489

@@ -74,49 +99,67 @@ def train(self):
7499
# 5. Get f_network result
75100
f_result = self.f_network(z_emb)
76101

102+
# Return G network result, F network result, and Latent Variable
77103
return g_result, f_result, z
78104

105+
# Define G Network (Same with Deterministic Model)
79106
def g_network(self):
107+
# 1. Encode with 3 Convolutional layer
80108
result = self.conv2d(self.X,self.g_weights['wc1'],self.g_biases['bc1'],2)
81109
result = self.conv2d(result,self.g_weights['wc2'],self.g_biases['bc2'],2)
82110
result = self.conv2d(result,self.g_weights['wc3'],self.g_biases['bc3'],2)
111+
# 2. Decode with 3 Transpose Convolutional layer
83112
result = self.conv2d_transpose(result,self.g_weights['wc4'],self.g_biases['bc4'],2)
84113
result = self.conv2d_transpose(result,self.g_weights['wc5'],self.g_biases['bc5'],2)
85114
result = self.conv2d_transpose(result,self.g_weights['wc6'],self.g_biases['bc6'],2)
86-
return tf.clip_by_value(result,-1,1) # Limit X range to generate valid imags
115+
# 3. Return clipped output value for it to range between -1 and 1
116+
# --> for generating valid image
117+
return tf.clip_by_value(result,-1,1)
87118

119+
# Define F Network (Encoded Latent Variable Implemented)
88120
def f_network(self,z_emb):
89-
# Encode
121+
# 1. Encode with 3 Convolutional layer
90122
result = self.conv2d(self.X,self.f_weights['wc1'],self.f_biases['bc1'],2)
91123
result = self.conv2d(result,self.f_weights['wc2'],self.f_biases['bc2'],2)
92124
result = self.conv2d(result,self.f_weights['wc3'],self.f_biases['bc3'],2)
93125

94-
# Add Latent Variable
126+
# 2. Add Latent Variable (z_emb --> encoded latent variable)
95127
result = result + z_emb
96128

97-
# Decode
129+
# 3. Decode with 3 Transpose Convolutional layer
98130
result = self.conv2d_transpose(result,self.g_weights['wc4'],self.g_biases['bc4'],2)
99131
result = self.conv2d_transpose(result,self.g_weights['wc5'],self.g_biases['bc5'],2)
100132
result = self.conv2d_transpose(result,self.g_weights['wc6'],self.g_biases['bc6'],2)
133+
# 4. Return clipped output value for it to range between -1 and 1
134+
# --> for generating valid image
101135
return tf.clip_by_value(result,-1,1)
102136

137+
# Define Phi Network (Error Encoding Network with 4 Conv layers)
103138
def phi_network(self,residual_error):
104-
# Compute Convolutional Process of Phi Network
139+
# Get residual_error as input
140+
# 1. Encode residual error with 4 conv layers
105141
conv_result = self.conv2d(residual_error,self.phi_weights['wc1'],self.phi_biases['bc1'],2)
106142
conv_result = self.conv2d(conv_result,self.phi_weights['wc2'],self.phi_biases['bc2'],2)
107143
conv_result = self.conv2d(conv_result,self.phi_weights['wc3'],self.phi_biases['bc3'],2)
108144
conv_result = self.conv2d(conv_result,self.phi_weights['wc4'],self.phi_biases['bc4'],2)
109-
# Reshape Conv Result to [n_batch, -1]
145+
# 2. Reshape Conv Result to [n_batch, -1]
110146
conv_result = tf.reshape(conv_result,[conv_result.shape[0].value,-1])
111-
# Compute Fully Connected Process of Phi Network
147+
# 3. Compute Fully Connected Process of Phi Network
112148
fc_result = tf.nn.relu(tf.matmul(conv_result,self.phi_weights['wf1'])+self.phi_biases['bf1'])
113149
fc_result = tf.nn.relu(tf.matmul(fc_result,self.phi_weights['wf2'])+self.phi_biases['bf2'])
114150
fc_result = tf.nn.relu(tf.matmul(fc_result,self.phi_weights['wf3'])+self.phi_biases['bf3'])
151+
# 4. Return latent variable
115152
return fc_result
116153

154+
# Encodes latent variable using fully connected network
155+
# Converts latet variable to the size of '-nfeature'
156+
# --> For latent variable to be addable with encoded F network layer
117157
def latent_encoder(self,latent):
158+
# Matrix Multiplication
118159
result = tf.matmul(latent,self.phi_weights['wf4'])
160+
# Reshape to right size
119161
result = tf.reshape(result,[result.shape[0].value,1,1,result.shape[1].value])
162+
# Return Result
120163
return result
121164

122165

train_een_latent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
trainable = trainable + list(phi_biases.values())
118118

119119
model = models.LatentResidualModel3Layer(x_train,y_train,g_weights,f_weights,g_biases,f_biases,phi_weights,phi_biases)
120-
feed_op = model.train()
120+
feed_op = model.feed()
121121
loss_op = tf.losses.mean_squared_error(labels=y_train,predictions=feed_op[1])
122122
optimize_op = tf.train.AdamOptimizer(arg.lrt).minimize(loss_op,var_list=trainable)
123123

0 commit comments

Comments
 (0)