tensorflow / fold

Deep learning with dynamic computation graphs in TensorFlow
Apache License 2.0
1.83k stars 266 forks source link

Composition `scope` Example fails to run #45

Open coopie opened 7 years ago

coopie commented 7 years ago

The example:

c = td.Composition()
with c.scope():
  x = td.Vector(3).reads(c.input)
  x_squared = td.Function(tf.mul).reads(x, x)
  ten = td.FromTensor(10 * np.ones(3, dtype='float32'))
  ten_x = td.Function(tf.mul).reads(ten, x)
  c.output.reads(td.Function(tf.add).reads(x_squared, ten_x)

does not run properly with modern tensorflow - the tf.muls need to be changed to multiply

On top of that: when run with the name change, it throws an error:

Traceback (most recent call last):
  File "tf_fold_playground.py", line 133, in <module>
    c.output.reads(td.Function(tf.add)).reads(x_squared, ten_x)
  File "/usr/local/lib/python3.6/site-packages/tensorflow_fold/blocks/blocks.py", line 151, in reads
    _COMPOSITION_CONTEXT.current.connect(other, self)
  File "/usr/local/lib/python3.6/site-packages/tensorflow_fold/blocks/blocks.py", line 874, in connect
    raise ValueError('input of block is already connected: %s' % (b,))
ValueError: input of block is already connected: <td.Composition.output>

using python3.6 ubuntu 16.04

okcd00 commented 7 years ago

Hi, coopie: I tried code as yours:

c = td.Composition()
with c.scope():
    x = td.Vector(3).reads(c.input)
    x_squared = td.Function(tf.mul).reads(x, x)
    ten = td.FromTensor(10 * np.ones(3, dtype='float32'))
    ten_x = td.Function(tf.mul).reads(ten, x)
    c.output.reads(td.Function(tf.add).reads(x_squared, ten_x)) # BTW. You missed one ')' here :)
c.eval([1,2,3])

And then output:

array([ 11., 24., 39.], dtype=float32)

About mul and multiply: You can check functions at api_docs The number of functions startswith 'tf.mul' is only 2. (tf.multinomial and tf.multiply). Maybe tf.mul doesn't exist now.

And about the error: You said that "when run with the change", I think maybe you're using Ipython or Jupyter Notebook, if so, kill your python kernal and restart one(or you can change the composition name from c to c_tmp). If one block is connected, it can't be connected again.

I saw this just now, perhaps it helps:

def test_composition_raises_double_connect_output(self):
    a = tdb.Scalar()
    b = tdb.Scalar()
    c = tdb.Composition([a, b])
    c.connect(a, c.output)
    self.assertRaisesWithLiteralMatch(
        ValueError,
        'input of block is already connected: <td.Composition.output>',
        c.connect, b, c.output)