0
$\begingroup$

Is there a way to feed tensorflow tensors into a sklearn model? I have the following model to set up data compression:

with tf.name_scope('model'):   with tf.name_scope('setup'):
    token_size = X_train[0][0].shape[1]
    batch_size = 64
    dropout_1 = tf.placeholder_with_default(1. , shape=(), name='dropout_1')
    dropout_2 = tf.placeholder_with_default(1. , shape=(), name='dropout_2')
    # [None, 5, 19, 400]
    inputs = tf.placeholder(tf.float32, shape=(None, FLAGS.eval_beam_size, largest, token_size), name='inputs')
    label = tf.placeholder(tf.float32, shape=(None, FLAGS.eval_beam_size), name='label')

  with tf.name_scope('layer1'):
    hid_size_1 = 20
    # [400, 5]
    w1 = tf.Variable(tf.random_normal([token_size ,hid_size_1], stddev=0.01), name='w1')
    b1 = tf.Variable(tf.constant(0.1, shape=([hid_size_1])), name='b1')
    y1 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.tensordot(inputs, w1, 1), b1)), dropout_1, name='dropout_1_op')

    tf.summary.histogram('layer1_weights', w1)
    tf.summary.histogram('layer1_bias', b1)

  with tf.name_scope('layer_2'):
    hid_size_2 = 5   # [400, 5]
    w2 = tf.Variable(tf.random_normal([hid_size_1, hid_size_2], stddev=0.01), name='w2')
    b2 = tf.Variable(tf.constant(0.1, shape=([hid_size_2])), name='b2')
    y2 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.tensordot(y1, w2, 1), b2)), dropout_2, name='dropout_2_op')

    tf.summary.histogram('layer2_weights', w2)
    tf.summary.histogram('layer2_bias', b2)

  with tf.name_scope('layer_3'):
    hid_size_3 = 1
    w3 = tf.Variable(tf.random_normal([hid_size_2, hid_size_3], stddev=0.01), name='w3')
    b3 = tf.Variable(tf.constant(0.1, shape=([hid_size_3])), name='b3')
    y3 = tf.nn.dropout(tf.nn.tanh(tf.add(tf.tensordot(y2, w3, 1), b3)), dropout_2, name='dropout_3_op')

    tf.summary.histogram('layer3_weights', w3)
    tf.summary.histogram('layer3_bias', b3)

with tf.name_scope('support_ops'):   summed = tf.squeeze(tf.reduce_sum(y3, axis=2), axis=-1, name='summed')

From here I would like to be able to feed the outputs into a sklearn model and perhaps leave these layers to train based on the model output. Is this possible?

$\endgroup$
2
$\begingroup$

Yes it is possible. Once you actually return the results from the Tensorflow model, they will (by default) be returned as NumPy arrays. You can then use them as input e.g. to a SciKit Learn model.

Have a look at this thread, which shows some nice examples the types returned by TF models.

One last consideration: The model must return results in such a way that they are in the normal memory, reachable for the CPU - not somehow left in GPU memory.

$\endgroup$
4
  • $\begingroup$ Thank you for the response, I am going to look into the thread and update when possible. $\endgroup$
    – Jacob B
    Dec 3 '18 at 1:31
  • $\begingroup$ One issue may be that although I can convert tensors into numpy arrays there may notbe a way to update the network layers based on sklearn returns. $\endgroup$
    – Jacob B
    Dec 3 '18 at 1:32
  • $\begingroup$ @JacobB - I'm sure there would be a way to do that - it might just be a little complicated. Search for something like "tensorflow intermediate placeholders". This idea might be helpful. Otherwise you could switch to either Tensorflow Eager mode or even to PyTorch, where doing this kind of thing is embraced :-) $\endgroup$
    – n1k31t4
    Dec 3 '18 at 1:35
  • $\begingroup$ Awesome, thank you I did not know PyTorch had this idea as a main factor! $\endgroup$
    – Jacob B
    Dec 3 '18 at 1:51

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.