

def pad_sequences(sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre', value=0.0):
    "Pads each sequence to the same length:\n    the length of the longest sequence.\n\n    If maxlen is provided, any sequence longer\n    than maxlen is truncated to maxlen.\n    Truncation happens off either the beginning (default) or\n    the end of the sequence.\n\n    Supports post-padding and pre-padding (default).\n\n    # Arguments\n        sequences: list of lists where each element is a sequence\n        maxlen: int, maximum length\n        dtype: type to cast the resulting sequence.\n        padding: 'pre' or 'post', pad either before or after each sequence.\n        truncating: 'pre' or 'post', remove values from sequences larger than\n            maxlen either in the beginning or in the end of the sequence\n        value: float, value to pad the sequences to the desired value.\n\n    # Returns\n        x: numpy array with dimensions (number_of_sequences, maxlen)\n    "
    lengths = [len(s) for s in sequences]
    nb_samples = len(sequences)
    if (maxlen is None):
        maxlen = np.max(lengths)
    sample_shape = tuple()
    for s in sequences:
        if (len(s) > 0):
            sample_shape = np.asarray(s).shape[1:]
            break
    x = (np.ones(((nb_samples, maxlen) + sample_shape)) * value).astype(dtype)
    for (idx, s) in enumerate(sequences):
        if (len(s) == 0):
            continue
        if (truncating == 'pre'):
            trunc = s[(- maxlen):]
        elif (truncating == 'post'):
            trunc = s[:maxlen]
        else:
            raise ValueError(('Truncating type "%s" not understood' % truncating))
        trunc = np.asarray(trunc, dtype=dtype)
        if (trunc.shape[1:] != sample_shape):
            raise ValueError(('Shape of sample %s of sequence at position %s is different from expected shape %s' % (trunc.shape[1:], idx, sample_shape)))
        if (padding == 'post'):
            x[idx, :len(trunc)] = trunc
        elif (padding == 'pre'):
            x[idx, (- len(trunc)):] = trunc
        else:
            raise ValueError(('Padding type "%s" not understood' % padding))
    return x

dataset/ETHPy150Open fchollet/keras/keras/preprocessing/sequence.py pad_sequences/original