Example #1
0
    def discriminator(self, inputs, scope='discriminator', reuse=None):
        with tf.variable_scope(scope, reuse=reuse):

            # Set discriminator to always be training. Reason for doing this is
            # For the WGAN gradient loss (which is not the default loss function for
            # this model, still uses this architecture), the loss function has an expression
            # which is the gradient of an instance of the discriminator. Putting that
            # into the optimizer creates a dependency on the second order gradient of the
            # disriminator. Batch normalization where the training vs running flag is itself
            # a TF variable (rather than normal python boolean) seems to break this. Easier to
            # just set to True because in this model we only ever use the discriminator for
            # training (to train the generator).
            bn = BN(True)

            t = lrelu(conv2d(inputs, 64))  # no bn here
            t = lrelu(bn(conv2d(t, 128)))
            t = lrelu(bn(conv2d(t, 256)))
            t = lrelu(bn(conv2d(t, 512)))
            t = lrelu(bn(conv2d(t, 1024)))

            # flatten 3D tensor into 1D to prepare for a dense (fully connected)
            # layer. Flattened tensor can also be treated as vector that can be
            # used for learned similarty measurements between images.
            similarity = flatten(t)

            # return logits (before sigmoid activation) because several TF
            # accumulator functions expect logits, and do the sigmoid for you
            logits = dense(similarity, 1)
            classification = sigmoid(logits)
            return classification, logits, similarity
Example #2
0
def E_x(m_opts, m_vars):
    PSI = m_vars['U_batch'].dot(m_vars['V'].T)
    PSI_sigmoid = sigmoid(-PSI)
    E_x = (m_vars['mu'] * PSI_sigmoid) / (EPS + m_vars['mu'] * PSI_sigmoid +
                                          (1. - m_vars['mu']))
    E_x[m_vars['Y_batch'].nonzero()] = 1.
    return E_x
Example #3
0
def predict(m_opts, m_vars, X, break_chunks=1):
    if break_chunks != 1:
        print "Warning, break_chunks no longer supported in predict."
    U = X.dot(m_vars['W'].T)
    Y_pred = U.dot(m_vars['V'].T)
    Y_pred = sigmoid(Y_pred)
    Y_pred_1 = Y_pred * m_vars['mu']
    return Y_pred_1, Y_pred
Example #4
0
def E_x_omega_col(m_opts, m_vars):
    PSI = m_vars['V'].dot(m_vars['U_batch'].T)
    E_omega = 0.5 * np.tanh(0.5 * PSI) / (EPS + PSI)
    PSI_sigmoid = sigmoid(-PSI)
    mu_temp = m_vars['mu'][:, np.newaxis]
    E_x = (mu_temp * PSI_sigmoid) / (EPS + mu_temp * PSI_sigmoid +
                                     (1. - mu_temp))
    E_x[m_vars['Y_batch_T'].nonzero()] = 1.
    return E_x, E_omega
Example #5
0
def E_x_omega_row(m_opts, m_vars):
    PSI = m_vars['U_batch'].dot(m_vars['V'].T)
    E_omega = 0.5 * np.tanh(0.5 * PSI) / (EPS + PSI)
    PSI_sigmoid = sigmoid(-PSI)
    E_x = (m_vars['mu'] * PSI_sigmoid) / (EPS + m_vars['mu'] * PSI_sigmoid +
                                          (1. - m_vars['mu']))

    E_x[m_vars['Y_batch'].nonzero()] = 1.
    return E_x, E_omega
Example #6
0
    def __init__(self):
        self.inputs = tf.placeholder(tf.float32, [None, IMG_H, IMG_W, 3])
        self.is_training = tf.placeholder(tf.bool)
        _, _, class_logits_dict, box_logits_dict = backbone(self.inputs, self.is_training)

        class_logits_dict["P3"], class_logits_dict["P4"], class_logits_dict["P5"], class_logits_dict["P6"], class_logits_dict["P7"] = \
        tf.reshape(class_logits_dict["P3"], [-1, K]), tf.reshape(class_logits_dict["P4"], [-1, K]), tf.reshape(class_logits_dict["P5"], [-1, K]), \
        tf.reshape(class_logits_dict["P6"], [-1, K]), tf.reshape(class_logits_dict["P7"], [-1, K])

        box_logits_dict["P3"], box_logits_dict["P4"], box_logits_dict["P5"], box_logits_dict["P6"], box_logits_dict["P7"] = \
        tf.reshape(box_logits_dict["P3"], [-1, 4]), tf.reshape(box_logits_dict["P4"], [-1, 4]), tf.reshape(box_logits_dict["P5"], [-1, 4]), \
        tf.reshape(box_logits_dict["P6"], [-1, 4]), tf.reshape(box_logits_dict["P7"], [-1, 4])

        P3_class_pred, P4_class_pred, P5_class_pred, P6_class_pred, P7_class_pred = \
        sigmoid(class_logits_dict["P3"]), sigmoid(class_logits_dict["P4"]), sigmoid(class_logits_dict["P5"]), sigmoid(class_logits_dict["P6"]), sigmoid(class_logits_dict["P7"])

        P3_bbox_pred, P4_bbox_pred, P5_bbox_pred, P6_bbox_pred, P7_bbox_pred = \
        box_logits_dict["P3"], box_logits_dict["P4"], box_logits_dict["P5"], box_logits_dict["P6"], box_logits_dict["P7"]
        # thresholding confidence at 0.05 at most 1000 top k score
        P3_topK_score, P3_topK_bbox, P3_topK_anchors, P3_topK_class = top_k_score_bbox(P3_class_pred, P3_bbox_pred, anchors_p3, threshold=0.05, k=1000)
        P4_topK_score, P4_topK_bbox, P4_topK_anchors, P4_topK_class = top_k_score_bbox(P4_class_pred, P4_bbox_pred, anchors_p4, threshold=0.05, k=1000)
        P5_topK_score, P5_topK_bbox, P5_topK_anchors, P5_topK_class = top_k_score_bbox(P5_class_pred, P5_bbox_pred, anchors_p5, threshold=0.05, k=1000)
        P6_topK_score, P6_topK_bbox, P6_topK_anchors, P6_topK_class = top_k_score_bbox(P6_class_pred, P6_bbox_pred, anchors_p6, threshold=0.05, k=1000)
        P7_topK_score, P7_topK_bbox, P7_topK_anchors, P7_topK_class = top_k_score_bbox(P7_class_pred, P7_bbox_pred, anchors_p7, threshold=0.05, k=1000)

        self.topK_score = tf.concat([P3_topK_score, P4_topK_score, P5_topK_score, P6_topK_score, P7_topK_score], axis=0)
        self.topK_bbox = tf.concat([P3_topK_bbox, P4_topK_bbox, P5_topK_bbox, P6_topK_bbox, P7_topK_bbox], axis=0)
        self.topK_anchors = tf.concat([P3_topK_anchors, P4_topK_anchors, P5_topK_anchors, P6_topK_anchors, P7_topK_anchors], axis=0)
        self.topK_class = tf.concat([P3_topK_class, P4_topK_class, P5_topK_class, P6_topK_class, P7_topK_class], axis=0)

        self.bbox = offset2bbox(self.topK_anchors, self.topK_bbox)
        self.nms_idx = tf.image.non_max_suppression(self.bbox, self.topK_score, max_output_size=300)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        saver = tf.train.Saver()
        saver.restore(self.sess, "./model/model.ckpt")
Example #7
0
def predictPrecision(m_opts, m_vars, X, k=5, break_chunks=2):
    U = ssp.csr_matrix(X.dot(m_vars['W'].T))
    n_users_test = m_vars['Y_test'].shape[0]
    chunk_size = n_users_test // break_chunks

    start_idx = range(0, n_users_test, chunk_size)[:break_chunks]
    end_idx = start_idx[1:] + [n_users_test]

    p_1 = np.zeros((break_chunks, k))
    p_2 = np.zeros((break_chunks, k))
    n_total_items = 0
    n_labels = 0
    Y_predict_chunk = None
    Y_test_chunk = None
    print "Chunk #",
    for i, (lo, hi) in enumerate(zip(start_idx, end_idx)):
        print i,
        Y_pred_chunk = np.asarray(U[lo:hi].dot(m_vars['V'].T))
        Y_pred_chunk_2 = sigmoid(Y_pred_chunk)
        Y_pred_chunk_1 = m_vars['mu'] * Y_pred_chunk_2

        prevMatch_1 = 0
        prevMatch_2 = 0
        Y_pred_1 = Y_pred_chunk_1.copy()
        Y_pred_2 = Y_pred_chunk_2.copy()
        Y_true = m_vars['Y_test'][lo:hi].copy()
        n_items, n_labels = Y_pred_1.shape
        n_total_items += n_items
        for t in xrange(1, k + 1):
            Jidx_1 = np.argmax(Y_pred_1, 1)
            prevMatch_1 += Y_true[np.arange(n_items), Jidx_1].sum()
            Y_pred_1[np.arange(n_items), Jidx_1] = -np.inf
            p_1[i, t - 1] = prevMatch_1  #/(t*n_items)

            Jidx_2 = np.argmax(Y_pred_2, 1)
            prevMatch_2 += Y_true[np.arange(n_items), Jidx_2].sum()
            Y_pred_2[np.arange(n_items), Jidx_2] = -np.inf
            p_2[i, t - 1] = prevMatch_2  #/(t*n_items)
    print "Done"

    q_1 = np.zeros(k)
    q_2 = np.zeros(k)
    for i in range(1, k + 1):
        q_1[i - 1] = p_1[:, i - 1].sum() / (i * n_total_items)
        q_2[i - 1] = p_2[:, i - 1].sum() / (i * n_total_items)

    return tuple(q_1[[0, 2, 4]]), tuple(q_2[[0, 2, 4]])
    for user_id, item_id, outcome, nb_wins, nb_fails in np.array(df_test):
        if user_id > 3374:
            break

        print('Welcome', user_id)

        train_users[user_id].append(user_id)
        train_items[user_id].append(item_id)
        train_rates[user_id].append(outcome)
        train_wins[user_id].append(nb_wins)
        train_fails[user_id].append(nb_fails)

        item_logit = sess.run(logits, feed_dict={
            user_batch: [user_id], item_batch: [item_id], wins_batch: [nb_wins], fails_batch: [nb_fails]})
        item_proba = ops.sigmoid(item_logit)

        all_truth.append(outcome)
        all_pred.append(item_proba)
        # print('Asking', item_id, 'predicted', item_proba, 'actually', outcome)

        for i in range(1, EPOCH_MAX + 1):

            _, train_logits_cdf, train_infer, train_user_bias, train_item_bias, train_user_features, train_thresholds = sess.run(
                    [train_op, logits_cdf, infer, user_bias, item_bias, user_features, thresholds], feed_dict={
                        user_batch: train_users[user_id], item_batch: train_items[user_id], rate_batch: train_rates[user_id], wins_batch: train_wins[user_id], fails_batch: train_fails[user_id]})

            if i % LOG_STEP == 0:

                train_cost = deque()
                train_acc = deque()
Example #9
0
 def fprop(self):
     self.out = sigmoid(self.pred.out)
Example #10
0
File: nodes.py Project: xiamike/nn
 def fprop(self):
     self.out = sigmoid(self.pred.out)
Example #11
0
def svd(train, test):
    nb_batches = len(train) // BATCH_SIZE

    iter_train = dataio.ShuffleIterator([
        train["user"], train["item"], train["outcome"], train["wins"],
        train["fails"]
    ],
                                        batch_size=BATCH_SIZE)

    iter_test = dataio.OneEpochIterator([
        test["user"], test["item"], test["outcome"], test["wins"],
        test["fails"]
    ],
                                        batch_size=-1)

    user_batch = tf.placeholder(tf.int32, shape=[None], name="id_user")
    item_batch = tf.placeholder(tf.int32, shape=[None], name="id_item")
    rate_batch = tf.placeholder(tf.float32, shape=[None])
    wins_batch = tf.placeholder(tf.float32, shape=[None], name="nb_wins")
    fails_batch = tf.placeholder(tf.float32, shape=[None], name="nb_fails")

    # infer, logits, logits_cdf, logits_pdf, regularizer, user_bias, user_features, item_bias, item_features, thresholds = ops.inference_svd(user_batch, item_batch, wins_batch, fails_batch, user_num=USER_NUM, item_num=ITEM_NUM, dim=DIM, device=DEVICE)
    infer, logits, regularizer, user_bias, user_features, item_bias, item_features = ops.inference_svd(
        user_batch,
        item_batch,
        wins_batch,
        fails_batch,
        user_num=USER_NUM,
        item_num=ITEM_NUM,
        dim=DIM,
        device=DEVICE)
    global_step = tf.train.get_or_create_global_step()
    #cost_l2, train_op = ops.optimization(infer, regularizer, rate_batch, learning_rate=LEARNING_RATE, reg=LAMBDA_REG, device=DEVICE)
    cost_nll, train_op = ops.optimization(infer,
                                          logits,
                                          regularizer,
                                          rate_batch,
                                          learning_rate=LEARNING_RATE,
                                          reg=LAMBDA_REG,
                                          device=DEVICE)
    #cost, train_op = ops.optimization(infer, logits, logits_cdf, logits_pdf, regularizer, rate_batch, learning_rate=LEARNING_RATE, reg=LAMBDA_REG, device=DEVICE)

    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(init_op)
        summary_writer = tf.summary.FileWriter(logdir="/tmp/svd/log",
                                               graph=sess.graph)
        print("{} {} {} {}".format("epoch", "train_error", "val_error",
                                   "elapsed_time"))
        train_se = deque(maxlen=nb_batches)
        train_nll = deque(maxlen=nb_batches)
        train_cost = deque(maxlen=nb_batches)
        train_acc = deque(maxlen=nb_batches)
        train_obo = deque(maxlen=nb_batches)
        train_auc = deque(maxlen=nb_batches)
        start = time.time()
        for i in range(EPOCH_MAX * nb_batches):
            train_users, train_items, train_rates, train_wins, train_fails = next(
                iter_train)
            batch_size = len(train_rates)

            _, train_logits, train_infer = sess.run(
                [train_op, logits, infer],
                feed_dict={
                    user_batch: train_users,
                    item_batch: train_items,
                    rate_batch: train_rates,
                    wins_batch: train_wins,
                    fails_batch: train_fails
                })
            #print('values', train_infer[42], train_logits[42], train_logits_cdf[42], ops.sigmoid(train_logits[42]), ops.sigmoid(train_logits_cdf[42]))

            # print(train_logits_cdf[42])
            # print(train_logits_pdf[42])
            # print(train_rates[42])

            if DISCRETE:
                if NB_CLASSES > 2:
                    cost_batch = sess.run(cost,
                                          feed_dict={
                                              rate_batch: train_rates,
                                              item_batch: train_items,
                                              user_batch: train_users,
                                              logits_cdf: train_logits_cdf
                                          })
                    # print(train_users[42])
                    # print(train_items[42])
                    # print(train_logits_pdf[42])
                    # print(train_logits_cdf[42])
                    # print('thr', all_thresholds)
                    # print('infer', train_infer[42])
                    train_cost.append(cost_batch)
                    train_acc.append(train_infer == train_rates)
                    train_obo.append(abs(train_infer - train_rates) <= 1)
                    train_se.append(np.power(train_infer - train_rates, 2))
                else:
                    nll_batch = sess.run(cost_nll,
                                         feed_dict={
                                             rate_batch: train_rates,
                                             logits: train_logits
                                         })
                    proba_batch = ops.sigmoid(train_logits)
                    train_acc.append(np.round(proba_batch) == train_rates)
                    train_auc.append(roc_auc_score(train_rates, proba_batch))
                    train_nll.append(nll_batch)
            else:
                l2_batch = sess.run(cost_l2,
                                    feed_dict={
                                        rate_batch: train_rates,
                                        infer: train_infer
                                    })
                #print('est-ce', np.sum(np.power(train_rates - train_pred_batch, 2)))
                #print('que = ', l2_batch)
                #train_se.append(np.power(l2_batch, 2))
                train_se.append(np.power(train_rates - train_infer, 2))

            if i % nb_batches == 0:
                # Compute test error
                train_rmse = np.sqrt(np.mean(train_se))
                train_macc = np.mean(train_acc)
                train_mobo = np.mean(train_obo)
                train_mauc = np.mean(train_auc)
                train_mnll = np.mean(train_nll) / BATCH_SIZE
                train_mcost = np.mean(train_cost)
                test_se = []
                test_acc = []
                test_obo = []
                test_auc = 0
                test_nll = []
                test_cost = []
                for test_users, test_items, test_rates, test_wins, test_fails in iter_test:
                    test_logits, test_infer = sess.run(
                        [logits, infer],
                        feed_dict={
                            user_batch: test_users,
                            item_batch: test_items,
                            wins_batch: test_wins,
                            fails_batch: test_fails
                        })
                    test_size = len(test_rates)

                    # print(test_logits_cdf[42], test_logits_pdf[42])
                    # print(test_infer[42], test_rates[42])

                    if DISCRETE:
                        if NB_CLASSES > 2:
                            cost_batch = sess.run(cost,
                                                  feed_dict={
                                                      rate_batch: test_rates,
                                                      item_batch: test_items,
                                                      user_batch: test_users
                                                  })
                            #print(cost_batch)
                            test_cost.append(cost_batch)
                            test_acc.append(test_infer == test_rates)
                            test_obo.append(abs(test_infer - test_rates) <= 1)
                            test_se.append(np.power(test_infer - test_rates,
                                                    2))
                        else:
                            #train_cost.append(cost_batch)
                            nll_batch = sess.run(cost_nll,
                                                 feed_dict={
                                                     rate_batch: test_rates,
                                                     logits: test_logits
                                                 })
                            proba_batch = ops.sigmoid(test_logits)
                            test_acc.append(
                                np.round(proba_batch) == test_rates)
                            test_auc = roc_auc_score(test_rates, proba_batch)
                            # print(proba_batch[:5], test_rates[:5], test_auc)
                            test_nll.append(nll_batch)
                    else:
                        l2_batch = sess.run(cost_l2,
                                            feed_dict={
                                                rate_batch: rates,
                                                infer: pred_batch
                                            })
                        test_se.append(np.power(rates - pred_batch, 2))

                end = time.time()
                test_rmse = np.sqrt(np.mean(test_se))
                test_macc = np.mean(test_acc)
                test_mobo = np.mean(test_obo)
                test_mnll = np.mean(test_nll) / len(test)
                test_mcost = np.mean(test_cost)
                if DISCRETE:
                    if NB_CLASSES > 2:
                        print(
                            "{:3d} TRAIN(size={:d}/{:d}, macc={:f}, mobo={:f}, rmse={:f}, mcost={:f}) TEST(size={:d}, macc={:f}, mobo={:f}, rmse={:f}, mcost={:f}) {:f}(s)"
                            .format(i // nb_batches, len(train_users),
                                    len(train), train_macc,
                                    train_mobo, train_rmse, train_mcost,
                                    len(test), test_macc, test_mobo, test_rmse,
                                    test_mcost, end - start))
                    else:
                        print(
                            "{:3d} TRAIN(size={:d}/{:d}, macc={:f}, mauc={:f}, mnll={:f}) TEST(size={:d}, macc={:f}, auc={:f}, mnll={:f}) {:f}(s)"
                            .format(
                                i // nb_batches,
                                len(train_users),
                                len(train),
                                #train_rmse, # rmse={:f}
                                train_macc,
                                train_mauc,
                                train_mnll,
                                len(test),
                                #test_rmse, # rmse={:f}
                                test_macc,
                                test_auc,
                                test_mnll,
                                end - start))
                else:
                    print(
                        "{:3d} TRAIN(size={:d}/{:d}, rmse={:f}) TEST(size={:d}, rmse={:f}) {:f}(s)"
                        .format(
                            i // nb_batches,
                            len(train_users),
                            len(train),
                            train_rmse,  # rmse={:f} 
                            #train_macc, train_mauc, train_mnll,
                            len(test),
                            test_rmse,  # rmse={:f} 
                            #test_macc, test_mauc, test_mnll,
                            end - start))
                train_err_summary = make_scalar_summary(
                    "training_error", train_rmse)
                test_err_summary = make_scalar_summary("test_error", test_rmse)
                summary_writer.add_summary(train_err_summary, i)
                summary_writer.add_summary(test_err_summary, i)
                start = end
        # print('thr', all_thresholds)

        # Save model
        print(os.path.join(BASE_DIR, 'fm.ckpt'))
        saver.save(sess, os.path.join(BASE_DIR, 'fm.ckpt'))