L1Norm layer
- class layers.l1norm_layer.L1Norm_layer(input_shape=None, axis=None, **kwargs)[source]
Bases:
NumPyNet.layers.base.BaseLayer
L1Norm layer
- Parameters
input_shape (tuple (default=None)) – Shape of the input in the format (batch, w, h, c), None is used when the layer is part of a Network model.
axis (integer, default None.) – Axis along which the L1Normalization is performed. If None, normalize the entire array.
Example
>>> import os >>> >>> import pylab as plt >>> from PIL import Image >>> >>> img_2_float = lambda im : ((im - im.min()) * (1./(im.max() - im.min()) * 1.)).astype(float) >>> float_2_img = lambda im : ((im - im.min()) * (1./(im.max() - im.min()) * 255.)).astype(np.uint8) >>> >>> filename = os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'dog.jpg') >>> inpt = np.asarray(Image.open(filename), dtype=float) >>> inpt.setflags(write=1) >>> inpt = img_2_float(inpt) >>> >>> # add batch = 1 >>> inpt = np.expand_dims(inpt, axis=0) >>> >>> layer = L1Norm_layer(input_shape=inpt.shape) >>> >>> # FORWARD >>> >>> layer.forward(inpt) >>> forward_out = layer.output >>> print(layer) >>> >>> # BACKWARD >>> >>> delta = np.zeros(shape=inpt.shape, dtype=float) >>> layer.backward(delta, copy=True) >>> >>> # Visualizations >>> >>> fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(10, 5)) >>> fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.15) >>> >>> fig.suptitle('L1Normalization Layer') >>> >>> ax1.imshow(float_2_img(inpt[0])) >>> ax1.set_title('Original image') >>> ax1.axis('off') >>> >>> ax2.imshow(float_2_img(forward_out[0])) >>> ax2.set_title("Forward") >>> ax2.axis("off") >>> >>> ax3.imshow(float_2_img(delta[0])) >>> ax3.set_title('Backward') >>> ax3.axis('off') >>> >>> fig.tight_layout() >>> plt.show()
TODO
- backward(delta)[source]
Backward function of the l1norm_layer
- Parameters
delta (array-like) – delta array of shape (batch, w, h, c). Global delta to be backpropagated.
- Return type
self
- forward(inpt)[source]
Forward of the l1norm layer, apply the l1 normalization over the input along the given axis
- Parameters
inpt (array-like) – Input batch of images in format (batch, in_w, in_h, in _c)
- Return type
self
- property out_shape
Get the output shape
- Returns
out_shape – Tuple as (batch, out_w, out_h, out_c)
- Return type
tuple