def model(word_len, sent_len, nclass): unicode_size = 1000 ch_embed_dim = 20 h, w = valid(ch_embed_dim, word_len, stride=(1, 1), kernel_size=(ch_embed_dim, 5)) h, w = valid(h, w, stride=(1, 1), kernel_size=(1, 5)) h, w = valid(h, w, stride=(1, 2), kernel_size=(1, 5)) conv_out_dim = int(h * w * 60) X_ph = tf.placeholder('int32', [None, sent_len, word_len]) input_sn = tg.StartNode(input_vars=[X_ph]) charcnn_hn = tg.HiddenNode(prev=[input_sn], layers=[ Reshape(shape=(-1, word_len)), Embedding(cat_dim=unicode_size, encode_dim=ch_embed_dim, zero_pad=True), Reshape(shape=(-1, ch_embed_dim, word_len, 1)), Conv2D(input_channels=1, num_filters=20, padding='VALID', kernel_size=(ch_embed_dim, 5), stride=(1, 1)), RELU(), Conv2D(input_channels=20, num_filters=40, padding='VALID', kernel_size=(1, 5), stride=(1, 1)), RELU(), Conv2D(input_channels=40, num_filters=60, padding='VALID', kernel_size=(1, 5), stride=(1, 2)), RELU(), Flatten(), Linear(conv_out_dim, nclass), Reshape((-1, sent_len, nclass)), ReduceSum(1), Softmax() ]) output_en = tg.EndNode(prev=[charcnn_hn]) graph = tg.Graph(start=[input_sn], end=[output_en]) y_train_sb = graph.train_fprop()[0] y_test_sb = graph.test_fprop()[0] return X_ph, y_train_sb, y_test_sb
def discriminator(self): if not self.generator_called: raise Exception( 'self.generator() has to be called first before self.discriminator()' ) scope = 'Discriminator' with self.tf_graph.as_default(): with tf.name_scope(scope): h1, w1 = valid(self.h, self.w, kernel_size=(5, 5), stride=(1, 1)) h2, w2 = valid(h1, w1, kernel_size=(5, 5), stride=(2, 2)) h3, w3 = valid(h2, w2, kernel_size=(5, 5), stride=(2, 2)) flat_dim = int(h3 * w3 * 32) real_ph = tf.placeholder('float32', [None, self.h, self.w, 1], name='real') real_sn = tg.StartNode(input_vars=[real_ph]) # fake_ph = tf.placeholder('float32', [None, self.h, self.w, 1], name='fake') # fake_sn = tg.StartNode(input_vars=[fake_ph]) disc_hn = tg.HiddenNode( prev=[real_sn, self.gen_hn], layers=[ Conv2D(input_channels=1, num_filters=32, kernel_size=(5, 5), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/c1'), LeakyRELU(), Conv2D(input_channels=32, num_filters=32, kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/c2'), LeakyRELU(), Conv2D(input_channels=32, num_filters=32, kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/c3'), LeakyRELU(), # Conv2D(input_channels=32, num_filters=32, kernel_size=(5,5), stride=(2,2), padding='VALID'), # RELU(), Flatten(), Linear(flat_dim, self.bottleneck_dim), TFBatchNormalization(name=scope + '/l1'), LeakyRELU(), # Dropout(0.5), ]) class_hn = tg.HiddenNode(prev=[disc_hn], layers=[ Linear(self.bottleneck_dim, self.nclass), Softmax() ]) judge_hn = tg.HiddenNode( prev=[disc_hn], layers=[ Linear(self.bottleneck_dim, 1), # Sigmoid() ]) real_class_en = tg.EndNode(prev=[class_hn]) real_judge_en = tg.EndNode(prev=[judge_hn]) fake_class_en = tg.EndNode(prev=[class_hn]) fake_judge_en = tg.EndNode(prev=[judge_hn]) graph = tg.Graph(start=[real_sn], end=[real_class_en, real_judge_en]) real_train = graph.train_fprop() real_valid = graph.test_fprop() # dis_var_list = graph.variables # for var in dis_var_list: # print var.name graph = tg.Graph(start=[self.noise_sn, self.y_sn], end=[fake_class_en, fake_judge_en]) fake_train = graph.train_fprop() fake_valid = graph.test_fprop() # print('========') # for var in graph.variables: # print var.name dis_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) # for var in dis_var_list: # print(var.name) # # print('=========') # for var in tf.global_variables(): # print(var.name) # import pdb; pdb.set_trace() # print() # graph = tg.Graph(start=[G_sn], end=[class_en, judge_en]) # class_train_sb, judge_train_sb = graph.train_fprop() # symbolic outputs # class_test_sb, judge_test_sb = graph.test_fprop() # symbolic outputs return real_ph, real_train, real_valid, fake_train, fake_valid, dis_var_list
def generator(self): self.generator_called = True with self.tf_graph.as_default(): scope = 'Generator' with tf.name_scope(scope): # X_ph = tf.placeholder('float32', [None, self.h, self.w, 1], name='X') # X_sn = tg.StartNode(input_vars=[X_ph]) noise_ph = tf.placeholder('float32', [None, self.bottleneck_dim], name='noise') self.noise_sn = tg.StartNode(input_vars=[noise_ph]) h1, w1 = valid(self.h, self.w, kernel_size=(5, 5), stride=(1, 1)) h2, w2 = valid(h1, w1, kernel_size=(5, 5), stride=(2, 2)) h3, w3 = valid(h2, w2, kernel_size=(5, 5), stride=(2, 2)) flat_dim = int(h3 * w3 * 32) print('h1:{}, w1:{}'.format(h1, w1)) print('h2:{}, w2:{}'.format(h2, w2)) print('h3:{}, w3:{}'.format(h3, w3)) print('flat dim:{}'.format(flat_dim)) # enc_hn = tg.HiddenNode(prev=[X_sn], # layers=[Conv2D(input_channels=1, num_filters=32, kernel_size=(5,5), stride=(1,1), padding='VALID'), # RELU(), # Conv2D(input_channels=32, num_filters=32, kernel_size=(5,5), stride=(2,2), padding='VALID'), # RELU(), # Conv2D(input_channels=32, num_filters=32, kernel_size=(5,5), stride=(2,2), padding='VALID'), # RELU(), # # Conv2D(input_channels=32, num_filters=32, kernel_size=(5,5), stride=(2,2), padding='VALID'), # # RELU(), # Flatten(), # Linear(flat_dim, 300), # RELU(), # # seq.add(Dropout(0.5)) # Linear(300, self.bottleneck_dim), # Tanh(), # ]) y_ph = tf.placeholder('float32', [None, self.nclass], name='y') self.y_sn = tg.StartNode(input_vars=[y_ph]) noise_hn = tg.HiddenNode(prev=[self.noise_sn, self.y_sn], input_merge_mode=Concat(1)) self.gen_hn = tg.HiddenNode( prev=[noise_hn], layers=[ Linear(self.bottleneck_dim + 10, flat_dim), RELU(), ######[ Method 0 ]###### # Reshape((-1, h3, w3, 32)), # Conv2D_Transpose(input_channels=32, num_filters=100, output_shape=(h2,w2), # kernel_size=(5,5), stride=(2,2), padding='VALID'), ######[ End Method 0 ]###### ######[ Method 1 ]###### Reshape((-1, 1, 1, flat_dim)), Conv2D_Transpose(input_channels=flat_dim, num_filters=200, output_shape=(2, 2), kernel_size=(2, 2), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/dc1'), RELU(), Conv2D_Transpose(input_channels=200, num_filters=100, output_shape=(h2, w2), kernel_size=(9, 9), stride=(1, 1), padding='VALID'), ######[ End Method 1 ]###### TFBatchNormalization(name=scope + '/dc2'), RELU(), Conv2D_Transpose(input_channels=100, num_filters=50, output_shape=(h1, w1), kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/dc3'), RELU(), Conv2D_Transpose(input_channels=50, num_filters=1, output_shape=(self.h, self.w), kernel_size=(5, 5), stride=(1, 1), padding='VALID'), SetShape((-1, self.h, self.w, 1)), Sigmoid() ]) y_en = tg.EndNode(prev=[self.gen_hn]) graph = tg.Graph(start=[self.noise_sn, self.y_sn], end=[y_en]) G_train_sb = graph.train_fprop()[0] G_test_sb = graph.test_fprop()[0] # import pdb; pdb.set_trace() gen_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) return y_ph, noise_ph, G_train_sb, G_test_sb, gen_var_list
def generator(self): self.generator_called = True with self.tf_graph.as_default(): scope = 'Generator' with tf.name_scope(scope): h1, w1 = valid(self.h, self.w, kernel_size=(5, 5), stride=(1, 1)) h2, w2 = valid(h1, w1, kernel_size=(5, 5), stride=(2, 2)) h3, w3 = valid(h2, w2, kernel_size=(5, 5), stride=(2, 2)) flat_dim = int(h3 * w3 * 32) print('h1:{}, w1:{}'.format(h1, w1)) print('h2:{}, w2:{}'.format(h2, w2)) print('h3:{}, w3:{}'.format(h3, w3)) print('flat dim:{}'.format(flat_dim)) self.gen_real_sn = tg.StartNode(input_vars=[self.real_ph]) enc_hn = tg.HiddenNode( prev=[self.gen_real_sn], layers=[ Conv2D(input_channels=self.c, num_filters=32, kernel_size=(5, 5), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/genc1'), RELU(), Conv2D(input_channels=32, num_filters=32, kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/genc2'), RELU(), Conv2D(input_channels=32, num_filters=32, kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/genc3'), RELU(), # Conv2D(input_channels=32, num_filters=32, kernel_size=(5,5), stride=(2,2), padding='VALID'), # RELU(), Flatten(), Linear(flat_dim, 300), TFBatchNormalization(name=scope + '/genc4'), RELU(), Linear(300, self.bottleneck_dim), Tanh(), ]) self.noise_sn = tg.StartNode(input_vars=[self.noise_ph]) self.gen_hn = tg.HiddenNode( prev=[self.noise_sn, enc_hn], input_merge_mode=Sum(), layers=[ Linear(self.bottleneck_dim, flat_dim), RELU(), ######[ Method 0 ]###### # Reshape((-1, h3, w3, 32)), # Conv2D_Transpose(input_channels=32, num_filters=100, output_shape=(h2,w2), # kernel_size=(5,5), stride=(2,2), padding='VALID'), ######[ End Method 0 ]###### ######[ Method 1 ]###### Reshape((-1, 1, 1, flat_dim)), # Reshape((-1, h)) Conv2D_Transpose(input_channels=flat_dim, num_filters=200, output_shape=(h3, w3), kernel_size=(h3, w3), stride=(1, 1), padding='VALID'), # BatchNormalization(layer_type='conv', dim=200, short_memory=0.01), TFBatchNormalization(name=scope + '/g1'), RELU(), Conv2D_Transpose(input_channels=200, num_filters=100, output_shape=(h2, w2), kernel_size=(5, 5), stride=(2, 2), padding='VALID'), # BatchNormalization(layer_type='conv', dim=100, short_memory=0.01), ######[ End Method 1 ]###### TFBatchNormalization(name=scope + '/g2'), RELU(), Conv2D_Transpose(input_channels=100, num_filters=50, output_shape=(h1, w1), kernel_size=(5, 5), stride=(2, 2), padding='VALID'), # BatchNormalization(layer_type='conv', dim=50, short_memory=0.01), TFBatchNormalization(name=scope + '/g3'), RELU(), Conv2D_Transpose(input_channels=50, num_filters=self.c, output_shape=(self.h, self.w), kernel_size=(5, 5), stride=(1, 1), padding='VALID'), SetShape((-1, self.h, self.w, self.c)), Sigmoid() ]) h, w = valid(self.h, self.w, kernel_size=(5, 5), stride=(1, 1)) h, w = valid(h, w, kernel_size=(5, 5), stride=(2, 2)) h, w = valid(h, w, kernel_size=(5, 5), stride=(2, 2)) h, w = valid(h, w, kernel_size=(h3, w3), stride=(1, 1)) y_en = tg.EndNode(prev=[self.gen_hn]) graph = tg.Graph(start=[self.noise_sn, self.gen_real_sn], end=[y_en]) G_train_sb = graph.train_fprop()[0] G_test_sb = graph.test_fprop()[0] gen_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) return self.y_ph, self.noise_ph, G_train_sb, G_test_sb, gen_var_list
def discriminator(self): if not self.generator_called: raise Exception( 'self.generator() has to be called first before self.discriminator()' ) scope = 'Discriminator' with self.tf_graph.as_default(): with tf.name_scope(scope): h1, w1 = valid(self.h, self.w, kernel_size=(5, 5), stride=(1, 1)) h2, w2 = valid(h1, w1, kernel_size=(5, 5), stride=(2, 2)) h3, w3 = valid(h2, w2, kernel_size=(5, 5), stride=(2, 2)) flat_dim = int(h3 * w3 * 32) dis_real_sn = tg.StartNode(input_vars=[self.real_ph]) # fake_ph = tf.placeholder('float32', [None, self.h, self.w, 1], name='fake') # fake_sn = tg.StartNode(input_vars=[fake_ph]) disc_hn = tg.HiddenNode( prev=[dis_real_sn, self.gen_hn], layers=[ Conv2D(input_channels=self.c, num_filters=32, kernel_size=(5, 5), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/d1'), # BatchNormalization(layer_type='conv', dim=32, short_memory=0.01), LeakyRELU(), Conv2D(input_channels=32, num_filters=32, kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/d2'), # BatchNormalization(layer_type='conv', dim=32, short_memory=0.01), LeakyRELU(), Conv2D(input_channels=32, num_filters=32, kernel_size=(5, 5), stride=(2, 2), padding='VALID'), TFBatchNormalization(name=scope + '/d3'), # BatchNormalization(layer_type='conv', dim=32, short_memory=0.01), LeakyRELU(), # Conv2D(input_channels=32, num_filters=32, kernel_size=(5,5), stride=(2,2), padding='VALID'), # RELU(), Flatten(), Linear(flat_dim, self.bottleneck_dim), # BatchNormalization(layer_type='fc', dim=self.bottleneck_dim, short_memory=0.01), TFBatchNormalization(name=scope + '/d4'), LeakyRELU(), # Dropout(0.5), ]) class_hn = tg.HiddenNode(prev=[disc_hn], layers=[ Linear(self.bottleneck_dim, self.nclass), Softmax() ]) judge_hn = tg.HiddenNode( prev=[disc_hn], layers=[Linear(self.bottleneck_dim, 1), Sigmoid()]) real_class_en = tg.EndNode(prev=[class_hn]) real_judge_en = tg.EndNode(prev=[judge_hn]) fake_class_en = tg.EndNode(prev=[class_hn]) fake_judge_en = tg.EndNode(prev=[judge_hn]) graph = tg.Graph(start=[dis_real_sn], end=[real_class_en, real_judge_en]) real_train = graph.train_fprop() real_valid = graph.test_fprop() graph = tg.Graph(start=[self.noise_sn, self.gen_real_sn], end=[fake_class_en, fake_judge_en]) fake_train = graph.train_fprop() fake_valid = graph.test_fprop() dis_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) return self.real_ph, real_train, real_valid, fake_train, fake_valid, dis_var_list
def discriminator(self): if not self.generator_called: raise Exception( 'self.generator() has to be called first before self.discriminator()' ) scope = 'Discriminator' with self.tf_graph.as_default(): with tf.name_scope(scope): h1, w1 = valid(self.h, self.w, kernel_size=(5, 5), stride=(1, 1)) h2, w2 = valid(h1, w1, kernel_size=(5, 5), stride=(2, 2)) h3, w3 = valid(h2, w2, kernel_size=(5, 5), stride=(2, 2)) flat_dim = int(h3 * w3 * 32) h1, w1 = valid(self.char_embed_dim, self.word_len, kernel_size=(self.char_embed_dim, 3), stride=(1, 1)) print('h1:{}, w1:{}'.format(h1, w1)) h2, w2 = valid(h1, w1, kernel_size=(1, 3), stride=(1, 1)) print('h2:{}, w2:{}'.format(h2, w2)) h3, w3 = valid(h2, w2, kernel_size=(1, 3), stride=(1, 1)) print('h3:{}, w3:{}'.format(h3, w3)) # h4, w4 = valid(h3, w3, kernel_size=(1,6), stride=(1,1)) # print('h4:{}, w4:{}'.format(h4, w4)) # hf, wf = h4, w4 hf, wf = h3, w3 n_filters = 100 real_sn = tg.StartNode(input_vars=[self.real_ph]) real_hn = tg.HiddenNode(prev=[real_sn], layers=[ OneHot(self.char_embed_dim), Transpose(perm=[0, 3, 2, 1]) ]) disc_hn = tg.HiddenNode( prev=[real_hn, self.gen_hn], layers=[ Conv2D(input_channels=self.sent_len, num_filters=100, kernel_size=(self.char_embed_dim, 3), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/d1'), LeakyRELU(), Conv2D(input_channels=100, num_filters=100, kernel_size=(1, 3), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/d2'), LeakyRELU(), Conv2D(input_channels=100, num_filters=100, kernel_size=(1, 3), stride=(1, 1), padding='VALID'), TFBatchNormalization(name=scope + '/d3'), LeakyRELU(), # Conv2D(input_channels=32, num_filters=128, kernel_size=(1,6), stride=(1,1), padding='VALID'), # RELU(), Flatten(), Linear(int(hf * wf * n_filters), self.bottleneck_dim), TFBatchNormalization(name=scope + '/d4'), LeakyRELU(), ]) class_hn = tg.HiddenNode(prev=[disc_hn], layers=[ Linear(self.bottleneck_dim, self.nclass), Softmax() ]) judge_hn = tg.HiddenNode( prev=[disc_hn], layers=[ Linear(self.bottleneck_dim, 1), # Sigmoid() ]) real_class_en = tg.EndNode(prev=[class_hn]) real_judge_en = tg.EndNode(prev=[judge_hn]) fake_class_en = tg.EndNode(prev=[class_hn]) fake_judge_en = tg.EndNode(prev=[judge_hn]) graph = tg.Graph(start=[real_sn], end=[real_class_en, real_judge_en]) real_train = graph.train_fprop() real_valid = graph.test_fprop() graph = tg.Graph(start=[self.noise_sn], end=[fake_class_en, fake_judge_en]) fake_train = graph.train_fprop() fake_valid = graph.test_fprop() dis_var_list = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope) return self.real_ph, real_train, real_valid, fake_train, fake_valid, dis_var_list