def __init__(self, is_training=True): self.graph = tf.Graph() with self.graph.as_default(): # inputs if is_training: self.x, self.y, self.num_batch = get_batch_data() # (N, 9, 9) else: self.x = tf.placeholder(tf.float32, (None, 9, 9)) self.y = tf.placeholder(tf.int32, (None, 9, 9)) self.enc = tf.expand_dims(self.x, axis=-1) # (N, 9, 9, 1) self.istarget = tf.to_float(tf.equal(self.x, tf.zeros_like( self.x))) # 0: blanks # network for i in range(hp.num_blocks): with tf.variable_scope("conv2d_{}".format(i)): self.enc = conv(self.enc, filters=hp.num_filters, size=hp.filter_size, is_training=is_training, norm_type="bn", activation_fn=tf.nn.relu) # outputs self.logits = conv(self.enc, 10, 1, scope="logits") # (N, 9, 9, 1) self.probs = tf.reduce_max(tf.nn.softmax(self.logits), axis=-1) #(N, 9, 9) self.preds = tf.to_int32(tf.arg_max(self.logits, dimension=-1)) #(N, 9, 9) # accuracy self.hits = tf.to_float(tf.equal(self.preds, self.y)) * self.istarget self.acc = tf.reduce_sum( self.hits) / (tf.reduce_sum(self.istarget) + 1e-8) tf.summary.scalar("acc", self.acc) if is_training: # Loss self.ce = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.y, logits=self.logits) self.loss = tf.reduce_sum( self.ce * self.istarget) / (tf.reduce_sum(self.istarget)) # Training Scheme self.global_step = tf.Variable(0, name='global_step', trainable=False) self.optimizer = tf.train.AdamOptimizer(learning_rate=hp.lr) self.train_op = self.optimizer.minimize( self.loss, global_step=self.global_step) tf.summary.scalar("loss", self.loss) self.merged = tf.summary.merge_all()
def load_net(self, x): """ Builds model """ conv1 = md.conv('conv1', x=x, fsize=[11, 11], nfilters=96, stride=4, padding='VALID') #default padding='SAME' pool1 = md.maxpool('pool1', x=conv1, padding='VALID') # default fsize=3, stride=2 norm1 = md.lrn(x=pool1, radius=2, alpha=2e-05, beta=0.75) conv2 = md.conv('conv2', x=norm1, fsize=[5, 5], nfilters=256, stride=1, groups=2) pool2 = md.maxpool('pool2', x=conv2, padding='VALID') norm2 = md.lrn(x=pool2, radius=2, alpha=2e-05, beta=0.75) conv3 = md.conv('conv3', x=norm2, fsize=[3, 3], nfilters=384, stride=1) conv4 = md.conv('conv4', x=conv3, fsize=[3, 3], nfilters=384, stride=1, groups=2) conv5 = md.conv('conv5', x=conv4, fsize=[3, 3], nfilters=256, stride=1) pool5 = md.maxpool('pool5', x=conv5, padding='VALID') flat = md.flatten(x=pool5) fc6 = md.fc('fc6', x=flat, noutputs=4096) if self.cfg.is_training: fc6 = tf.nn.dropout(fc6, rate=self.cfg.dropout_rate) fc7 = md.fc('fc7', x=fc6, noutputs=4096) if self.cfg.is_training: fc7 = tf.nn.dropout(fc7, rate=1 - self.cfg.dropout_rate) return md.fc('fc8', x=fc7, noutputs=self.cfg.num_classes, relu=False)
from flask import Flask, render_template from modules import convert_to_dict as conv app = Flask(__name__) presidents_List = conv("presidents.csv") # print(presidents_List) @app.route('/') def index(): # heading = '<h1>Welcome to The Presidential Flask Example!</h1>' # test1 = '<p>' + presidents_List[0]['President'] # test2 = ", born in " + presidents_List[0]['Birthplace'] + '.<p>' president_Ids = [] presidentNames = [] for presido in presidents_List: # print(presido) president_Ids.append(presido['Presidency']) presidentNames.append(presido['President']) pairs_list = zip(president_Ids, presidentNames) return render_template('index.html', pairs=pairs_list, the_title="Presidents Index") # return heading + test1 + test2 # return '<h1>Welcome to the presidential Flask example!</h1>' @app.route('/president/<num>') def detail(num): for president in presidents_List:
def __init__(self, batch, FLAGS, embed_dim, vocab_size, char_dim, char_size, rnn_dim, max_turn, max_word_len, max_char_len, \ pretrained_word_embeddings=None, pretrained_char_embeddings=None): self.embed_dim = embed_dim self.char_dim = char_dim self.rnn_dim = rnn_dim self.max_turn = max_turn self.max_word_len = max_word_len self.max_char_len = max_char_len self.context, self.context_mask, self.context_len, \ self.response, self.response_mask, self.response_len, \ self.char_context, self.char_context_mask, self.char_context_len, \ self.char_response, self.char_response_mask, self.char_response_len, self.target = batch.get_next() self.context_mask = tf.to_float(self.context_mask) self.response_mask = tf.to_float(self.response_mask) self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob") self.y_pred = 0.0 self.loss = 0.0 self.loss_list = [] self.expand_response_len = tf.tile( tf.expand_dims(self.response_len, 1), [1, max_turn]) self.expand_response_len = tf.reshape(self.expand_response_len, [-1]) self.expand_response_mask = tf.tile( tf.expand_dims(self.response_mask, 1), [1, max_turn, 1]) self.expand_response_mask = tf.reshape(self.expand_response_mask, [-1, max_word_len]) self.parall_context_len = tf.reshape(self.context_len, [-1]) self.parall_context_mask = tf.reshape(self.context_mask, [-1, max_word_len]) self.all_utterance_mask = tf.unstack(self.context_mask, num=max_turn, axis=1) self.concat_context_mask = tf.concat(self.all_utterance_mask, axis=1) # character-based word representation if FLAGS.use_char: conv_dim = FLAGS.char_hid kernels = [5] with tf.variable_scope("char_embeddings"): char_embeddings = tf.get_variable('char_embeddings_v', shape=(char_size, char_dim), dtype=tf.float32, trainable=True) if pretrained_char_embeddings is not None: self.char_embeddings_init = char_embeddings.assign( pretrained_char_embeddings) response_char_embeddings = tf.nn.embedding_lookup( char_embeddings, self.char_response) response_char_embeddings = tf.reshape( response_char_embeddings, [-1, max_char_len, char_dim]) response_char_embeddings = conv(response_char_embeddings, conv_dim, kernel_size=kernels, bias=True, activation=tf.nn.relu, name="char_conv", isNormalize=False, reuse=None) response_char_embeddings = tf.reduce_max( response_char_embeddings, axis=1) response_char_embeddings = tf.reshape( response_char_embeddings, [ -1, max_word_len, response_char_embeddings.get_shape().as_list()[-1] ]) response_char_embeddings = tf.layers.dropout( response_char_embeddings, rate=1 - self.dropout_keep_prob) self.expand_response_char_embeddings = tf.tile( tf.expand_dims(response_char_embeddings, 1), [1, max_turn, 1, 1]) self.expand_response_char_embeddings = tf.reshape( self.expand_response_char_embeddings, [-1, max_word_len, conv_dim]) context_char_embeddings = tf.nn.embedding_lookup( char_embeddings, self.char_context ) # [batch, max_turn, max_word_len, max_char_len, char_dim] cont_char_embeddings = [] for k, utt_char_emb in enumerate( tf.unstack(context_char_embeddings, axis=1)): utt_char_embeddings = tf.reshape( utt_char_emb, [-1, max_char_len, char_dim]) utt_char_embeddings = conv(utt_char_embeddings, conv_dim, kernel_size=kernels, bias=True, activation=tf.nn.relu, name="char_conv", isNormalize=False, reuse=True) utt_char_embeddings = tf.reduce_max(utt_char_embeddings, axis=1) utt_char_embeddings = tf.reshape(utt_char_embeddings, [ -1, max_word_len, utt_char_embeddings.get_shape().as_list()[-1] ]) cont_char_embeddings.append(utt_char_embeddings) context_char_embeddings = tf.stack(cont_char_embeddings, axis=1) self.parall_context_char_embeddings = tf.reshape( context_char_embeddings, [-1, max_word_len, conv_dim * len(kernels)]) char_interaction = self.interaction_matching_batch( self.parall_context_char_embeddings, self.expand_response_char_embeddings, conv_dim, scope='char_interaction_matching') loss_char = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.target, logits=char_interaction)) self.loss += loss_char self.y_pred += tf.nn.softmax(char_interaction) self.loss_list.append(loss_char) with tf.variable_scope("word_embeddings"): word_embeddings = tf.get_variable('word_embeddings_v', shape=(vocab_size, embed_dim), dtype=tf.float32, trainable=True) if pretrained_word_embeddings is not None: self.embedding_init = word_embeddings.assign( pretrained_word_embeddings) self.context_embeddings = tf.nn.embedding_lookup( word_embeddings, self.context) self.response_embeddings = tf.nn.embedding_lookup( word_embeddings, self.response) self.context_embeddings = tf.layers.dropout( self.context_embeddings, rate=1 - self.dropout_keep_prob) self.response_embeddings = tf.layers.dropout( self.response_embeddings, rate=1 - self.dropout_keep_prob) self.parall_context_embeddings = tf.reshape( self.context_embeddings, [-1, max_word_len, embed_dim]) self.all_utterance_embeddings = tf.unstack(self.context_embeddings, num=max_turn, axis=1) self.expand_response_embeddings = tf.tile( tf.expand_dims(self.response_embeddings, 1), [1, max_turn, 1, 1]) self.expand_response_embeddings = tf.reshape( self.expand_response_embeddings, [-1, max_word_len, embed_dim]) # word representation if FLAGS.use_word: word_interaction = self.interaction_matching_batch( self.parall_context_embeddings, self.expand_response_embeddings, embed_dim, scope='word_interaction_matching') loss_word = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.target, logits=word_interaction)) self.loss += loss_word self.y_pred += tf.nn.softmax(word_interaction) self.loss_list.append(loss_word) # sequential representation if FLAGS.use_seq: with tf.variable_scope("RNN_embeddings"): sentence_gru_cell = tf.contrib.rnn.GRUCell(rnn_dim) with tf.variable_scope('sentence_gru'): self.response_GRU_embeddings, _ = tf.nn.dynamic_rnn( sentence_gru_cell, self.response_embeddings, sequence_length=self.response_len, dtype=tf.float32) with tf.variable_scope('sentence_gru', reuse=True): self.context_GRU_embeddings, _ = tf.nn.dynamic_rnn( sentence_gru_cell, self.parall_context_embeddings, sequence_length=self.parall_context_len, dtype=tf.float32) self.expand_response_GRU_embeddings = tf.tile( tf.expand_dims(self.response_GRU_embeddings, 1), [1, max_turn, 1, 1]) self.expand_response_GRU_embeddings = tf.reshape( self.expand_response_GRU_embeddings, [-1, max_word_len, rnn_dim]) seg_interaction = self.interaction_matching_batch( self.context_GRU_embeddings, self.expand_response_GRU_embeddings, rnn_dim, scope='seg_interaction_matching') loss_seg = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.target, logits=seg_interaction)) self.loss += loss_seg self.y_pred += tf.nn.softmax(seg_interaction) self.loss_list.append(loss_seg) # local representation if FLAGS.use_conv: conv_dim = 50 kernels = [1, 2, 3, 4] with tf.variable_scope("conv_embeddings"): self.response_conv_embeddings = conv(self.response_embeddings, conv_dim, kernel_size=kernels, bias=True, activation=tf.nn.relu, isNormalize=True, reuse=False) self.context_conv_embeddings = conv( self.parall_context_embeddings, conv_dim, kernel_size=kernels, bias=True, activation=tf.nn.relu, isNormalize=True, reuse=True) self.expand_response_conv_embeddings = tf.tile( tf.expand_dims(self.response_conv_embeddings, 1), [1, max_turn, 1, 1]) self.expand_response_conv_embeddings = tf.reshape( self.expand_response_conv_embeddings, [-1, max_word_len, conv_dim * len(kernels)]) conv_interaction = self.interaction_matching_batch( self.context_conv_embeddings, self.expand_response_conv_embeddings, conv_dim * len(kernels), scope='conv_interaction_matching') loss_conv = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.target, logits=conv_interaction)) self.loss += loss_conv self.y_pred += tf.nn.softmax(conv_interaction) self.loss_list.append(loss_conv) # self attention if FLAGS.use_self: with tf.variable_scope("self_att_embeddings"): self.response_self_att_embeddings = multihead_attention( self.response_embeddings, self.response_embeddings, embed_dim, reuse=False) self.context_self_att_embeddings = multihead_attention( self.parall_context_embeddings, self.parall_context_embeddings, embed_dim, reuse=True) self.expand_response_self_att_embeddings = tf.tile( tf.expand_dims(self.response_self_att_embeddings, 1), [1, max_turn, 1, 1]) self.expand_response_self_att_embeddings = tf.reshape( self.expand_response_self_att_embeddings, [-1, max_word_len, embed_dim]) self_att_interaction = self.interaction_matching_batch( self.context_self_att_embeddings, self.expand_response_self_att_embeddings, embed_dim, scope='self_att_interaction_matching') self.y_pred += tf.nn.softmax(self_att_interaction) loss_self = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.target, logits=self_att_interaction)) self.loss += loss_self self.loss_list.append(loss_self) # cross attention if FLAGS.use_cross: with tf.variable_scope("cross_att_embeddings"): expand_response_embeddings = tf.tile( tf.expand_dims(self.response_embeddings, 1), [1, self.max_turn, 1, 1]) expand_response_embeddings = tf.reshape( expand_response_embeddings, [-1, max_word_len, embed_dim]) self.context_cross_att_embeddings = multihead_attention( self.parall_context_embeddings, expand_response_embeddings, rnn_dim, reuse=False) self.response_cross_att_embeddings = [] for k, utterance_embeddings in enumerate( self.all_utterance_embeddings): response_cross_att_embedding = multihead_attention( self.response_embeddings, utterance_embeddings, rnn_dim, reuse=True) self.response_cross_att_embeddings.append( response_cross_att_embedding) self.response_cross_att_embeddings = tf.stack( self.response_cross_att_embeddings, axis=1) self.response_cross_att_embeddings = tf.reshape( self.response_cross_att_embeddings, [-1, max_word_len, embed_dim]) logits = self.interaction_matching_batch( self.context_cross_att_embeddings, self.response_cross_att_embeddings, embed_dim, scope='cross_att_interaction_matching') loss_cross = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.target, logits=logits)) self.loss += loss_cross self.y_pred += tf.nn.softmax(logits) self.loss_list.append(loss_cross) with tf.name_scope("accuracy"): self.correct = tf.equal( tf.cast(tf.argmax(self.y_pred, axis=1), tf.int32), tf.to_int32(self.target)) self.accuracy = tf.reduce_mean(tf.cast(self.correct, 'float'))
def __init__(self, pretrained_word_embeddings=None, pretrained_char_embeddings=None): super(model, self).__init__() self.char_kernels = [2] self.char_emb = torch.nn.Embedding(FLAGS.vocab_char_size, FLAGS.char_embed_dim) if pretrained_char_embeddings is not None: self.char_emb.weight.data.copy_( torch.from_numpy(np.array(pretrained_char_embeddings))) self.char_conv = conv(FLAGS.char_embed_dim, FLAGS.char_hid, self.char_kernels, bias=True, activation=F.relu, isNormalize=False) #.cuda() self.char_dropout = nn.Dropout(FLAGS.dropout_prob).cuda() self.utt_char_conv = conv(FLAGS.char_embed_dim, FLAGS.char_hid, self.char_kernels, bias=True, activation=F.relu, isNormalize=False) #.cuda() self.char_interaction_matching_batch = Interaction_matching_batch( FLAGS.char_hid * len(self.char_kernels), FLAGS.char_hid) #.cuda() # word self.word_embeddings = nn.Embedding(FLAGS.vocab_size, FLAGS.embed_dim) #.cuda() if pretrained_word_embeddings is not None: self.word_embeddings.weight.data.copy_( torch.from_numpy(np.array(pretrained_word_embeddings))) self.word_context_dropout = nn.Dropout(FLAGS.dropout_prob) #.cuda() self.word_response_dropout = nn.Dropout(FLAGS.dropout_prob) #.cuda() self.word_interaction_matching_batch = Interaction_matching_batch( FLAGS.embed_dim, FLAGS.embed_dim) #.cuda() # seq self.sentence_gru_cell = nn.GRU(FLAGS.embed_dim, FLAGS.rnn_dim).cuda() self.seq_interaction_matching_batch = Interaction_matching_batch( FLAGS.rnn_dim, FLAGS.rnn_dim) #.cuda() # conv conv_dim = 50 self.conv_kernels = [1, 2, 3] self.conv = conv(FLAGS.embed_dim, conv_dim, kernel_size=self.conv_kernels, bias=True, activation=F.relu, isNormalize=True) #.cuda() self.conv_interaction_matching_batch = Interaction_matching_batch( conv_dim * len(self.conv_kernels), conv_dim * len(self.conv_kernels)) #.cuda() # self attention self.self_multihead_attention = MultiHeadAttention( FLAGS.embed_dim, 12) #.cuda() self.self_interaction_matching_batch = Interaction_matching_batch( FLAGS.embed_dim, FLAGS.embed_dim) #.cuda() # cross attention self.cross_multihead_attention = MultiHeadAttention( FLAGS.embed_dim, 12) #.cuda() self.cross_interaction_matching_batch = Interaction_matching_batch( FLAGS.embed_dim, FLAGS.embed_dim) #.cuda()
def load_net(self, x): """ Builds model """ conv1_1 = md.conv('conv1_1', x=x, fsize=[3, 3], nfilters=64) #default padding='SAME', stride=1 conv1_2 = md.conv('conv1_2', x=conv1_1, fsize=[3, 3], nfilters=64) pool1 = md.maxpool('pool1', x=conv1_2, fsize=2, stride=2) conv2_1 = md.conv('conv2_1', x=pool1, fsize=[3, 3], nfilters=128) conv2_2 = md.conv('conv2_2', x=conv2_1, fsize=[3, 3], nfilters=128) pool2 = md.maxpool('pool2', x=conv2_2, fsize=2, stride=2) conv3_1 = md.conv('conv3_1', x=pool2, fsize=[3, 3], nfilters=256) conv3_2 = md.conv('conv3_2', x=conv3_1, fsize=[3, 3], nfilters=256) conv3_3 = md.conv('conv3_3', x=conv3_2, fsize=[3, 3], nfilters=256) pool3 = md.maxpool('pool3', x=conv3_3, fsize=2, stride=2) conv4_1 = md.conv('conv4_1', x=pool3, fsize=[3, 3], nfilters=512) conv4_2 = md.conv('conv4_2', x=conv4_1, fsize=[3, 3], nfilters=512) conv4_3 = md.conv('conv4_3', x=conv4_2, fsize=[3, 3], nfilters=512) pool4 = md.maxpool('pool4', x=conv4_3, fsize=2, stride=2) conv5_1 = md.conv('conv5_1', x=pool4, fsize=[3, 3], nfilters=512) conv5_2 = md.conv('conv5_2', x=conv5_1, fsize=[3, 3], nfilters=512) conv5_3 = md.conv('conv5_3', x=conv5_2, fsize=[3, 3], nfilters=512) pool5 = md.maxpool('pool5', x=conv5_3, fsize=2, stride=2) # FIX THIS DESIGN flat = md.flatten(x=pool5) fc6 = md.fc('fc6', x=flat, noutputs=4096, binit_val=1.0) if self.cfg.is_training: fc6 = tf.nn.dropout(fc6, rate=1 - self.cfg.dropout_rate) fc7 = md.fc('fc7', x=fc6, noutputs=4096, binit_val=1.0) if self.cfg.is_training: fc7 = tf.nn.dropout(fc7, rate=1 - self.cfg.dropout_rate) return md.fc('fc8', x=fc7, noutputs=self.cfg.num_classes, relu=False)