def set_network(self): ########################################## # Recurrent layer ########################################## with tf.variable_scope('LSTMcell'): # 여러개의 셀을 조합한 RNN 셀을 생성합니다. multi_cells = tf.contrib.rnn.MultiRNNCell( [utils.LSTM_cell(FLAGS.RNN_HIDDEN_DIMENSION, self.Dropout_Rate1, self.Dropout_Rate2) for _ in range(FLAGS.N_LAYERS)]) # RNN 신경망을 생성 (SEQ를 통해 매 sentence의 길이까지만 계산을 해 효율성 증대) self.outputs, _states = tf.nn.dynamic_rnn(cell=multi_cells, inputs=self.X, dtype=tf.float32) # sequence_length=self.SEQ, # 마지막에 해당하는 결과물을 가져옴 (utils의 last_relevant 함수를 이용) self.rnn_outputs = utils.last_relevant(self.outputs, self.SEQ) ########################################## # Fully connected network ########################################## with tf.variable_scope('FC-layer'): FC1 = tf.contrib.layers.fully_connected(self.rnn_outputs, FLAGS.FC_HIDDEN_DIMENSION, activation_fn=None) FC_act1 = tf.nn.relu(tf.layers.batch_normalization(FC1, momentum=0.9, training=self.TRAIN_PH)) FC2 = tf.contrib.layers.fully_connected(FC_act1, FLAGS.FC_HIDDEN_DIMENSION, activation_fn=None) FC_act2 = tf.nn.relu(tf.layers.batch_normalization(FC2, momentum=0.9, training=self.TRAIN_PH)) self.y_logits = tf.contrib.layers.fully_connected(FC_act2, FLAGS.NUM_OF_CLASS, activation_fn=None)
def set_network(self): ########################################## # Embedding ########################################## with tf.variable_scope('Embedding'): self.W = tf.get_variable( name='embedding_matrix', shape=[FLAGS.VOCAB_SIZE, FLAGS.EMBEDDING_SIZE], initializer=tf.random_normal_initializer(stddev=0.1)) self.X = tf.nn.embedding_lookup(self.W, self.X_idx) ########################################## # Convolutional layer ########################################## net = self.X for i, width in enumerate(FLAGS.CONV_KERNEL_WIDTH): with tf.variable_scope('conv_{}'.format(i + 1)): # 1d-conv net = utils.conv_1d(net, width, FLAGS.HIDDEN_DIMENSION, self.TRAIN_PH) # 1d-max pool # if i % 2 == 1: # net = tf.layers.max_pooling1d(net, pool_size=2, strides=2, padding='SAME') ########################################## # Recurrent layer ########################################## with tf.variable_scope('LSTMcell'): # 여러개의 셀을 조합한 RNN 셀을 생성합니다. multi_cells = tf.contrib.rnn.MultiRNNCell([ utils.LSTM_cell(FLAGS.RNN_HIDDEN_DIMENSION, self.Dropout_Rate1, self.Dropout_Rate2) for _ in range(FLAGS.N_LAYERS) ]) # RNN 신경망을 생성 (SEQ를 통해 매 sentence의 길이까지만 계산을 해 효율성 증대) outputs, _states = tf.nn.dynamic_rnn( cell=multi_cells, inputs=net, dtype=tf.float32) #sequence_length=self.SEQ, # Attention with tf.variable_scope('Attention'): self.att_matrix, self.att_alpha = utils.attention( INPUTS=outputs, ATTENTION_SIZE=FLAGS.ATTENTION_SIZE, SEQ=self.SEQ, time_major=False, return_alphas=True) ########################################## # Fully connected network ########################################## with tf.variable_scope('FC-layer'): FC1 = tf.contrib.layers.fully_connected( self.att_matrix, FLAGS.FC_HIDDEN_DIMENSION, weights_initializer=utils.he_init, activation_fn=None) FC_act1 = tf.nn.relu( tf.layers.batch_normalization(FC1, momentum=0.9, training=self.TRAIN_PH)) FC2 = tf.contrib.layers.fully_connected( FC_act1, FLAGS.FC_HIDDEN_DIMENSION, weights_initializer=utils.he_init, activation_fn=None) FC_act2 = tf.nn.relu( tf.layers.batch_normalization(FC2, momentum=0.9, training=self.TRAIN_PH)) self.y_logits = tf.contrib.layers.fully_connected( FC_act2, FLAGS.NUM_OF_CLASS, activation_fn=None)