Skip to content
Snippets Groups Projects
Commit 8f40ec4b authored by Olivier Canévet's avatar Olivier Canévet
Browse files

[layers] Create rnn3d to force input size to (batch_size, n_steps, n_features)

parent 8e81ae89
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -12,14 +12,27 @@ def rnn(inputs, n_hidden, ...@@ -12,14 +12,27 @@ def rnn(inputs, n_hidden,
return RNN(n_hidden = n_hidden, return RNN(n_hidden = n_hidden,
cell_fn = cell_fn, cell_fn = cell_fn,
cell_args = cell_args, cell_args = cell_args,
name=name)(inputs) name = name)(inputs)
def rnn3d(inputs, n_hidden,
cell_fn = tf.nn.rnn_cell.BasicLSTMCell,
cell_args = { "forget_bias": 1.0, },
name = None):
"""
Expect input of size (-1, n_sequence, n_features)
For instance (batch_size, n_sequence, n_features)
"""
return RNN3D(n_hidden = n_hidden,
cell_fn = cell_fn,
cell_args = cell_args,
name = name)(inputs)
class RNN(base.Layer): class RNN(base.Layer):
""" """
Inspired from tensorlayer/tensorlayer/layers.py Inspired from tensorlayer/tensorlayer/layers.py
""" """
def __init__(self, n_hidden, def __init__(self, n_hidden,
cell_fn = tf.nn.rnn_cell.BasicLSTMCell, cell_fn = tf.nn.rnn_cell.BasicLSTMCell,
cell_args = { "forget_bias": 1.0, }, cell_args = { "forget_bias": 1.0, },
...@@ -41,3 +54,79 @@ class RNN(base.Layer): ...@@ -41,3 +54,79 @@ class RNN(base.Layer):
# Compare to tensorlayer, it is as if return_last = True # Compare to tensorlayer, it is as if return_last = True
return outputs[-1] return outputs[-1]
class RNN3D(base.Layer):
"""
Inspired from tensorlayer/tensorlayer/layers.py
"""
def __init__(self, n_hidden,
cell_fn = tf.nn.rnn_cell.BasicLSTMCell,
cell_args = { "forget_bias": 1.0, },
name=None,
**kwargs):
"""
"""
super(RNN3D, self).__init__(name=name, **kwargs)
self.n_hidden = n_hidden
self.cell = cell_fn(num_units = self.n_hidden, **kwargs)
self.n_steps = -1
self.n_features = -1
def call(self, inputs, training=False):
"""
"""
shape = inputs.get_shape()
if not shape.ndims == 3:
raise Exception("Got ndims {} but expect a tensor of size "
" (?, n_steps, n_features)".format(shape.ndims))
else:
self.n_steps = shape[1]
self.n_features = shape[2]
# Unfold 3D matrix into a temporal sequence
graph = tf.unstack(inputs, self.n_steps, 1)
# Length of list outputs is n_steps
# Size of outputs[0] is (?, n_hidden)
outputs, states = tf.nn.static_rnn(self.cell,
graph,
dtype=tf.float32)
############################################################
# Taken from
#
# tensorlayer/blob/master/tensorlayer/layers.py
#
# TODO: If stacking multiple RNN layers, the output of the
# first ones should be a cube matrix (?, n_steps, n_features)
# and return_last = False (to implement in input arguments)
#
# Currently, only one RNN, so return output[-1]
#
############################################################
# if return_last:
# # 2D Tensor [batch_size, n_hidden]
# self.outputs = outputs[-1]
# else:
# if return_seq_2d:
# # PTB tutorial: stack dense layer after that, or compute the cost from the output
# # 2D Tensor [n_example, n_hidden]
# try: # TF1.0
# self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_hidden])
# except: # TF0.12
# self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_hidden])
# else:
# # <akara>: stack more RNN layer after that
# # 3D Tensor [n_example/n_steps, n_steps, n_hidden]
# try: # TF1.0
# self.outputs = tf.reshape(tf.concat(outputs, 1), [-1, n_steps, n_hidden])
# except: # TF0.12
# self.outputs = tf.reshape(tf.concat(1, outputs), [-1, n_steps, n_hidden])
# Compare to tensorlayer, it is as if return_last = True
return outputs[-1]
...@@ -2,6 +2,7 @@ from .Layer import Layer ...@@ -2,6 +2,7 @@ from .Layer import Layer
from .Conv1D import Conv1D from .Conv1D import Conv1D
#from .Maxout import maxout #from .Maxout import maxout
from .RNN import rnn from .RNN import rnn
from .RNN import rnn3d
# gets sphinx autodoc done right - don't remove it # gets sphinx autodoc done right - don't remove it
......
#!/usr/bin/env python #!/usr/bin/env python
import sys
import numpy import numpy
from bob.learn.tensorflow.datashuffler import Memory, ScaleFactor from bob.learn.tensorflow.datashuffler import Memory, ScaleFactor
from bob.learn.tensorflow.network import MLP, Embedding from bob.learn.tensorflow.network import MLP, Embedding
...@@ -7,7 +8,7 @@ from bob.learn.tensorflow.loss import BaseLoss ...@@ -7,7 +8,7 @@ from bob.learn.tensorflow.loss import BaseLoss
from bob.learn.tensorflow.trainers import Trainer, constant from bob.learn.tensorflow.trainers import Trainer, constant
from bob.learn.tensorflow.utils import load_real_mnist, load_mnist from bob.learn.tensorflow.utils import load_real_mnist, load_mnist
from bob.learn.tensorflow.utils.session import Session from bob.learn.tensorflow.utils.session import Session
from bob.learn.tensorflow.layers import rnn from bob.learn.tensorflow.layers import rnn, rnn3d
import tensorflow as tf import tensorflow as tf
import shutil import shutil
...@@ -17,14 +18,14 @@ logger = logging.getLogger("bob.learn.tf") ...@@ -17,14 +18,14 @@ logger = logging.getLogger("bob.learn.tf")
###################################################################### ######################################################################
batch_size = 128 batch_size = 64
iterations = 200 iterations = 200
seed = 10 seed = 10
learning_rate = 0.001 learning_rate = 0.001
n_input = 28 # MNIST data input (img shape: 28*28) n_input = 28 # MNIST data input (img shape: 28*28)
n_steps = 27 # timesteps n_steps = 27 # timesteps
n_hidden = 128 # hidden layer num of features n_hidden = 144 # hidden layer num of features
n_classes = 10 # MNIST total classes (0-9 digits) n_classes = 10 # MNIST total classes (0-9 digits)
directory = "./temp/lstm" directory = "./temp/lstm"
...@@ -70,7 +71,7 @@ def test_dnn_trainer(): ...@@ -70,7 +71,7 @@ def test_dnn_trainer():
# Preparing the architecture # Preparing the architecture
input_pl = train_data_shuffler("data", from_queue=False) input_pl = train_data_shuffler("data", from_queue=False)
version = "bob" version = "bob3d"
# Original code using MLP # Original code using MLP
if version == "mlp": if version == "mlp":
...@@ -98,6 +99,13 @@ def test_dnn_trainer(): ...@@ -98,6 +99,13 @@ def test_dnn_trainer():
graph = rnn(graph, n_hidden) graph = rnn(graph, n_hidden)
graph = slim.fully_connected(graph, n_classes, activation_fn=None) graph = slim.fully_connected(graph, n_classes, activation_fn=None)
elif version == "bob3d":
slim = tf.contrib.slim
graph = input_pl[:, n_input:]
graph = tf.reshape(graph, (-1, n_steps, n_input))
graph = rnn3d(graph, n_hidden)
graph = slim.fully_connected(graph, n_classes, activation_fn=None)
# Loss for the softmax # Loss for the softmax
loss = BaseLoss(tf.nn.sparse_softmax_cross_entropy_with_logits, tf.reduce_mean) loss = BaseLoss(tf.nn.sparse_softmax_cross_entropy_with_logits, tf.reduce_mean)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment