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 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 __init__(self, h, w, c, z_dim=100, gf_dim=64, df_dim=64): self.z_dim = z_dim out_shape2 = same_nd([h, w], kernel_size=(5, 5), stride=(2, 2)) out_shape4 = same_nd(out_shape2, kernel_size=(5, 5), stride=(2, 2)) out_shape8 = same_nd(out_shape4, kernel_size=(5, 5), stride=(2, 2)) out_shape16 = same_nd(out_shape8, kernel_size=(5, 5), stride=(2, 2)) h16, w16 = out_shape16 with tf.variable_scope('Generator'): self.g_layers = [ Linear(z_dim, 8 * gf_dim * h16 * w16), Reshape([-1, h16, w16, 8 * gf_dim]), # TFBatchNormalization(name='gbn1'), BatchNormalization(input_shape=[h16, w16, 8 * gf_dim]), RELU(), Conv2D_Transpose(input_channels=8 * gf_dim, num_filters=4 * gf_dim, output_shape=out_shape8, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), # TFBatchNormalization(name='gbn2'), BatchNormalization(input_shape=out_shape8 + [4 * gf_dim]), RELU(), Conv2D_Transpose(input_channels=4 * gf_dim, num_filters=2 * gf_dim, output_shape=out_shape4, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), # TFBatchNormalization(name='gbn3'), BatchNormalization(input_shape=out_shape4 + [2 * gf_dim]), RELU(), Conv2D_Transpose(input_channels=2 * gf_dim, num_filters=gf_dim, output_shape=out_shape2, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), # TFBatchNormalization(name='gbn4'), BatchNormalization(input_shape=out_shape2 + [gf_dim]), RELU(), Conv2D_Transpose(input_channels=gf_dim, num_filters=c, output_shape=(h, w), kernel_size=(5, 5), stride=(2, 2), padding='SAME'), # Sigmoid() ] out_shape2 = same_nd([h, w], kernel_size=(5, 5), stride=(2, 2)) out_shape4 = same_nd(out_shape2, kernel_size=(5, 5), stride=(2, 2)) out_shape8 = same_nd(out_shape4, kernel_size=(5, 5), stride=(2, 2)) out_shape16 = same_nd(out_shape8, kernel_size=(5, 5), stride=(2, 2)) h16, w16 = out_shape16 with tf.variable_scope('Discriminator'): self.d1_layers = [ Conv2D(input_channels=c, num_filters=df_dim, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), LeakyRELU(), Conv2D(input_channels=df_dim, num_filters=2 * df_dim, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), ] # TFBatchNormalization(name='dbn1'), self.d2_layers = [ BatchNormalization(input_shape=out_shape4 + [2 * df_dim]), LeakyRELU(), Conv2D(input_channels=2 * df_dim, num_filters=4 * df_dim, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), ] self.d3_layers = [ # TFBatchNormalization(name='dbn2'), BatchNormalization(input_shape=out_shape8 + [4 * df_dim]), LeakyRELU(), Conv2D(input_channels=4 * df_dim, num_filters=8 * df_dim, kernel_size=(5, 5), stride=(2, 2), padding='SAME'), ] self.d4_layers = [ # TFBatchNormalization(name='dbn3'), BatchNormalization(input_shape=out_shape16 + [8 * df_dim]), LeakyRELU(), ReduceMax(reduction_indices=[1, 2]), ] self.d5_layers = [ Flatten(), Linear(8 * df_dim, 1), # LeakyRELU(), # Linear(1000, 1) # Sigmoid() ] print('====:', 8 * df_dim)
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