def cell_create(self, scope_name): with tf.compat.v1.variable_scope(scope_name): if self.cell_type == 'tanh': cells = rnn.MultiRNNCell([ rnn.BasicRNNCell(self.n_hidden[i]) for i in range(self.n_layers) ], state_is_tuple=True) elif self.cell_type == 'LSTM': cells = rnn.MultiRNNCell([ rnn.BasicLSTMCell(self.n_hidden[i]) for i in range(self.n_layers) ], state_is_tuple=True) elif self.cell_type == 'GRU': cells = rnn.MultiRNNCell([ rnn.GRUCell(self.n_hidden[i]) for i in range(self.n_layers) ], state_is_tuple=True) elif self.cell_type == 'LSTMP': cells = rnn.MultiRNNCell([ rnn.LSTMCell(self.n_hidden[i]) for i in range(self.n_layers) ], state_is_tuple=True) cells = rnn.DropoutWrapper(cells, input_keep_prob=self.dropout_ph, output_keep_prob=self.dropout_ph) return cells
def create_model(self, optimizer): features = tf.placeholder(tf.int32, [None, self.seq_len], name='features') labels = tf.placeholder(tf.int64, [None,], name='labels') embs = tf.Variable(self.emb_arr, dtype=tf.float32, trainable=False) x = tf.nn.embedding_lookup(embs, features) forward_cell1, backward_cell1 = rnn.BasicGRUCell(64), rnn.BasicGRUCell(64) forward_cell2, backward_cell2 = rnn.BasicGRUCell(32), rnn.BasicGRUCell(32) (birnn_fw1, birnn_bw1), _, _ = dynamic_birnn(forward_cell1, backward_cell1, x, dtype=tf.float32) print("shape:", birnn_fw1.shape, birnn_bw1.shape) stacked_lstm = rnn.MultiRNNCell( [rnn.BasicLSTMCell(self.n_hidden) for _ in range(2)]) outputs, _ = tf.nn.dynamic_rnn(stacked_lstm, x, dtype=tf.float32) fc1 = tf.layers.dense(inputs=outputs[:,-1,:], units=30) pred = tf.squeeze(tf.layers.dense(inputs=fc1, units=1)) loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=labels, logits=pred) #optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) grads_and_vars = optimizer.compute_gradients(loss) grads, _ = zip(*grads_and_vars) train_op = optimizer.apply_gradients(grads_and_vars, global_step=tf.train.get_global_step()) correct_pred = tf.equal(tf.to_int64(tf.greater(pred,0)), labels) eval_metric_ops = tf.count_nonzero(correct_pred) return features, labels, train_op, grads, eval_metric_ops, loss
def create_model(self, optimizer): features = tf.placeholder(tf.int32, [None, self.seq_len]) embedding = tf.get_variable("embedding", [self.num_classes, 8]) x = tf.nn.embedding_lookup(embedding, features) labels = tf.placeholder(tf.int32, [None, self.num_classes]) stacked_lstm = rnn.MultiRNNCell( [rnn.BasicLSTMCell(self.n_hidden) for _ in range(2)]) outputs, _ = tf.nn.dynamic_rnn(stacked_lstm, x, dtype=tf.float32) pred = tf.layers.dense(inputs=outputs[:, -1, :], units=self.num_classes) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=labels)) grads_and_vars = optimizer.compute_gradients(loss) grads, _ = zip(*grads_and_vars) train_op = optimizer.apply_gradients( grads_and_vars, global_step=tf.train.get_global_step()) correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(labels, 1)) eval_metric_ops = tf.count_nonzero(correct_pred) return features, labels, train_op, grads, eval_metric_ops, loss
def __init__(self, dim_bert, rnn_size, rnn_layer, batch_size, input_embedding_size, dim_hidden, max_words_d, vocabulary_size_d, drop_out_rate, method): self.dim_bert = dim_bert self.rnn_size = rnn_size self.rnn_layer = rnn_layer self.batch_size = batch_size self.input_embedding_size = input_embedding_size self.dim_hidden = dim_hidden self.max_words_d = max_words_d self.vocabulary_size_d = vocabulary_size_d self.drop_out_rate = drop_out_rate self.method = method if self.method == 'lstm': # print("\nmethod: {}\n".format(self.method)) # description-embedding self.embed_desc_W = tf.Variable(tf.random.uniform( [self.vocabulary_size_d, self.input_embedding_size], -0.08, 0.08), name='embed_desc_W') # encoder: RNN body self.lstm_1_d = rnn_cell.LSTMCell(rnn_size, input_embedding_size, state_is_tuple=False) self.lstm_dropout_1_d = rnn_cell.DropoutWrapper( self.lstm_1_d, output_keep_prob=1 - self.drop_out_rate) self.lstm_2_d = rnn_cell.LSTMCell(rnn_size, rnn_size, state_is_tuple=False) self.lstm_dropout_2_d = rnn_cell.DropoutWrapper( self.lstm_2_d, output_keep_prob=1 - self.drop_out_rate) self.stacked_lstm_d = rnn_cell.MultiRNNCell( [self.lstm_dropout_1_d, self.lstm_dropout_2_d]) # description state-embedding self.embed_state_desc_W = tf.Variable(tf.random.uniform( [2 * rnn_size * rnn_layer, self.dim_hidden], -0.08, 0.08), name='embed_state_desc_W') elif self.method == 'bert': # print("\nmethod: {}\n".format(self.method)) # description state-embedding self.embed_state_desc_W = tf.Variable(tf.random.uniform( [dim_bert, self.dim_hidden], -0.08, 0.08), name='embed_state_desc_W') self.embed_state_desc_b = tf.Variable(tf.random.uniform( [self.dim_hidden], -0.08, 0.08), name='embed_state_desc_b') # score embedding self.embed_score_W = tf.Variable(tf.random.uniform( [dim_hidden, vocabulary_size_d], -0.08, 0.08), name='embed_score_W') self.embed_score_b = tf.Variable(tf.random.uniform([vocabulary_size_d], -0.08, 0.08), name='embed_score_b')
def __init__(self, rnn_size, rnn_layer, batch_size, input_embedding_size, dim_image, dim_hidden, max_words_q, vocabulary_size, max_words_d, vocabulary_size_d, drop_out_rate, num_output): self.rnn_size = rnn_size self.rnn_layer = rnn_layer self.batch_size = batch_size self.input_embedding_size = input_embedding_size self.dim_image = dim_image self.dim_hidden = dim_hidden self.max_words_q = max_words_q self.vocabulary_size = vocabulary_size self.max_words_d = max_words_d self.vocabulary_size_d = vocabulary_size_d self.drop_out_rate = drop_out_rate self.num_output = num_output # question-embedding self.embed_ques_W = tf.Variable(tf.random.uniform([self.vocabulary_size, self.input_embedding_size], -0.08, 0.08), name='embed_ques_W') # encoder: RNN body self.lstm_1 = rnn_cell.LSTMCell(rnn_size, input_embedding_size, state_is_tuple=False) self.lstm_dropout_1 = rnn_cell.DropoutWrapper(self.lstm_1, output_keep_prob = 1 - self.drop_out_rate) self.lstm_2 = rnn_cell.LSTMCell(rnn_size, rnn_size, state_is_tuple=False) self.lstm_dropout_2 = rnn_cell.DropoutWrapper(self.lstm_2, output_keep_prob = 1 - self.drop_out_rate) self.stacked_lstm = rnn_cell.MultiRNNCell([self.lstm_dropout_1, self.lstm_dropout_2]) # state-embedding self.embed_state_W = tf.Variable(tf.random.uniform([2*rnn_size*rnn_layer, self.dim_hidden], -0.08,0.08),name='embed_state_W') self.embed_state_b = tf.Variable(tf.random.uniform([self.dim_hidden], -0.08, 0.08), name='embed_state_b') # description-embedding self.embed_desc_W = tf.Variable(tf.random.uniform([self.vocabulary_size_d, self.input_embedding_size], -0.08, 0.08), name='embed_desc_W') # encoder: RNN body self.lstm_1_d = rnn_cell.LSTMCell(rnn_size, input_embedding_size, state_is_tuple=False) self.lstm_dropout_1_d = rnn_cell.DropoutWrapper(self.lstm_1_d, output_keep_prob = 1 - self.drop_out_rate) self.lstm_2_d = rnn_cell.LSTMCell(rnn_size, rnn_size, state_is_tuple=False) self.lstm_dropout_2_d = rnn_cell.DropoutWrapper(self.lstm_2_d, output_keep_prob = 1 - self.drop_out_rate) self.stacked_lstm_d = rnn_cell.MultiRNNCell([self.lstm_dropout_1_d, self.lstm_dropout_2_d]) # description state-embedding self.embed_state_desc_W = tf.Variable(tf.random.uniform([2*rnn_size*rnn_layer, self.dim_hidden], -0.08,0.08),name='embed_state_desc_W') self.embed_state_desc_b = tf.Variable(tf.random.uniform([self.dim_hidden], -0.08, 0.08), name='embed_state_desc_b') # image-embedding 1 self.embed_image_W = tf.Variable(tf.random.uniform([dim_image, self.dim_hidden], -0.08, 0.08), name='embed_image_W') self.embed_image_b = tf.Variable(tf.random.uniform([dim_hidden], -0.08, 0.08), name='embed_image_b') # my code # image-embedding 2 self.embed_image2_W = tf.Variable(tf.random.uniform([dim_image, self.dim_hidden], -0.08, 0.08), name='embed_image2_W') self.embed_image2_b = tf.Variable(tf.random.uniform([dim_hidden], -0.08, 0.08), name='embed_image2_b') # image-embedding 3 self.embed_image3_W = tf.Variable(tf.random.uniform([dim_image, self.dim_hidden], -0.08, 0.08), name='embed_image3_W') self.embed_image3_b = tf.Variable(tf.random.uniform([dim_hidden], -0.08, 0.08), name='embed_image3_b') # image-embedding 4 self.embed_image4_W = tf.Variable(tf.random.uniform([dim_image, self.dim_hidden], -0.08, 0.08), name='embed_image4_W') self.embed_image4_b = tf.Variable(tf.random.uniform([dim_hidden], -0.08, 0.08), name='embed_image4_b') # image-embedding 5 self.embed_image5_W = tf.Variable(tf.random.uniform([dim_image, self.dim_hidden], -0.08, 0.08), name='embed_image5_W') self.embed_image5_b = tf.Variable(tf.random.uniform([dim_hidden], -0.08, 0.08), name='embed_image5_b') # end my code # score-embedding self.embed_scor_W = tf.Variable(tf.random.uniform([dim_hidden, self.num_output], -0.08, 0.08), name='embed_scor_W') self.embed_scor_b = tf.Variable(tf.random.uniform([self.num_output], -0.08, 0.08), name='embed_scor_b')