Implement a N-aryTreeLSTM version of the TreeLSTM in TensorFlow Fold

Viewed 655

I'm trying to implement this paper TreeLSTM by using TensorFlow Fold. Actually, in Tensorflow Fold, there's already an example of TreeLSTM but in a BinaryTreeLSTM version, here's the tutorial: https://github.com/tensorflow/fold/blob/master/tensorflow_fold/g3doc/sentiment.ipynb

What I'm trying to do now is to implement a real NaryTreeLSTM, means that a LSTM node can be the parent of any number of children, not just 2 like in the above tutorial.

This is my attempt at trying to fold the tree, this is a modify version of logits_and_state() in the above example "

 def logits_and_state():
  """Creates a block that goes from tokens to (logits, state) tuples."""
  word2vec = (td.GetItem(0) >> td.InputTransform(lookup_word) >>
              td.Scalar('int32') >> word_embedding)

  children_num = 
  children2vec_list = list()
  children2vec_list.append(embed_subtree())
  for i in range(children_num):
    children2vec_list.append(embed_subtree())

  children2vec = tuple(children2vec_list)

  # Trees are binary, so the tree layer takes two states as its input_state.

  zero_state = td.Zeros((tree_lstm.state_size,) * 2)
  # Input is a word vector.
  zero_inp = td.Zeros(word_embedding.output_type.shape[0])

  # word_case = 
  word_case = td.AllOf(word2vec, zero_state)
  children_case = td.AllOf(zero_inp, children2vec)

  tree2vec = td.OneOf(lambda x: 1 if len(x) == 1 else 2), [(1,word_case),(2,children_case)])
  return tree2vec >> tree_lstm >> (output_layer, td.Identity())

The children_num is the thing that I'm struggling at this moment, I have no idea to get out that number, eventhought I know that the length of children can be obtained by td.GetItem(1) ==> will produce a block that contains an array of children ==> how to get out the real number of that block?

You may say that I should try PyTorch or some others DL framework that also provides Dynamic Computation Graph, but in my case, the requirement is strict with Tensorflow Fold.

0 Answers
Related