tensorflow / fold

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

can't feed tuple to map #77

Closed blythed closed 5 years ago

blythed commented 7 years ago

I am attempting the blocks tutorial. Here's my code:

import tensorflow_fold as td
import tensorflow as tf

sess = tf.Session()

char_cell = td.ScopedLayer(tf.contrib.rnn.BasicLSTMCell(num_units=16), 'char_cell')
word_cell = td.ScopedLayer(tf.contrib.rnn.BasicLSTMCell(num_units=32), 'word_cell')

char_lstm = (td.InputTransform(lambda s: [ord(c) for c in s]) >>
             td.Map(td.Scalar('int32') >>
             td.Function(td.Embedding(128, 8))) >>
             td.RNN(char_cell))

word_lstm = td.Map(char_lstm >> td.GetItem(1)) >> td.RNN(word_cell)

print char_lstm.eval("man", session=sess) # this works fine
print word_lstm.eval(("a", "man"), session=sess) # AttributeError: 'tuple' object has no attribute 'get_shape

The error is printed in the comment However the td.Map documentation says that it takes tuples as inputs. Any help would be greatly appreciated.

tadpole commented 7 years ago

The output of char_lstm is (output_sequence, final_state), see this. Above code use td.Map(char_lstm >> td.GetItem(1)) to get final_state which is a tuple (new_h, new_c) actually. td.RNN(word_cell) need a sequence of new_h, so when it tries to call get_shape on get_h, it calls get_shape on tuple (new_h, new_c).

I think the word_lstm should be like this:

 word_lstm = td.Map(char_lstm >> td.GetItem(1) >> td.GetItem(0)) >> td.RNN(word_cell)
pklfz commented 7 years ago

see issue #54 and pr #63