Last active
September 15, 2017 18:45
-
-
Save flowpoint/2a0a141d57f29f2306a08105b0399dec to your computer and use it in GitHub Desktop.
DNC Tensorflow example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# # DNC Tensorflow example | |
# running deepminds dnc implementation via "tf.nn.dynamic_rnn" | |
# | |
# needed dependencies (python3.6): | |
# https://github.com/deepmind/dnc | |
# numpy, | |
# tensorflow, | |
# sonnet | |
# | |
# place this file in the folder of dnc.py | |
# | |
# results are random | |
# | |
# MIT licensed (if it matters???) | |
# In[1]: | |
import dnc | |
import tensorflow as tf | |
import numpy as np | |
import random | |
# In[2]: | |
access_config = { | |
"memory_size": 10, | |
"word_size": 4, | |
"num_reads": 1, | |
"num_writes": 1, | |
} | |
controller_config = { | |
"hidden_size": 2, | |
} | |
dataset_target_size = 10 | |
# In[3]: | |
xor = [[0,0,0], | |
[0,1,1], | |
[1,0,1], | |
[1,1,0]] | |
xorset = random.choices(xor,k=10) | |
xorset = np.array(xorset,dtype='float64').reshape(1,10,3) | |
dataset = xorset[:,:,0:2] | |
print("dataset=\n" + str(dataset)) | |
labels = xorset[:,:,2] | |
print("labels=\n" + str(labels)) | |
batch_size = 1 | |
# In[4]: | |
tf.reset_default_graph() | |
dncs = dnc.DNC(access_config,controller_config, dataset_target_size) | |
outputs, last_states = tf.nn.dynamic_rnn( | |
cell=dncs, | |
initial_state=dncs.initial_state(batch_size,dtype='float64'), | |
inputs=dataset) | |
# In[5]: | |
result = tf.contrib.learn.run_n( | |
{"outputs": outputs, "last_states": last_states}, | |
n=1, | |
feed_dict=None) | |
print(result[0]["outputs"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment