def save_test_data(y1, y2, y3, i): sen_y1 = data_processor.getSentence(y1, vocab)[0] sen_y2 = data_processor.getSentence(y2, vocab)[0] sen_y3 = data_processor.getSentence(y3, vocab) data_processor.saveData('\nQuestion ' + str(i + 1) + ':\n' + sen_y1) data_processor.saveData('\nPositive Answer:\n' + sen_y2) data_processor.saveData('\nNegative Answers:') for j in range(4): data_processor.saveData('\n' + str(j + 1) + ' ' + sen_y3[j]) return
def save_train_data(x1, x2, x3): sen_x1 = data_processor.getSentence(x1, vocab) sen_x2 = data_processor.getSentence(x2, vocab) sen_x3 = data_processor.getSentence(x3, vocab) for j in range(4): data_processor.saveData('\nQuestion ' + str(j + 1) + ':\n' + sen_x1[j]) data_processor.saveData('\nPositive Answer' + ':\n' + sen_x2[j]) data_processor.saveData('\nNegative Answer' + ':\n' + sen_x3[j]) return
def save_data_losses(x1, x2, x3, losses): sen_x1 = data_processor.getSentence(x1, vocab) sen_x2 = data_processor.getSentence(x2, vocab) sen_x3 = data_processor.getSentence(x3, vocab) # print (np.shape(losses),losses) num = 0 for k in range(len(losses)): # print ("losses", np.shape(losses), type(losses)) if (losses[k] != 0.0): data_processor.saveData('\nQuestion_wrong ' + str(num + 1) + ':\n' + sen_x1[k]) data_processor.saveData('\nPositive Answer' + ':\n' + sen_x2[k]) data_processor.saveData('\nNegative Answer' + ':\n' + sen_x3[k]) num += 1 if (num == 4): return return
def show(sess, vocab, file_path, sequence_length, ratio): batch_y1, batch_y2, batch_y3, flag_list = data_processor.loadValData( vocab, file_path, sequence_length, ratio) # batch_size*seq_len with tf.device('/cpu:0'), tf.name_scope("embedding"): embedded_chars1 = tf.nn.embedding_lookup(w_embedding, batch_y1) embedded_chars2 = tf.nn.embedding_lookup(w_embedding, batch_y2) embedded_chars3 = tf.nn.embedding_lookup(w_embedding, batch_y3) embedded_chars1_expanded = tf.expand_dims( embedded_chars1, -1 ) # add a dim at the end of the variable. input of image conv has 4 dims. embedded_chars2_expanded = tf.expand_dims(embedded_chars2, -1) embedded_chars3_expanded = tf.expand_dims(embedded_chars3, -1) pooled_outputs1 = [] pooled_outputs2 = [] pooled_outputs3 = [] for i in range((len(filter_sizes))): conv = tf.nn.conv2d(embedded_chars1_expanded, w_conv[i], strides=[1, 1, 1, 1], padding="VALID", name="conv") h = tf.nn.relu(tf.nn.bias_add(conv, b_conv[i]), name="relu") # print ("h", h.get_shape()) pooled = tf.nn.max_pool( h, ksize=[1, sequence_length - filter_sizes[i] + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name="pool") # shape of pooled is [batch_size,1,1,num_filters] # print ("pooled_outputs"+ str(i), pooled.get_shape()) pooled_outputs1.append(pooled) conv = tf.nn.conv2d(embedded_chars2_expanded, w_conv[i], strides=[1, 1, 1, 1], padding="VALID", name="conv") h = tf.nn.relu(tf.nn.bias_add(conv, b_conv[i]), name="relu") pooled = tf.nn.max_pool( h, ksize=[1, sequence_length - filter_sizes[i] + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name="pool") # shape of pooled is [batch_size,1,1,num_filters] pooled_outputs2.append(pooled) conv = tf.nn.conv2d(embedded_chars3_expanded, w_conv[i], strides=[1, 1, 1, 1], padding="VALID", name="conv") # print('\n--- shape of cov is {}'.format(conv.get_shape())) # Apply nonlinearity h = tf.nn.relu(tf.nn.bias_add(conv, b_conv[i]), name="relu") # Max-pooling over the outputs pooled = tf.nn.max_pool( h, ksize=[1, sequence_length - filter_sizes[i] + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID', name="pool") # shape of pooled is [batch_size,1,1,num_filters] pooled_outputs3.append(pooled) # reshape the outputs to combine all the pooled features num_filters_total = num_filters * len(filter_sizes) # print ("pooled_outputs1",len(pooled_outputs1), pooled_outputs1[0].get_shape(),pooled_outputs1[1].get_shape(),pooled_outputs1[2].get_shape(),pooled_outputs1[3].get_shape()) h_pooled1 = tf.concat(pooled_outputs1, 3) # the 4th dim corresponds to num_filters h_pooled1_flat = tf.reshape(h_pooled1, [-1, num_filters_total]) # self.h_pooled1_flat = tf.nn.dropout(self.h_pooled1_reshape,1.0) # print('\n--- shape of h_pooled1_flat {}'.format(self.h_pooled1_flat.get_shape())) h_pooled2 = tf.concat(pooled_outputs2, 3) h_pooled2_flat = tf.reshape(h_pooled2, [-1, num_filters_total]) # self.h_pooled2_flat = tf.nn.dropout(self.h_pooled2_reshape,1.0) h_pooled3 = tf.concat(pooled_outputs3, 3) h_pooled3_flat = tf.reshape(h_pooled3, [-1, num_filters_total]) # self.h_pooled3_flat = tf.nn.dropout(self.h_pooled3_reshape,1.0) len_pooled1 = tf.sqrt( tf.reduce_sum(tf.multiply(h_pooled1_flat, h_pooled1_flat), 1)) # length of quesiton vectors # print('\n--- shape of len_pooled1 {}'.format(len_pooled1.get_shape())) len_pooled2 = tf.sqrt( tf.reduce_sum(tf.multiply(h_pooled2_flat, h_pooled2_flat), 1)) # length of positive answer vectors len_pooled3 = tf.sqrt( tf.reduce_sum(tf.multiply(h_pooled3_flat, h_pooled3_flat), 1)) # length of negative answer vectors mul_12 = tf.reduce_sum(tf.multiply(h_pooled1_flat, h_pooled2_flat), 1) # wisely multiple vectors, 向量的点积 # print('\n--- shape of mul_12 {}'.format(mul_12.get_shape())) mul_13 = tf.reduce_sum(tf.multiply(h_pooled1_flat, h_pooled3_flat), 1) # output cos_12 = tf.div( mul_12, tf.multiply(len_pooled1, len_pooled2), name="scores") # computes the angle between the two vectors cos_13 = tf.div(mul_13, tf.multiply(len_pooled1, len_pooled3)) max_idx = tf.argmax(cos_13, 0) pos = sess.run(cos_12) neg = sess.run(cos_13) max_idx = sess.run(max_idx) # tmp1 = neg[max_idx] if (pos[0] > neg[max_idx]): print('\033[1;31;34m') print "Correct prediction" print "Question:", '\033[0m' print data_processor.getSentence(batch_y1, vocab)[0], '\033[1;31;34m' print "Correct Answer:", '\033[0m' print data_processor.getSentence(batch_y2, vocab)[0], '\033[1;31;34m' print "Predicted Answer:", '\033[0m' print data_processor.getSentence(batch_y2, vocab)[0], '\033[0m' else: print('\033[1;31;34m') print "False prediction" print "Question:", '\033[0m' print data_processor.getSentence(batch_y1, vocab)[0], '\033[1;31;34m' print "Correct Answer:", '\033[0m' print data_processor.getSentence(batch_y2, vocab)[0], '\033[1;31;34m' print "Predicted Answer:", '\033[0m' print data_processor.getSentence(batch_y3, vocab)[max_idx], '\033[0m'