Пример #1
0
 def build(self, guidence, newNet):
     with tf.variable_scope("training_variable"):
         inputEmb = tf.nn.embedding_lookup(self.embedding, self.X)
         initFw = tf.nn.rnn_cell.LSTMStateTuple(
             tf.nn.relu(
                 tf.matmul(guidence, self.weights["Fw1"]) +
                 self.biases["Fw1"]),
             tf.nn.relu(
                 tf.matmul(guidence, self.weights["Fw2"]) +
                 self.biases["Fw2"]))
         initBw = tf.nn.rnn_cell.LSTMStateTuple(
             tf.nn.relu(
                 tf.matmul(guidence, self.weights["Bw1"]) +
                 self.biases["Bw1"]),
             tf.nn.relu(
                 tf.matmul(guidence, self.weights["Bw2"]) +
                 self.biases["Bw2"]))
         rnnCellFw = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
             tf.nn.rnn_cell.BasicLSTMCell(self.nHidden),
             input_keep_prob=self.pKeep,
             output_keep_prob=1.0)
         rnnCellBw = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
             tf.nn.rnn_cell.BasicLSTMCell(self.nHidden),
             input_keep_prob=self.pKeep,
             output_keep_prob=1.0)
         outputs, state = tf.nn.bidirectional_dynamic_rnn(
             cell_fw=rnnCellFw,
             cell_bw=rnnCellBw,
             inputs=inputEmb,
             initial_state_fw=initFw,
             initial_state_bw=initBw,
             dtype=tf.float32)
         outputsConcat = tf.concat(outputs, axis=2)
         self.outputs = outputsConcat
         self.RNNState = tf.reduce_mean(outputsConcat, axis=1)
Пример #2
0
 def build_layer(s, c_names, n_l1, n_l2, w_initializer, b_initializer):
     with tf.variable_scope('l1'):
         w1 = tf.get_variable('w1', [self.n_features, n_l1],
                              initializer=w_initializer,
                              collections=c_names)
         b1 = tf.get_variable('b1', [1, n_l1],
                              initializer=b_initializer,
                              collections=c_names)
         l1 = tf.nn.relu(tf.matmul(s, w1) + b1)
     with tf.variable_scope('l2'):
         w2 = tf.get_variable('w2', [n_l1, n_l2],
                              initializer=w_initializer,
                              collections=c_names)
         b2 = tf.get_variable('b2', [1, n_l2],
                              initializer=b_initializer,
                              collections=c_names)
         l2 = tf.nn.relu(tf.matmul(l1, w2) + b2)
     with tf.variable_scope('l3'):
         w3 = tf.get_variable('w3', [n_l2, self.n_actions],
                              initializer=w_initializer,
                              collections=c_names)
         b3 = tf.get_variable('b3', [1, self.n_actions],
                              initializer=b_initializer,
                              collections=c_names)
         l3 = tf.nn.relu(tf.matmul(l2, w3) + b3)
     return l3
 def generator(x, m):
     # Concatenate Mask and Data
     inputs = tf.concat(values=[x, m], axis=1)
     G_h1 = tf.nn.relu(tf.matmul(inputs, G_W1) + G_b1)
     G_h2 = tf.nn.relu(tf.matmul(G_h1, G_W2) + G_b2)
     # MinMax normalized output
     G_prob = tf.nn.sigmoid(tf.matmul(G_h2, G_W3) + G_b3)
     return G_prob
 def discriminator(x, h):
     # Concatenate Data and Hint
     inputs = tf.concat(values=[x, h], axis=1)
     D_h1 = tf.nn.relu(tf.matmul(inputs, D_W1) + D_b1)
     D_h2 = tf.nn.relu(tf.matmul(D_h1, D_W2) + D_b2)
     D_logit = tf.matmul(D_h2, D_W3) + D_b3
     D_prob = tf.nn.sigmoid(D_logit)
     return D_prob
Пример #5
0
    def multihead_attn(q, k, v):
        # q, k, v have shape [batch, heads, sequence, features]
        w = tf.matmul(q, k, transpose_b=True)
        # w = w * tf.rsqrt(tf.cast(v.shape[-1].value, w.dtype))
        w = w * tf.rsqrt(tf.cast(v.shape[-1], w.dtype))

        w = mask_attn_weights(w)
        w = softmax(w)
        a = tf.matmul(w, v)
        return a
Пример #6
0
def model(hparams, X, past=None, scope='model', reuse=tf.AUTO_REUSE):
    with tf.variable_scope(scope, reuse=reuse):
        results = {}
        batch, sequence = shape_list(X)

        wpe = tf.get_variable('wpe', [hparams.n_ctx, hparams.n_embd],
                              initializer=tf.random_normal_initializer(stddev=0.01))
        wte = tf.get_variable('wte', [hparams.n_vocab, hparams.n_embd],
                              initializer=tf.random_normal_initializer(stddev=0.02))
        past_length = 0 if past is None else tf.shape(past)[-2]
        h = tf.gather(wte, X) + tf.gather(wpe, positions_for(X, past_length))

        # Transformer
        presents = []
        pasts = tf.unstack(past, axis=1) if past is not None else [None] * hparams.n_layer
        assert len(pasts) == hparams.n_layer
        for layer, past in enumerate(pasts):
            h, present = block(h, 'h%d' % layer, past=past, hparams=hparams)
            if layer == 10:
                tf.add_to_collection('checkpoints', h)
            presents.append(present)
        results['present'] = tf.stack(presents, axis=1)
        h = norm(h, 'ln_f')

        # Language model loss.  Do tokens <n predict token n?
        h_flat = tf.reshape(h, [batch * sequence, hparams.n_embd])
        logits = tf.matmul(h_flat, wte, transpose_b=True)
        logits = tf.reshape(logits, [batch, sequence, hparams.n_vocab])
        results['logits'] = logits
        return results
Пример #7
0
def pca(x, dim=2):
    '''
        x:输入矩阵
        dim:降维之后的维度数
    '''
    with tf.name_scope("PCA"):

        m, n = tf.to_float(x.get_shape()[0]), tf.to_int32(x.get_shape()[1])
        assert not tf.assert_less(dim, n)
        mean = tf.reduce_mean(x, axis=1)
        x_new = x - tf.reshape(mean, (-1, 1))
        cov = tf.matmul(x_new, x_new, transpose_a=True) / (m - 1)
        e, v = tf.linalg.eigh(cov, name="eigh")
        e_index_sort = tf.math.top_k(e, sorted=True, k=dim)[1]
        v_new = tf.gather(v, indices=e_index_sort)
        pca = tf.matmul(x_new, v_new, transpose_b=True)
    return pca
Пример #8
0
def conv1d(x, scope, nf, *, w_init_stdev=0.02):
    with tf.variable_scope(scope):
        *start, nx = shape_list(x)
        w = tf.get_variable('w', [1, nx, nf],
                            initializer=tf.random_normal_initializer(stddev=w_init_stdev))
        b = tf.get_variable('b', [nf], initializer=tf.constant_initializer(0))
        c = tf.reshape(tf.matmul(tf.reshape(x, [-1, nx]), tf.reshape(w, [-1, nf])) + b,
                       start + [nf])
        return c
Пример #9
0
 def predicting(self, rate):
     hidden = tf.nn.relu(
         tf.matmul(self.concatInput, self.weights["MLP1"]) +
         self.biases["MLP1"])
     logits = tf.matmul(hidden, self.weights["MLP2"]) + self.biases["MLP2"]
     predictPossibility = tf.nn.sigmoid(logits)
     accuracy = tf.reduce_mean(
         tf.cast(
             tf.equal(tf.cast(predictPossibility > 0.5, tf.float32),
                      self.y), tf.float32))
     loss = tf.reduce_mean(
         tf.nn.weighted_cross_entropy_with_logits(targets=self.y,
                                                  logits=logits,
                                                  pos_weight=rate))
     tv = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                            'training_variable')
     l2_loss = self.l2_para * tf.reduce_sum([tf.nn.l2_loss(v) for v in tv])
     loss += l2_loss
     return loss, accuracy, predictPossibility
Пример #10
0
def _build_layer(input_dim,
                 output_dim,
                 input_data,
                 c_name,
                 layer_name,
                 w_name='w',
                 bias_name='b',
                 has_activate=True):
    with tf.variable_scope(layer_name):
        w = tf.get_variable(w_name, [input_dim, output_dim],
                            dtype=tf.float32,
                            initializer=tf.truncated_normal_initializer(),
                            collections=c_name)
        b = tf.get_variable(bias_name, [output_dim],
                            dtype=tf.float32,
                            initializer=tf.truncated_normal_initializer(),
                            collections=c_name)
        if has_activate:
            l = tf.nn.relu(tf.add(tf.matmul(input_data, w), b))
        else:
            l = tf.add(tf.matmul(input_data, w), b)
    return l
Пример #11
0
 def choose_action(self, state, epsilon=1.):
     # 计算q值. epsilon为随机度. 为1时, 则为全随机选择action
     M_s = np.zeros([self.n_action, self.fai_s_size])
     for i in range(self.n_action):
         M_s[i] = self.sess.run(self.eval_M[i],
                                feed_dict={self.state, state})
     w = self.sess.graph.get_tensor_by_name('eval_net/R_s/r/w:0')
     # fai = self.sess.graph.get_tensor_by_name('eval_net/f_theta/fai_s:0')
     q_ = tf.matmul(w, tf.Variable(M_s))
     if np.random.uniform(0, 1) < epsilon:
         action = np.random.randint(self.n_action)
     else:
         action = np.argmax(q_)
     return action
Пример #12
0
 def __init__(self):
     self.embedding = self.getEmb()
     self.embSize = self.embedding.shape[1]
     self.vocabSize = self.embedding.shape[0]
     self.x = tf.placeholder(tf.int32, [None, 5])
     with tf.variable_scope("training_variable"):
         self.weights = {
             "MLP1":
             tf.Variable(
                 tf.truncated_normal(
                     shape=[self.embSize,
                            int(self.embSize / 2)],
                     stddev=0.08)),
             "MLP2":
             tf.Variable(
                 tf.truncated_normal(shape=[int(self.embSize / 2), 1],
                                     stddev=0.08))
         }
         self.biases = {
             "MLP1":
             tf.Variable(
                 tf.constant(0.01,
                             shape=[int(self.embSize / 2)],
                             dtype=tf.float32)),
             "MLP2":
             tf.Variable(tf.constant(0.01, shape=[1], dtype=tf.float32))
         }
     self.inputEmb = tf.nn.embedding_lookup(self.embedding, self.x)
     p1 = tf.matmul(tf.reshape(self.inputEmb, [-1, self.embSize]),
                    self.weights["MLP1"]) + self.biases["MLP1"]
     p1 = tf.matmul(tf.nn.relu(p1),
                    self.weights["MLP2"]) + self.biases["MLP2"]
     p1 = tf.reshape(p1, [-1, 5])
     p1 = tf.reshape(tf.nn.softmax(p1), [-1, 1, 5])
     self.finalState = tf.reshape(tf.matmul(p1, self.inputEmb),
                                  [-1, self.embSize])
Пример #13
0
    def __fully_connected(self,
                          input,
                          inputs_count,
                          outputs_count,
                          relu=True,
                          init_biases_with_the_constant_1=False,
                          name='fully_connected'):
        with tf.name_scope(name):
            wights = tf.Variable(
                self.__random_values(shape=[inputs_count, outputs_count]),
                name='wights')
            if init_biases_with_the_constant_1:
                biases = tf.Variable(tf.ones(shape=[outputs_count],
                                             dtype=tf.float32),
                                     name='biases')
            else:
                biases = tf.Variable(tf.zeros(shape=[outputs_count],
                                              dtype=tf.float32),
                                     name='biases')
            preactivations = tf.nn.bias_add(tf.matmul(input, wights),
                                            biases,
                                            name='preactivations')
            if relu:
                activations = tf.nn.relu(preactivations, name='activations')

            with tf.name_scope('wight_summaries'):
                self.__variable_summaries(wights)

            with tf.name_scope('bias_summaries'):
                self.__variable_summaries(biases)

            with tf.name_scope('preactivations_histogram'):
                tf.summary.histogram('preactivations', preactivations)

            if relu:
                with tf.name_scope('activations_histogram'):
                    tf.summary.histogram('activations', activations)

            if relu:
                return activations
            else:
                return preactivations
Пример #14
0
def train(x_train, y_train):
    n_samples, n_features = x_train.shape

    w = tf.Variable(np.random.rand(input_dim, 1).astype(dtype='float32'),
                    name="weight")
    b = tf.Variable(0.0, dtype=tf.float32, name="bias")

    x = tf.placeholder(dtype=tf.float32, name='x')
    y = tf.placeholder(dtype=tf.float32, name='y')

    predictions = tf.matmul(x, w) + b
    loss = tf.reduce_mean(
        tf.log(1 + tf.exp(tf.multiply(-1.0 * y, predictions))))

    # optimizer = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss)
    optimizer = tf.train.ProximalGradientDescentOptimizer(
        learning_rate=learn_rate,
        l1_regularization_strength=0.1).minimize(loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        for epoch in range(n_epochs):
            for idx in range(0, n_samples, batch_size):
                iE = min(n_samples, idx + batch_size)
                x_batch = x_train[idx:iE, :]
                y_batch = y_train[idx:iE, :]
                sess.run([optimizer], feed_dict={x: x_batch, y: y_batch})
            curr_w, curr_b = sess.run([w, b])

            for idx in range(len(curr_w)):
                if curr_w[idx] < threshold * -1:
                    curr_w[idx] += threshold
                else:
                    curr_w[idx] -= threshold
            sess.run([tf.assign(w, curr_w)])
    return curr_w, curr_b
Пример #15
0
 def Attention(self, g0, sequence, g1, g2, AttStr, AttStr2, length, flag):
     tmpLS = []
     tmpLS2 = []
     tmpLS3 = []
     if not flag:
         seq = tf.transpose(sequence, [1, 0, 2])
     else:
         seq = sequence
     for i in range(length):
         nHidden = tf.tanh(
             tf.matmul(tf.concat([seq[i], g1], axis=1), self.weights[
                 AttStr + "_1"]) + self.biases[AttStr + "_1"])
         nHidden2 = tf.tanh(
             tf.matmul(tf.concat([seq[i], g2], axis=1), self.weights[
                 AttStr + "_2"]) + self.biases[AttStr + "_2"])
         nHidden3 = tf.tanh(
             tf.matmul(tf.concat([seq[i], g0], axis=1), self.weights[
                 AttStr + "_3"]) + self.biases[AttStr + "_3"])
         tmpLS.append(
             tf.matmul(nHidden, self.weights[AttStr2 + "_1"]) +
             self.biases[AttStr2 + "_1"])
         tmpLS2.append(
             tf.matmul(nHidden2, self.weights[AttStr2 + "_2"]) +
             self.biases[AttStr2 + "_2"])
         tmpLS3.append(
             tf.matmul(nHidden3, self.weights[AttStr2 + "_3"]) +
             self.biases[AttStr2 + "_3"])
     tmpLS = tf.nn.softmax(tmpLS, dim=0)
     tmpLS2 = tf.nn.softmax(tmpLS2, dim=0)
     tmpLS3 = tf.nn.softmax(tmpLS3, dim=0)
     self.representation_score[AttStr] = (tmpLS + tmpLS2 + tmpLS3) / 3
     ret = tmpLS[0] * seq[0] / 3 + tmpLS2[0] * seq[0] / 3 + tmpLS3[0] * seq[
         0] / 3
     for i in range(1, length):
         ret += tmpLS[i] * seq[i] / 3 + tmpLS2[i] * seq[i] / 3 + tmpLS3[
             i] * seq[i] / 3
     return ret
Пример #16
0
def main(trainModel=True,
         buildConfusionMatrix=True,
         restore=False,
         buildClassifiedMatrix=True):

    tf.disable_v2_behavior()

    input_images = tf.placeholder(tf.float32, [None, 28, 28], name="Input")
    real = tf.placeholder(tf.float32, [None, CLASSES], name="real_classes")

    layer1 = create_conv_layer(tf.reshape(input_images, [-1, 28, 28, 1]),
                               1,
                               28, [5, 5], [2, 2],
                               name="conv_no_pool")
    layer2 = create_conv_layer(layer1,
                               28,
                               56, [5, 5], [2, 2],
                               name='conv_with_pool')
    conv_result = tf.reshape(layer2, [-1, 7 * 7 * 56])

    relu_layer_weight = tf.Variable(tf.truncated_normal([7 * 7 * 56, 1000],
                                                        stddev=STDDEV * 2),
                                    name='relu_layer_weight')
    rely_layer_bias = tf.Variable(tf.truncated_normal([1000],
                                                      stddev=STDDEV / 2),
                                  name='rely_layer_bias')
    relu_layer = tf.matmul(conv_result, relu_layer_weight) + rely_layer_bias
    relu_layer = tf.nn.relu(relu_layer)
    relu_layer = tf.nn.dropout(relu_layer, DROPOUT)

    final_layer_weight = tf.Variable(tf.truncated_normal([1000, CLASSES],
                                                         stddev=STDDEV * 2),
                                     name='final_layer_weight')
    final_layer_bias = tf.Variable(tf.truncated_normal([CLASSES],
                                                       stddev=STDDEV / 2),
                                   name='final_layer_bias')
    final_layer = tf.matmul(relu_layer, final_layer_weight) + final_layer_bias

    predicts = tf.nn.softmax(final_layer)
    predicts_for_log = tf.clip_by_value(predicts, 1e-9, 0.999999999)

    #crossEntropy = -tf.reduce_mean(tf.reduce_sum(y * tf.log(y_clipped) + (1 - y) * tf.log(1 - y_clipped), axis=1))

    loss = -tf.reduce_mean(
        tf.reduce_sum(real * tf.log(predicts_for_log) +
                      (1 - real) * tf.log(1 - predicts_for_log),
                      axis=1),
        axis=0)
    #test = tf.reduce_sum(real * tf.log(predicts_for_log) + (1 - real) * tf.log(1 - predicts_for_log), axis=1)
    #loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=final_layer, labels=real))
    optimiser = tf.train.GradientDescentOptimizer(
        learning_rate=LEARNING_RATE).minimize(loss)

    correct_prediction = tf.equal(tf.argmax(real, axis=1),
                                  tf.argmax(predicts, axis=1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    confusion_matrix = tf.confusion_matrix(labels=tf.argmax(real, axis=1),
                                           predictions=tf.argmax(predicts,
                                                                 axis=1),
                                           num_classes=CLASSES)

    saver = tf.train.Saver()

    # dataset = get_mnist_dataset()
    dataset = get_fashion_dataset()

    with tf.Session() as session:

        session.run(tf.global_variables_initializer())

        if restore:
            saver.restore(session, SAVE_PATH)

        if trainModel:
            train(input_images, real, session, optimiser, loss, accuracy,
                  saver, dataset)

        if buildConfusionMatrix:
            test_cm = session.run(confusion_matrix,
                                  feed_dict={
                                      input_images: dataset.test_x,
                                      real: dataset.test_y
                                  })
            draw_confusion_matrix(test_cm)

        if buildClassifiedMatrix:
            all_probs = session.run(predicts,
                                    feed_dict={
                                        input_images: dataset.test_x,
                                        real: dataset.test_y
                                    })
            max_failure_picture_index = [[(-1, -1.0)] * CLASSES
                                         for _ in range(CLASSES)]
            for i in range(len(all_probs)):
                real = np.argmax(dataset.test_y[i])
                for j in range(CLASSES):
                    if max_failure_picture_index[real][j][1] < all_probs[i][j]:
                        max_failure_picture_index[real][j] = (i,
                                                              all_probs[i][j])
            draw_max_failure_pictures(dataset.test_x,
                                      max_failure_picture_index)
import tensorflow._api.v2.compat.v1 as tf
import numpy as np
tf.reset_default_graph()
tf.compat.v1.disable_eager_execution()
tf.compat.v1.disable_v2_behavior()
tf.global_variables_initializer()
x = tf.placeholder(tf.float32, shape=[None, 4])
y = tf.placeholder(tf.float32, shape=[None, 1])
w = tf.Variable(tf.random_normal([4, 1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")
hypo = tf.matmul(x, w) + b
saver = tf.train.Saver()
test_arr = [[12, 6.5, 15.7, 10.8]]
sess2 = tf.Session()
saver.restore(sess2, "./saved.ckpt")
predict = sess2.run(hypo, feed_dict={x: test_arr})
print(predict[0])
Пример #18
0
# Import data
# from tensorflow.examples.tutorials.mnist import input_data
import input_data

mnist = input_data.read_data_sets("tmp/data/", one_hot=True)

g = tf.Graph()

with g.as_default():
    # Create the model
    # x = tf.placeholder("float", [None, 784])
    x = tf.compat.v1.placeholder("float", [None, 784])
    W = tf.Variable(tf.zeros([784, 10]), name="vaiable_W")
    b = tf.Variable(tf.zeros([10]), name="variable_b")
    y = tf.nn.softmax(tf.matmul(x, W) + b)

    # Define loss and optimizer
    y_ = tf.compat.v1.placeholder("float", [None, 10])
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
        cross_entropy)

    sess = tf.Session()

    # Train
    init = tf.initialize_all_variables()
    sess.run(init)

    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
Пример #19
0
# Create a simple TF Graph 
# By Omid Alemi - Jan 2017
# Works with TF r1.0

# import tensorflow as tf
import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()

I = tf.placeholder(tf.float32, shape=[None,3], name='I') # input
W = tf.Variable(tf.zeros(shape=[3,2]), dtype=tf.float32, name='W') # weights
b = tf.Variable(tf.zeros(shape=[2]), dtype=tf.float32, name='b') # biases
O = tf.nn.relu(tf.matmul(I, W) + b, name='O') # activation / output

saver = tf.train.Saver()
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init_op)
  
  # save the graph
  tf.train.write_graph(sess.graph_def, '.', 'tfdroid.pbtxt')  

  # normally you would do some training here
  # but fornow we will just assign something to W
  sess.run(tf.assign(W, [[1, 2],[4,5],[7,8]]))
  sess.run(tf.assign(b, [1,1]))

  #save a checkpoint file, which will store the above assignment  
  saver.save(sess, 'tfdroid.ckpt')
  
Пример #20
0
    def __init__(self, nHidden, seqLen):
        self.representation_score = {}
        self.y = tf.placeholder(tf.float32, shape=[None, 1])
        self.extractFeature = ExtractFeature.ExtractFeature()
        self.imageFeature = ImageFeature.ImageFeature()
        newNet = tf.reduce_mean(self.imageFeature.outputLS, axis=0)
        self.textFeature = TextFeature.TextFeature(
            nHidden, seqLen, self.extractFeature.finalState, newNet)
        self.l2_para = 1e-7
        with tf.variable_scope("training_variable"):

            self.weights = {
                "MLP1":
                tf.Variable(
                    tf.truncated_normal(shape=[512, 256],
                                        stddev=0.08,
                                        name="MLP1_W")),
                "MLP2":
                tf.Variable(
                    tf.truncated_normal(shape=[256, 1],
                                        stddev=0.08,
                                        name="MLP2_W")),
                "ATT_attr1_1":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.imageFeature.defaultFeatureSize +
                        self.extractFeature.embSize,
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.extractFeature.embSize / 2)
                    ],
                                        stddev=0.08,
                                        name="ATT_attr1_1")),
                "ATT_attr1_2":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.textFeature.nHidden * 2 +
                        self.extractFeature.embSize,
                        int(self.textFeature.nHidden +
                            self.extractFeature.embSize / 2)
                    ],
                                        stddev=0.08,
                                        name="ATT_attr1_2")),
                "ATT_attr1_3":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        2 * self.extractFeature.embSize,
                        self.extractFeature.embSize
                    ],
                                        stddev=0.08,
                                        name="ATT_attr1_3")),
                "ATT_attr2_1":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.extractFeature.embSize / 2), 1
                    ],
                                        stddev=0.08,
                                        name="ATT_attr2_1")),
                "ATT_attr2_2":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        int(self.textFeature.nHidden +
                            self.extractFeature.embSize / 2), 1
                    ],
                                        stddev=0.08,
                                        name="ATT_attr2_2")),
                "ATT_attr2_3":
                tf.Variable(
                    tf.truncated_normal(shape=[self.extractFeature.embSize, 1],
                                        stddev=0.08,
                                        name="ATT_attr2_3")),
                "ATT_img1_1":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.imageFeature.defaultFeatureSize +
                        self.textFeature.nHidden * 2,
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.textFeature.nHidden)
                    ],
                                        stddev=0.08,
                                        name="ATT_image1_1")),
                "ATT_img1_2":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.imageFeature.defaultFeatureSize +
                        self.extractFeature.embSize,
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.extractFeature.embSize / 2)
                    ],
                                        stddev=0.08,
                                        name="ATT_image1_2")),
                "ATT_img1_3":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.imageFeature.defaultFeatureSize * 2,
                        self.imageFeature.defaultFeatureSize
                    ],
                                        stddev=0.08,
                                        name="ATT_image1_3")),
                "ATT_img2_1":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.textFeature.nHidden), 1
                    ],
                                        stddev=0.08,
                                        name="ATT_image2_1")),
                "ATT_img2_2":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.extractFeature.embSize / 2), 1
                    ],
                                        stddev=0.08,
                                        name="ATT_image2_2")),
                "ATT_img2_3":
                tf.Variable(
                    tf.truncated_normal(
                        shape=[self.imageFeature.defaultFeatureSize, 1],
                        stddev=0.08,
                        name="ATT_image2_3")),
                "ATT_text1_1":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.imageFeature.defaultFeatureSize +
                        self.textFeature.nHidden * 2,
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.textFeature.nHidden)
                    ],
                                        stddev=0.08,
                                        name="ATT_text1_1")),
                "ATT_text1_2":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.textFeature.nHidden * 2 +
                        self.extractFeature.embSize,
                        int(self.textFeature.nHidden +
                            self.extractFeature.embSize / 2)
                    ],
                                        stddev=0.08,
                                        name="ATT_text1_2")),
                "ATT_text1_3":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        self.textFeature.nHidden * 4,
                        self.textFeature.nHidden * 2
                    ],
                                        stddev=0.08,
                                        name="ATT_text1_3")),
                "ATT_text2_1":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        int(self.imageFeature.defaultFeatureSize / 2 +
                            self.textFeature.nHidden), 1
                    ],
                                        stddev=0.08,
                                        name="ATT_text2_1")),
                "ATT_text2_2":
                tf.Variable(
                    tf.truncated_normal(shape=[
                        int(self.textFeature.nHidden +
                            self.extractFeature.embSize / 2), 1
                    ],
                                        stddev=0.08,
                                        name="ATT_text2_2")),
                "ATT_text2_3":
                tf.Variable(
                    tf.truncated_normal(
                        shape=[self.textFeature.nHidden * 2, 1],
                        stddev=0.08,
                        name="ATT_text2_3")),
                "ATT_WI1":
                tf.Variable(
                    tf.truncated_normal(
                        shape=[self.imageFeature.defaultFeatureSize, 512],
                        stddev=0.08,
                        name="ATT_WI")),
                "ATT_WT1":
                tf.Variable(
                    tf.truncated_normal(shape=[2 * nHidden, 512],
                                        stddev=0.08,
                                        name="ATT_WT")),
                "ATT_WA1":
                tf.Variable(
                    tf.truncated_normal(shape=[200, 512],
                                        stddev=0.08,
                                        name="ATT_WA")),
                "ATT_WI2":
                tf.Variable(
                    tf.truncated_normal(
                        shape=[self.imageFeature.defaultFeatureSize, 512],
                        stddev=0.08,
                        name="ATT_WI2")),
                "ATT_WT2":
                tf.Variable(
                    tf.truncated_normal(shape=[2 * nHidden, 512],
                                        stddev=0.08,
                                        name="ATT_WT2")),
                "ATT_WA2":
                tf.Variable(
                    tf.truncated_normal(shape=[200, 512],
                                        stddev=0.08,
                                        name="ATT_WA2")),
                "ATT_WF_1":
                tf.Variable(
                    tf.truncated_normal(shape=[512, 1],
                                        stddev=0.08,
                                        name="ATT_WF_1")),
                "ATT_WF_2":
                tf.Variable(
                    tf.truncated_normal(shape=[512, 1],
                                        stddev=0.08,
                                        name="ATT_WF_2")),
                "ATT_WF_3":
                tf.Variable(
                    tf.truncated_normal(shape=[512, 1],
                                        stddev=0.08,
                                        name="ATT_WF_3")),
            }
            self.biases = {
                "MLP1":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[256],
                                dtype=tf.float32,
                                name="MLP1_b")),
                "MLP2":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[1],
                                dtype=tf.float32,
                                name="MLP2_b")),
                "ATT_attr1_1":
                tf.Variable(
                    tf.constant(
                        0.01,
                        shape=[
                            int(self.imageFeature.defaultFeatureSize / 2 +
                                self.extractFeature.embSize / 2)
                        ],
                        name="ATT_attr1_1")),
                "ATT_attr1_2":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[
                                    int(self.textFeature.nHidden +
                                        self.extractFeature.embSize / 2)
                                ],
                                name="ATT_attr1_2")),
                "ATT_attr1_3":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[self.extractFeature.embSize],
                                name="ATT_attr1_3")),
                "ATT_attr2_1":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_attr2_1")),
                "ATT_attr2_2":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_attr2_2")),
                "ATT_attr2_3":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_attr2_3")),
                "ATT_img1_1":
                tf.Variable(
                    tf.constant(
                        0.01,
                        shape=[
                            int(self.imageFeature.defaultFeatureSize / 2 +
                                self.textFeature.nHidden)
                        ],
                        name="ATT_image1_1")),
                "ATT_img1_2":
                tf.Variable(
                    tf.constant(
                        0.01,
                        shape=[
                            int(self.imageFeature.defaultFeatureSize / 2 +
                                self.extractFeature.embSize / 2)
                        ],
                        name="ATT_image1_2")),
                "ATT_img1_3":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[self.imageFeature.defaultFeatureSize],
                                name="ATT_image1_3")),
                "ATT_img2_1":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_image2_1")),
                "ATT_img2_2":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_image2_2")),
                "ATT_img2_3":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_image2_3")),
                "ATT_text1_1":
                tf.Variable(
                    tf.constant(
                        0.01,
                        shape=[
                            int(self.imageFeature.defaultFeatureSize / 2 +
                                self.textFeature.nHidden)
                        ],
                        name="ATT_text1_1")),
                "ATT_text1_2":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[
                                    int(self.textFeature.nHidden +
                                        self.extractFeature.embSize / 2)
                                ],
                                name="ATT_text1_2")),
                "ATT_text1_3":
                tf.Variable(
                    tf.constant(0.01,
                                shape=[self.textFeature.nHidden * 2],
                                name="ATT_text1_3")),
                "ATT_text2_1":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_text2_1")),
                "ATT_text2_2":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_text2_2")),
                "ATT_text2_3":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_text2_3")),
                "ATT_WW":
                tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WW")),
                "ATT_WI":
                tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WI")),
                "ATT_WT":
                tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WT")),
                "ATT_WI1":
                tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WI1")),
                "ATT_WT1":
                tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WT1")),
                "ATT_WA":
                tf.Variable(tf.constant(0.01, shape=[512], name="ATT_WA")),
                "ATT_WF_1":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_WF_1")),
                "ATT_WF_2":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_WF_2")),
                "ATT_WF_3":
                tf.Variable(tf.constant(0.01, shape=[1], name="ATT_WF_3")),
            }
        print("newnet dimension :", newNet)

        imageVec = self.Attention(newNet, self.imageFeature.outputLS,
                                  self.textFeature.RNNState,
                                  self.extractFeature.finalState, "ATT_img1",
                                  "ATT_img2", 196, True)
        textVec = self.Attention(self.textFeature.RNNState,
                                 self.textFeature.outputs, newNet,
                                 self.extractFeature.finalState, "ATT_text1",
                                 "ATT_text2", self.textFeature.seqLen, False)
        attrVec = self.Attention(self.extractFeature.finalState,
                                 self.extractFeature.inputEmb, newNet,
                                 self.textFeature.RNNState, "ATT_attr1",
                                 "ATT_attr2", 5, False)

        attHidden = tf.tanh(
            tf.matmul(imageVec, self.weights["ATT_WI1"]) +
            self.biases["ATT_WI1"])
        attHidden2 = tf.tanh(
            tf.matmul(textVec, self.weights["ATT_WT1"]) +
            self.biases["ATT_WT1"])
        attHidden3 = tf.tanh(
            tf.matmul(attrVec, self.weights["ATT_WA1"]) +
            self.biases["ATT_WW"])
        scores1 = tf.matmul(attHidden,
                            self.weights["ATT_WF_1"]) + self.biases["ATT_WF_1"]
        scores2 = tf.matmul(attHidden2,
                            self.weights["ATT_WF_2"]) + self.biases["ATT_WF_2"]
        scores3 = tf.matmul(attHidden3,
                            self.weights["ATT_WF_3"]) + self.biases["ATT_WF_3"]
        scoreLS = [scores1, scores2, scores3]
        scoreLS = tf.nn.softmax(scoreLS, dim=0)
        imageVec = tf.tanh(
            tf.matmul(imageVec, self.weights["ATT_WI2"]) +
            self.biases["ATT_WI"])
        textVec = tf.tanh(
            tf.matmul(textVec, self.weights["ATT_WT2"]) +
            self.biases["ATT_WT"])
        attrVec = tf.tanh(
            tf.matmul(attrVec, self.weights["ATT_WA2"]) +
            self.biases["ATT_WA"])
        self.concatInput = scoreLS[0] * imageVec + scoreLS[
            1] * textVec + scoreLS[2] * attrVec
Пример #21
0
    W_conv1 = weight_variable([5, 5, 1, 32])
    b_conv1 = bias_variable([32])
    x_image = tf.reshape(x, [-1, 28, 28, 1])
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)

    W_conv2 = weight_variable([5, 5, 32, 64])
    b_conv2 = bias_variable([64])
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)

    W_fc1 = weight_variable([7 * 7 * 64, 1024])
    b_fc1 = bias_variable([1024])
    h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    keep_prob = tf.placeholder("float")
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    W_fc2 = weight_variable([1024, 10])
    b_fc2 = bias_variable([10])

    y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

    sess = tf.Session()
Пример #22
0
# Convolution Layer 5 | ReLU | Max Pooling
c_layer_5 = tf.nn.conv2d(c_layer_4,
                         conv_weights["c5_weights"],
                         strides=[1, 1, 1, 1],
                         padding="SAME",
                         name="c_layer_5")
c_layer_5 += conv_biases["c5_biases"]
c_layer_5 = tf.nn.relu(c_layer_5)
c_layer_5 = tf.nn.max_pool(c_layer_5,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 1, 1, 1],
                           padding="VALID")

# Flatten the multi-dimensional outputs to feed fully connected layers
feature_map = tf.reshape(c_layer_5, [-1, 13, 13, 256])

# Fully Connected Layer 1 | Dropout
fc_layer_1 = tf.matmul(feature_map,
                       conv_weights["f1_weights"]) + conv_biases["f1_biases"]
fc_layer_1 = tf.nn.dropout(fc_layer_1, keep_prob=DROPOUT_KEEP_PROB)

# Fully Connected Layer 2 | Dropout
fc_layer_2 = tf.matmul(fc_layer_1,
                       conv_weights["f2_weights"]) + conv_biases["f2_biases"]
fc_layer_2 = tf.nn.dropout(fc_layer_2, keep_prob=DROPOUT_KEEP_PROB)

# Fully Connected Layer 3 | Softmax
fc_layer_3 = tf.matmul(fc_layer_2,
                       conv_weights["f3_weights"]) + conv_biases["f3_biases"]
cnn_output = tf.nn.softmax(fc_layer_3)