def test_ntm_gradients(): state_size = 1 memory_shape = (5,1) batch_size=10 controller_num_layers=1 controller_hidden_size=10 input_size=1 n_batches=20 T=2 controller_network = MLP((input_size, memory_shape[1]), controller_num_layers, controller_hidden_size, state_size) x = tf.placeholder(tf.float32, [batch_size, T, input_size]) x_ = np.random.randn(batch_size*n_batches, T, input_size) y_ = 2*x_ + 1. addr = ShortcircuitAddressing(memory_shape, batch_size) rh = ReadHead(state_size, memory_shape, addresser=addr, batch_size=batch_size, hidden_size=2) #ntm_cell = NTMCell(controller_network, memory_shape, batch_size, # read_head=rh) ntm_cell = NTMCell(controller_network, memory_shape, batch_size) ntm = Network(ntm_cell, x) loss = lambda a, b: tf.nn.l2_loss(a - b) optimizer = tf.train.GradientDescentOptimizer(1e-4) ntm.compile(loss, optimizer) ntm.train(x_, y_, batch_size=batch_size, n_epochs=2)
def test_rnn(): input_size = 1 output_size = 1 T = 10 n_batches = 5 batch_size = 5 cell = RNNLayer(input_size, output_size, batch_size) x = tf.placeholder(tf.float32, [batch_size, T, input_size]) y = tf.placeholder(tf.float32, [batch_size, T, output_size]) network = Network(cell, x) x_ = np.random.randn(batch_size, T, input_size) y_ = 2*x_ + 1 optimizer = tf.train.GradientDescentOptimizer(1e-4) loss = lambda a, b: tf.reduce_mean(tf.pow(a - b, 2)) #loss = tf.reduce_mean(tf.pow(network.output()[0] - y, 2)) network.compile(loss, optimizer) losses = network.train(x_, y_, batch_size=batch_size, verbose=False)