1
$\begingroup$

I would like to use existing Scikit Learn LogisticRegression model in the BigQuery ML. However, BQ ML currently has a hard limit of 50 unique labels and my model needs to handle more than that.

BQ accepts TensorFlow models, which do not seem to have this limit.

How can I convert existing Scikit logistic regression model to TensorFlow model?

$\endgroup$
3
$\begingroup$

Sure, here is a skeleton in TF 2.0.

import tensorflow as tf

weights = tf.Variable(tf.random.normal(shape=(784, 10), dtype=tf.float64))
biases  = tf.Variable(tf.random.normal(shape=(10,), dtype=tf.float64))

def logistic_regression(x):
    lr = tf.add(tf.matmul(x, weights), biases)
    return lr

def cross_entropy(y_true, y_pred):
    y_true = tf.one_hot(y_true, 10)
    loss = tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred)
    return tf.reduce_mean(loss)

def grad(x, y):
    with tf.GradientTape() as tape:
        y_pred = logistic_regression(x)
        loss_val = cross_entropy(y, y_pred)
    return tape.gradient(loss_val, [weights, biases])

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.