Felix, I think your code only pads correctly if dim=0.  This is because in the pad vector in the pad_tensor function has *vec.size()[1:] hardcoded into it.  I think you need to create a vector that is pad - vec.size(dim) in the dim dimension and not always in the zeroth dimension.  However, I could be wrong.  I adapted the code to work with python3 and added the ability to pad with different values, so I may have screwed something up in the process.