3 ms·
For what it's worth, I've found Pytorch to be much more rigid than TF. Maybe I just haven't found the easy way to do things. For example here's a function that
by 0-_-0 6y ago
For what it's worth, I've found Pytorch to be much more rigid than TF. Maybe I just haven't found the easy way to do things. For example here's a function that applies an N×N box filter to all but the first 2 dimensions of a tensor (apologies to mobile users):
def boxfilter(image, N=3):
shape = image.shape
image = tf.reshape(image, [1, shape[0], shape[1], -1])
C = image.shape[-1]
conv = tf.nn.conv2d(image, tf.eye(C, C, [N, N]), 1, "SAME")[0]
return tf.reshape(conv, shape) / (N * N)
Is there a simple way to do this in Pytorch? Preferably without having to inherit from the base class for convolution. It seems to me that Pytorch is like Keras and Tensorflow is like Numpy.
- apl 6y agoYou can almost 1:1 translate this by swapping "tf" and "torch". No need to use nn.Conv2d -- there's a functional API for all these layers: https://pytorch.org/docs/master/nn.functional.html#conv2d https://pytorch.org/docs/master/nn.functional.html#conv2d Torch doesn't have "same" padding, so you have to manually calculate the correct padding value for your input/output shapes.
- 0-_-0 6y agoThat looks great, thanks!