Creating input (x) and target (y) for RNNs

When creating training data for RNN, the target label for a given input label is the input label itself but shifted by one position. Please refer to the diagram below.

charRNN@0.5x

Since the characters are number coded, numpy functions can be invoked. numpy.roll is particularly useful in this regard.

import numpy as np
x = np.random.randint(0,100, size=(3,3))
x
array([[29, 82, 83],
       [79, 38, 12],
       [21, 20, 56]])
# To shift along columns by 1
np.roll(x ,shift = 1, axis = 1)
array([[83, 29, 82],
       [12, 79, 38],
       [56, 21, 20]])

shift parameter determines how many positions to offset and axis argument determines the axis along which transformation has to be done.

Additional demo of num​py.roll

# To shift along rows by 1
np.roll(x ,shift = 1, axis = 0) 
array([[21, 20, 56],
           [29, 82, 83],
           [79, 38, 12]])
# To shift along columns by 2
np.roll(x ,shift = 2, axis = 1)
array([[82, 83, 29],
       [38, 12, 79],
       [20, 56, 21]])

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s