@@ -1412,14 +1412,15 @@ class ZeroPadding1D(Layer):
1412
1412
'''Zero-padding layer for 1D input (e.g. temporal sequence).
1413
1413
1414
1414
# Arguments
1415
- padding: int or tuple of int (length 2) or dictionary
1416
- For symmetric padding: int
1415
+ padding: int, or tuple of int (length 2), or dictionary.
1416
+ - If int:
1417
1417
How many zeros to add at the beginning and end of
1418
1418
the padding dimension (axis 1).
1419
- For asymmetric padding: tuple of int (length 2)
1419
+ - If tuple of int (length 2)
1420
1420
How many zeros to add at the beginning and at the end of
1421
- the padding dimension '(left_pad, right_pad)' or
1422
- '{'left_pad': left_pad, 'right_pad': right_pad}'.
1421
+ the padding dimension, in order '(left_pad, right_pad)'.
1422
+ - If dictionary: should contain the keys
1423
+ {'left_pad', 'right_pad'}.
1423
1424
If any key is missing, default value of 0 will be used for the missing key.
1424
1425
1425
1426
# Input shape
@@ -1436,15 +1437,21 @@ def __init__(self, padding=1, **kwargs):
1436
1437
if isinstance (padding , int ):
1437
1438
self .left_pad = padding
1438
1439
self .right_pad = padding
1440
+
1439
1441
elif isinstance (padding , dict ):
1440
1442
if set (padding .keys ()) <= {'left_pad' , 'right_pad' }:
1441
1443
self .left_pad = padding .get ('left_pad' , 0 )
1442
1444
self .right_pad = padding .get ('right_pad' , 0 )
1443
1445
else :
1444
- raise ValueError ('Unexpected key is found in the padding argument. '
1445
- 'Keys have to be in {"left_pad", "right_pad"}' )
1446
+ raise ValueError ('Unexpected key found in `padding` dictionary. '
1447
+ 'Keys have to be in {"left_pad", "right_pad"}. '
1448
+ 'Found: ' + str (padding .keys ()))
1446
1449
else :
1447
1450
padding = tuple (padding )
1451
+ if len (padding ) != 2 :
1452
+ raise ValueError ('`padding` should be int, or dict with keys '
1453
+ '{"left_pad", "right_pad"}, or tuple of length 2. '
1454
+ 'Found: ' + str (padding ))
1448
1455
self .left_pad = padding [0 ]
1449
1456
self .right_pad = padding [1 ]
1450
1457
self .input_spec = [InputSpec (ndim = 3 )]
@@ -1468,15 +1475,16 @@ class ZeroPadding2D(Layer):
1468
1475
'''Zero-padding layer for 2D input (e.g. picture).
1469
1476
1470
1477
# Arguments
1471
- padding: tuple of int (length 2) or tuple of int (length 4) or dictionary
1472
- For symmetric padding tuple of int (length 2)
1478
+ padding: tuple of int (length 2), or tuple of int (length 4), or dictionary.
1479
+ - If tuple of int (length 2):
1473
1480
How many zeros to add at the beginning and end of
1474
1481
the 2 padding dimensions (rows and cols).
1475
- For asymmetric padding tuple of int (length 4)
1482
+ - If tuple of int (length 4):
1476
1483
How many zeros to add at the beginning and at the end of
1477
- the 2 padding dimensions (rows and cols).
1478
- '(top_pad, bottom_pad, left_pad, right_pad)' or
1479
- '{'top_pad': top_pad, 'bottom_pad': bottom_pad, 'left_pad': left_pad, 'right_pad': right_pad}'
1484
+ the 2 padding dimensions (rows and cols), in the order
1485
+ '(top_pad, bottom_pad, left_pad, right_pad)'.
1486
+ - If dictionary: should contain the keys
1487
+ {'top_pad', 'bottom_pad', 'left_pad', 'right_pad'}.
1480
1488
If any key is missing, default value of 0 will be used for the missing key.
1481
1489
dim_ordering: 'th' or 'tf'.
1482
1490
In 'th' mode, the channels dimension (the depth)
@@ -1507,16 +1515,18 @@ def __init__(self,
1507
1515
dim_ordering = K .image_dim_ordering ()
1508
1516
1509
1517
self .padding = padding
1510
- try :
1518
+ if isinstance ( padding , dict ) :
1511
1519
if set (padding .keys ()) <= {'top_pad' , 'bottom_pad' , 'left_pad' , 'right_pad' }:
1512
1520
self .top_pad = padding .get ('top_pad' , 0 )
1513
1521
self .bottom_pad = padding .get ('bottom_pad' , 0 )
1514
1522
self .left_pad = padding .get ('left_pad' , 0 )
1515
1523
self .right_pad = padding .get ('right_pad' , 0 )
1516
1524
else :
1517
- raise ValueError ('Unexpected key is found in the padding argument. '
1518
- 'Keys have to be in {"top_pad", "bottom_pad", "left_pad", "right_pad"}' )
1519
- except AttributeError :
1525
+ raise ValueError ('Unexpected key found in `padding` dictionary. '
1526
+ 'Keys have to be in {"top_pad", "bottom_pad", '
1527
+ '"left_pad", "right_pad"}.'
1528
+ 'Found: ' + str (padding .keys ()))
1529
+ else :
1520
1530
padding = tuple (padding )
1521
1531
if len (padding ) == 2 :
1522
1532
self .top_pad = padding [0 ]
@@ -1529,9 +1539,11 @@ def __init__(self,
1529
1539
self .left_pad = padding [2 ]
1530
1540
self .right_pad = padding [3 ]
1531
1541
else :
1532
- raise TypeError ('padding should be tuple of int of length 2 or 4, or dict' )
1542
+ raise TypeError ('`padding` should be tuple of int '
1543
+ 'of length 2 or 4, or dict. '
1544
+ 'Found: ' + str (padding ))
1533
1545
1534
- assert dim_ordering in {'tf' , 'th' }, 'dim_ordering must be in {tf, th} '
1546
+ assert dim_ordering in {'tf' , 'th' }, '` dim_ordering` must be in {"tf", "th"}. '
1535
1547
self .dim_ordering = dim_ordering
1536
1548
self .input_spec = [InputSpec (ndim = 4 )]
1537
1549
@@ -1574,6 +1586,7 @@ class ZeroPadding3D(Layer):
1574
1586
padding: tuple of int (length 3)
1575
1587
How many zeros to add at the beginning and end of
1576
1588
the 3 padding dimensions (axis 3, 4 and 5).
1589
+ Currentl only symmetric padding is supported.
1577
1590
dim_ordering: 'th' or 'tf'.
1578
1591
In 'th' mode, the channels dimension (the depth)
1579
1592
is at index 1, in 'tf' mode is it at index 4.
0 commit comments