1D CNN in blocks or how to warp text as a 4D tensor

14 views
Skip to first unread message

Toms Bergmanis

unread,
Jan 30, 2017, 8:42:36 AM1/30/17
to fuel-users
Hello, 
I am new to theano/fuel/blocks. As an exercise I am trying to rewrite MNIST LeNet example to work with text, however I don't know how to convert my fuel data stream to an input that would match the format prescribed by blocks.bricks.conv.Convolutional (A 4D tensor with the axes representing batch size, number of channels, image height, and image width). Here is how I would prepare my training stream for that data set as source = tensor.lmatrix('source'), but I am clueless about how to wrap it as a tensor mentioned above.  Any help would be much appreciated.

from fuel.schemes import ShuffledScheme, ConstantScheme
from fuel.streams import DataStream
from toolz.itertoolz import interleave
from fuel.datasets import TextFile
from fuel.transformers import (
Merge, Batch, Filter, Padding, SortMapping, Unpack, Mapping)
def get_train_stream(src_vocab, trg_vocab, src_data, trg_data, batch_size=80):
"""Prepares the training data stream."""

# Load dictionaries and ensure special tokens exist
print(src_vocab)
src_vocab = pickle.load(open(src_vocab, 'rb'))

trg_vocab = pickle.load(open(trg_vocab, 'rb'))

# Get text files from both source and target
src_dataset = TextFile([src_data], src_vocab, bos_token='<w>', eos_token='</w>', unk_token='UNK')
trg_dataset = TextFile([trg_data], trg_vocab, bos_token=None, eos_token=None, unk_token=None)
# Merge them to get a source, target pair
stream = Merge([src_dataset.get_example_stream(), trg_dataset.get_example_stream()], ('source', 'target'))
# Construct batches from the stream with specified batch size
stream = Batch(stream, iteration_scheme=ConstantScheme(batch_size))
masked_stream = PaddingWithEOS(stream, [1, 1])

return masked_stream

Dmitriy Serdyuk

unread,
Jan 30, 2017, 2:57:43 PM1/30/17
to fuel-users
You could use a `Mapping` transformer to do it. Define a function
```python
def add_dimension(batch):
    # I assume that your input is batch[0], it is 2D, (time x batch), every element is an integer in the dictionary
    # First, I put the batch as the first dim:
    inp = batch[0].dimshuffle(1, 0)
    # Then I add two dummy dimensions
    inp = inp[:, :, None, None]
    return inp, batch[1]
```
and apply the transformer `stream = Mapping(add_dimension, data_stream=stream)`.

But probably you want to embed your input using a `Linear` brick first. In this case you can do similar transformation but with theano operations. Remember, that the embedded input will be 3D (time, batch, feature) and you want to transform to (batch, time, feature, dummy) or (batch, time, dummy, feature) depending if you want to convolve over your features.
Reply all
Reply to author
Forward
0 new messages