|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import tensorflow as tf |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from controller import Controller |
| 7 | +from heads import ReadHead, WriteHead |
| 8 | + |
| 9 | +from tensorflow.keras import Model |
| 10 | +from tensorflow.keras.layers import Dense |
| 11 | + |
| 12 | + |
| 13 | +class NTM(Model): |
| 14 | + |
| 15 | + def __init__(self, controller_size=100, memory_locations=128, memory_vector_size=20, maximum_shifts=3, output_size=8): |
| 16 | + super(NTM, self).__init__() |
| 17 | + |
| 18 | + self.memory_locations = memory_locations # N locations |
| 19 | + self.memory_vector_size = memory_vector_size # M size memory vectors |
| 20 | + self.maximum_shifts = maximum_shifts |
| 21 | + |
| 22 | + self.controller = Controller(controller_size) |
| 23 | + self.read_head = ReadHead(self.memory_locations, self.memory_vector_size, self.maximum_shifts) |
| 24 | + self.write_head = WriteHead(self.memory_locations, self.memory_vector_size, self.maximum_shifts) |
| 25 | + |
| 26 | + self.final_fc = Dense(units=output_size, activation=tf.nn.sigmoid, name="final_fc", |
| 27 | + kernel_initializer='glorot_uniform', bias_initializer='glorot_normal') |
| 28 | + |
| 29 | + self.stddev = 1.0 / (np.sqrt(self.memory_locations + self.memory_vector_size)) |
| 30 | + |
| 31 | + # The learned bias vector |
| 32 | + self.r_bias = tf.constant(tf.random.normal([1, self.memory_vector_size]) * 0.01) # Bias for previous reads |
| 33 | + self.M_bias = tf.constant(tf.random.uniform([1, self.memory_locations, self.memory_vector_size], |
| 34 | + minval=-self.stddev, maxval=self.stddev)) # Bias for Memory matrix |
| 35 | + |
| 36 | + # States of the NTM |
| 37 | + self.r_t_1 = None # Previous read vector variable [Batch size, M] |
| 38 | + self.w_t_1 = None # Previous weights over the memory matrix [Batch size, N] |
| 39 | + self.M_t = None # The memory matrix [Batch size, N, M] |
| 40 | + |
| 41 | + # Extra outputs that are tracked |
| 42 | + self.e_t = None |
| 43 | + self.a_t = None |
| 44 | + |
| 45 | + def create_new_state(self, batch_size): # Creates a new NTM state |
| 46 | + # This has to be manually called if stateful is set to true |
| 47 | + if self.r_t_1 is None: |
| 48 | + self.r_t_1 = tf.Variable(tf.tile(self.r_bias, [batch_size, 1]), trainable=False) |
| 49 | + else: |
| 50 | + self.r_t_1.assign(tf.tile(self.r_bias, [batch_size, 1])) |
| 51 | + |
| 52 | + if self.w_t_1 is None: |
| 53 | + self.w_t_1 = tf.Variable(tf.zeros([batch_size, self.memory_locations]), trainable=False) |
| 54 | + else: |
| 55 | + self.w_t_1.assign(tf.zeros([batch_size, self.memory_locations])) |
| 56 | + |
| 57 | + if self.M_t is None: |
| 58 | + self.M_t = tf.Variable(tf.tile(self.M_bias, [batch_size, 1, 1]), trainable=False) |
| 59 | + else: |
| 60 | + self.M_t.assign(tf.tile(self.M_bias, [batch_size, 1, 1])) |
| 61 | + |
| 62 | + def call(self, inputs, stateful=False): |
| 63 | + # Convert from [Batch, Timesteps, Features] to [Timesteps, Batch, Features] |
| 64 | + inputs = tf.transpose(inputs, [1, 0, 2]) |
| 65 | + outputs = tf.TensorArray(dtype=inputs.dtype, size=inputs.shape[0]) |
| 66 | + |
| 67 | + if not stateful: # A new state will not be created at the start of each new batch |
| 68 | + self.create_new_state(inputs.shape[1]) |
| 69 | + |
| 70 | + for i in range(inputs.shape[0]): |
| 71 | + # Concatenated input and previous reads [Batch, Features + N] |
| 72 | + controller_inputs = tf.concat([inputs[i], self.r_t_1], axis=1) |
| 73 | + controller_outputs = self.controller(controller_inputs) # [Batch size, Controller size] |
| 74 | + |
| 75 | + r_t, w_t = self.read_head(controller_outputs, tf.identity(self.w_t_1), tf.identity(self.M_t)) # [Batch size, M], [Batch size, N] |
| 76 | + self.r_t_1.assign(r_t) |
| 77 | + self.w_t_1.assign(w_t) |
| 78 | + |
| 79 | + # [Batch size, M, N], [Batch size, M], [Batch size, M], [Batch size, N] |
| 80 | + M_t, self.e_t, self.a_t, w_t = self.write_head(controller_outputs, tf.identity(self.w_t_1), tf.identity(self.M_t)) |
| 81 | + self.M_t.assign(M_t) |
| 82 | + self.w_t_1.assign(w_t) |
| 83 | + |
| 84 | + fc_input = tf.concat([controller_outputs, self.r_t_1], axis=1) # [Batch size, Controller size + M], |
| 85 | + output_t = self.final_fc(fc_input) # [Batch size, Output size] |
| 86 | + outputs.write(i, output_t) # Write it to an array |
| 87 | + |
| 88 | + outputs = tf.transpose(outputs.stack(), [1, 0, 2]) # [Batch size, Timesteps, Output size] |
| 89 | + return outputs |
| 90 | + |
| 91 | + |
| 92 | +# ntm = NTM(controller_size=100, memory_locations=10, memory_vector_size=5, output_size=3) |
| 93 | + |
| 94 | +# # [Batch, Timesteps, Features] |
| 95 | +# inp = tf.Variable(tf.reshape(tf.range(0.0,4.0,0.1),[2,5,4])) |
| 96 | +# out = ntm(inp) |
| 97 | +# print(out) |
0 commit comments