import matplotlib.pyplot as plt
import numpy as np
import paddle
from paddle.nn import Conv2D
from paddle.nn.initializer import Assign
%matplotlib inline
w = np.array([1, 0, -1], dtype='float32')
w = w.reshape([1, 1, 1, 3])
conv = Conv2D(in_channels=1, out_channels=1, kernel_size=[1, 3],
weight_attr=paddle.ParamAttr(
initializer=Assign(value=w)))
img = np.ones([50,50], dtype='float32')
img[:, 30:] = 0.
x = img.reshape([1,1,50,50])
x = paddle.to_tensor(x)
y = conv(x)
out = y.numpy()
f = plt.subplot(121)
f.set_title('input image', fontsize=15)
plt.imshow(img, cmap='gray')
f = plt.subplot(122)
f.set_title('output featuremap', fontsize=15)
plt.imshow(out.squeeze(), cmap='gray')
plt.show()