kju
kju

Reputation: 146

Computation on a Tensor as numpy array in graph?

Is there any way to do some computation on a tensor in graph.

Example my graph:

slim = tf.contrib.slim

def slim_graph(images, train=False):
    with slim.arg_scope([slim.conv2d, slim.fully_connected],
                  activation_fn=tf.nn.relu,
                  weights_initializer=tf.truncated_normal_initializer(0.0, 0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005)):
        net = slim.repeat(images, 2, slim.conv2d, 64, [3, 3], scope='conv1')
        // Do my compute by numpy on net

        np_array_result = my_func(net)

        // It will return a numpy array
        // Use numpy array as input of graph

        net = slim.max_pool2d(np_array_result, [2, 2], scope='pool1')

        ...
        return logits

I can separate graph into 2 parts and use Session.run([part1]) After that use the result to input my function, then feed it to Session.run([part2])

But it seems weird.

Upvotes: 2

Views: 606

Answers (1)

Sergio Guadarrama
Sergio Guadarrama

Reputation: 257

You can use tf.py_func wrapper for python functions.

Upvotes: 1

Related Questions