MCPcopy Create free account
hub / github.com/apache/singa / pad_helper

Method pad_helper

test/python/test_operation.py:3278–3343  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

3276 self.expand_helper(gpu_dev)
3277
3278 def pad_helper(self, dev):
3279 X = np.array([
3280 [1.0, 1.2],
3281 [2.3, 3.4],
3282 [4.5, 5.7],
3283 ]).astype(np.float32)
3284 Y1 = np.array([
3285 [0.0, 0.0, 1.0, 1.2],
3286 [0.0, 0.0, 2.3, 3.4],
3287 [0.0, 0.0, 4.5, 5.7],
3288 ],).astype(np.float32)
3289 Y2 = np.array([
3290 [1.0, 1.2, 1.0, 1.2],
3291 [2.3, 3.4, 2.3, 3.4],
3292 [4.5, 5.7, 4.5, 5.7],
3293 ],).astype(np.float32)
3294 Y3 = np.array([
3295 [1.0, 1.0, 1.0, 1.2],
3296 [2.3, 2.3, 2.3, 3.4],
3297 [4.5, 4.5, 4.5, 5.7],
3298 ],).astype(np.float32)
3299
3300 x = tensor.from_numpy(X)
3301 x.to_device(dev)
3302 pads = [0, 2, 0, 0]
3303
3304 DY = np.random.randn(3, 4).astype(np.float32)
3305 dy = tensor.from_numpy(DY)
3306 dy.to_device(dev)
3307
3308 y1 = autograd.pad(x, "constant", pads)
3309 y2 = autograd.pad(x, "reflect", pads)
3310 y3 = autograd.pad(x, "edge", pads)
3311 dx1 = y1.creator.backward(dy.data)
3312 dx2 = y2.creator.backward(dy.data)
3313 dx3 = y3.creator.backward(dy.data)
3314 pad_width = []
3315 half_width = len(pads) // 2
3316 for i in range(half_width):
3317 pad_width += [[pads[i], pads[i + half_width]]]
3318
3319 np.testing.assert_array_almost_equal(tensor.to_numpy(y1),
3320 np.pad(
3321 X,
3322 pad_width=pad_width,
3323 mode="constant",
3324 constant_values=0.,
3325 ),
3326 decimal=5)
3327 np.testing.assert_array_almost_equal(tensor.to_numpy(y2),
3328 np.pad(
3329 X,
3330 pad_width=pad_width,
3331 mode="reflect",
3332 ),
3333 decimal=5)
3334 np.testing.assert_array_almost_equal(tensor.to_numpy(y3),
3335 np.pad(

Callers 2

test_pad_cpuMethod · 0.95
test_pad_gpuMethod · 0.95

Calls 4

check_shapeMethod · 0.95
shapeMethod · 0.80
to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected