google-deepmind / dnc

A TensorFlow implementation of the Differentiable Neural Computer.
Apache License 2.0
2.5k stars 443 forks source link

How to make a placeholder for the dnc state? #1

Closed floodsung closed 7 years ago

floodsung commented 7 years ago

I need to run one step dnc, but dnc state is not a tensor, how can i feed a self constructed state to the dnc core just like the LSTM? thanks

timharley commented 7 years ago

You can construct a state with the initial_state(...) method.

If you want to build one of these by hand, you need to create a dnc.DNCState namedtuple, which also contains instances of access.AccessState and addressing.TemporalLinkageState namedtuples. Together these contain 9 different tensors of varying shapes (look at what's returned by initial_state() for the full details, the shapes vary with hyperparameters and will need to match exactly).

Alternatively, if you want to create a full set of placeholders for all components of the state, Sonnet provides a module of utilities called nest, which we use extensively for dealing with arbitrary RNN states or other similar data structures of tensors.

import tensorflow as tf
import sonnet as snt
import dnc

dnc_core = dnc.DNC(...)
initial_core_state = dnc_core.initial_state(batch_size=1)

# Creates placeholders in the same structure & shape as `initial_core_state`.
# Note: this is generic and will work for *any* Sonnet RNNCore, not just the DNC.
core_state_placeholders = snt.nest.map(lambda t: tf.placeholder(t.dtype, shape=t.shape),
                                       initial_core_state)

Hope this helps!

floodsung commented 7 years ago

Thank you so much. I have made it work!