Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get iterable Tensor without running eval

Is there a way to make a Tensor iterable without running eval() to get its numpy array?

I am trying to iterate through two parts of a tensor after using split() on it, but it happens within the construction of the hidden layers of my neural network, so it needs to happen before I am able to start a session.

import tensorflow as tf
x = tf.placeholder('float', [None, nbits])
layer = [x]
for i in range(1,numbits):
    layer.append(tf.add(tf.matmul(weights[i-1], layer[i-1]), biases[i-1]))
    aes, bes = tf.split(1, 2, layer[-1])
        if i%2 == 1:
            for am, a, b in zip(add_layer, aes, bes):
                layer.append(am.ex(a, b))

The problem is that layer[-1] is a tf.placeholder at this point, so aes and bes are both tensors, and I can't iterate through them with zip().

Any ideas would be appreciated.

like image 208
Vendea Avatar asked Oct 30 '22 00:10

Vendea


1 Answers

No, there isn't; not directly.

It's easiest to think about Tensorflow programs as being split into two phases: a building Python phase that builds a computation graph, and a execution phase that runs the computation graph. Nothing actually runs during the building phase; all computation happens during the execution phase. The building phase can't depend on the results of the execution phase, except by running the graph (session.run(), .eval(), etc.)

You can't iterate over a Tensor while building the graph, because it doesn't actually get evaluated to a specific set of values until you call session.run(). Instead it's just a reference to a node in the computation graph.

In general, you have to use Tensorflow functions to manipulate Tensors, not Python primitives (like zip). One way I like to think of it is that it's almost like a Tensor is a radioactive object in a sealed box, and you can only handle it indirectly using a robot that can perform a certain set of actions (Tensorflow library functions) :-) So you likely need to find a way to express your task using Tensorflow primitives.

If you gave a complete example of what you're trying to do, it might be possible to say more (it's not clear to me from your code fragment). One possibility might be to use tf.split to split the tensors up into Python lists of subtensors, and then use something like zip on the lists.

I hope that helps!

like image 56
Peter Hawkins Avatar answered Nov 15 '22 07:11

Peter Hawkins