Exemplo n.º 1
0
def main_test(dataset):
    """
        Predictor Test
    """
    tf.reset_default_graph()
    lr = logistic_regressor.LogisticRegressor(feature_num=conf.FEATURE_NUM,
                                              learning_rate=conf.LEARNING_RATE,
                                              random_seed=None)
    saver, logits, loss, train_op, stat_merged = lr.build_graph()
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        saver.restore(sess, '{}/model'.format(conf.CHKPT_DIR))

        in_feature_vecs = dataset[0][:100]
        in_labels = dataset[1][:100]
        in_labels = np.expand_dims(in_labels, 1)
        feed_dict = {
            lr.input_feature_vectors: in_feature_vecs,
            lr.input_labels: in_labels
        }
        out_logits, out_weights, out_biases = sess.run(
            [logits, lr.weights, lr.biases], feed_dict=feed_dict)
        print("accuracy: {}, f1: {}, auc: {}".format(
            metrics.calc_accuracy(out_logits, in_labels),
            metrics.calc_f1(out_logits, in_labels, log_confusion_matrix=True),
            metrics.calc_auc(out_logits, in_labels)))
        print("weights: ", out_weights)
        print("biases: ", out_biases)
def plot_decision_boundary_poly(X, y, theta, reg, p, xlabel, ylabel, legend):
    plot_twoclass_data(X, y, xlabel, ylabel, legend)

    # create a mesh to plot in
    h = 0.01
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, h),
                           np.arange(x2_min, x2_max, h))

    # make predictions on this mesh
    poly = sklearn.preprocessing.PolynomialFeatures(degree=p,
                                                    include_bias=False)
    X_poly = poly.fit_transform(np.c_[xx1.ravel(), xx2.ravel()])
    XX = np.vstack([np.ones((X_poly.shape[0], )), X_poly.T]).T

    lr1 = lr.LogisticRegressor()
    lr1.theta = theta
    Z = np.array(lr1.predict(XX))

    # Put the result into a color contour plot
    Z = Z.reshape(xx1.shape)
    #    plt.contourf(xx1, xx2, Z, cmap=plt.cm.Paired, alpha=0.5)
    plt.contour(xx1, xx2, Z, cmap=plt.cm.gray, levels=[0.5])
    plt.title("Decision boundary for lambda = " + str(reg))
Exemplo n.º 3
0
def plot_decision_boundary(X, y, theta, xlabel, ylabel, legend):
    plot_twoclass_data(X, y, xlabel, ylabel, legend)

    # create a mesh to plot in
    h = 0.01
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, h),
                           np.arange(x2_min, x2_max, h))
    print h

    # make predictions on this mesh
    lr1 = lr.LogisticRegressor()
    lr1.theta = theta
    Z = np.array(
        lr1.predict(np.c_[np.ones(xx1.ravel().shape),
                          xx1.ravel(),
                          xx2.ravel()]))

    print Z

    # Put the result into a color contour plot
    Z = Z.reshape(xx1.shape)
    #    plt.contourf(xx1, xx2, Z, cmap=plt.cm.Paired, alpha=0.5) # if you want a surface
    plt.contour(xx1, xx2, Z, cmap=plt.cm.gray, levels=[0.5])
 def fit(self,X,y,add_x0=True,method='stochastic',learning_rate=0.0001,
         epochs=100,starting_coeff=False,bin_size=10):
     '''
     Fits the data into n logistic regressors, using the same parameters provided here
     starting coefficients set to True will iterate all regressors using each's own current coefficients
     '''
     
     # determine how many regressors are needed
     self.unique = np.unique(y)
     # generate regressors and train each on a modified training set
     for key in self.unique:
         y_current = np.vectorize(lambda x: 1 if x==key else 0)(y)
         self.regressors[key] = logistic_regressor.LogisticRegressor()
         # if requested, give each regressor its own current coefficients
         if (starting_coeff):
             init = self.regressors[key].coeff
         else:
             init = None
         self.regressors[key].fit(X,y_current,add_x0,method,learning_rate,epochs,init,bin_size)
Exemplo n.º 5
0
def main(dataset):
    """
        Predictor
    """
    tf.reset_default_graph()
    lr = logistic_regressor.LogisticRegressor(feature_num=conf.FEATURE_NUM,
                                              learning_rate=conf.LEARNING_RATE,
                                              random_seed=None)
    lr.construct_placeholders()
    lr.construct_weights()
    saver, logits = lr.forward_pass()
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        saver.restore(sess, '{}/model'.format(conf.CHKPT_DIR))

        in_feature_vecs = dataset[0][:100]
        feed_dict = {lr.input_feature_vectors: in_feature_vecs}
        out_logits = sess.run([logits], feed_dict=feed_dict)
        print("logits: {}".format(out_logits))
Exemplo n.º 6
0
def main(dataset):
    """
        Tainer
    """
    # build graph
    tf.reset_default_graph()
    lr = logistic_regressor.LogisticRegressor(feature_num=conf.FEATURE_NUM,
                                              learning_rate=conf.LEARNING_RATE,
                                              random_seed=None)
    saver, logits, loss, train_op, stat_merged = lr.build_graph()

    # training
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)

        # log dir
        log_dir = conf.LOG_DIR
        if os.path.exists(log_dir):
            shutil.rmtree(log_dir)
        summary_writer = tf.summary.FileWriter(log_dir,
                                               graph=tf.get_default_graph())

        # checkpoint dir
        chkpt_dir = conf.CHKPT_DIR
        if os.path.exists(chkpt_dir):
            shutil.rmtree(chkpt_dir)
        if not os.path.isdir(chkpt_dir):
            os.makedirs(chkpt_dir)

        for epoch in range(conf.EPOCHES):
            batch_cnt = 0
            batches_per_epoch = math.floor(
                (len(dataset[0]) - 1) * 1.0 / conf.BATCH_SIZE) + 1
            best_loss = np.inf
            cur_loss = np.inf
            cur_accuracy = 0
            training_data = list(zip(dataset[0], dataset[1]))
            random.shuffle(training_data)
            for tu in utils.batch(training_data, n=conf.BATCH_SIZE):
                X, y = zip(*tu)
                y = np.expand_dims(y, 1)
                feed_dict = {lr.input_feature_vectors: X, lr.input_labels: y}
                sess.run(train_op, feed_dict=feed_dict)
                batch_cnt += 1
                global_step = epoch * batches_per_epoch + batch_cnt
                if global_step % conf.DISPLAY_STEP == 0:
                    in_f = dataset[0]
                    in_l = np.expand_dims(dataset[1], 1)
                    feed_dict = {
                        lr.input_feature_vectors: in_f,
                        lr.input_labels: in_l
                    }
                    cur_loss, cur_logits = sess.run([loss, logits],
                                                    feed_dict=feed_dict)
                    summary_train = sess.run(stat_merged, feed_dict=feed_dict)
                    summary_writer.add_summary(summary_train,
                                               global_step=global_step)
                    print("epoch: {}, global_step: {}, loss: {}, "
                          "accuracy: {}, f1: {}, auc: {}".format(
                              epoch, global_step, cur_loss,
                              metrics.calc_accuracy(cur_logits, in_l),
                              metrics.calc_f1(cur_logits, in_l),
                              metrics.calc_auc(cur_logits, in_l)))
            if cur_loss < best_loss:
                best_loss = cur_loss
                saver.save(sess, '{}/model'.format(chkpt_dir))