tensorflow / fold

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

Explanation on sentiment analysis example #73

Closed bdqnghi closed 6 years ago

bdqnghi commented 6 years ago

I'm trying to understand the sentiment analysis implementation in the examples.


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)

  # pair2vec = (embed_subtree(), embed_subtree())

  pair2vec_list = list()
  pair2vec_list.append(embed_subtree())
  for i in range(1):
    pair2vec_list.append(embed_subtree())

  pair2vec = tuple(pair2vec_list)

  # Trees are binary, so the tree layer takes two states as its input_state.
  # Zeros = (array[300],array[300])
  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)
  pair_case = td.AllOf(zero_inp, pair2vec)

  tree2vec = td.OneOf(len, [(1, word_case), (2, pair_case)])
  block_info(tree2vec)
  return tree2vec >> tree_lstm >> (output_layer, td.Identity())

In this part, I don't understand where did the tree2vec get the input from?, it takes the input of the len function then produce one of these 2 cases: word case or pair case, but the len of what?