Keras - Locally connected layer



Locally connected layers are similar to Conv1D layer but the difference is Conv1D layer weights are shared but here weights are unshared. We can use different set of filters to apply different patch of input.

Locally connected layer has one arguments and it is as follows −

keras.layers.LocallyConnected1D(n)

A simple example to use Locally connected 1D layer is as follows −

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense,LocallyConnected1D 
>>> model = Sequential() 

# apply a unshared weight convolution 1-dimension of length 3 to a sequence with 
# 10 timesteps, with 16 output filters 

>>> model.add(LocallyConnected1D(16, 3, input_shape = (10, 8))) 

# add a new conv1d on top 
>>> model.add(LocallyConnected1D(8, 3))

The signature of the Locally connected 1D layer function and its arguments with default value is as follows −

keras.layers.LocallyConnected1D (
   filters, 
   kernel_size, 
   strides = 1, 
   padding = 'valid', 
   data_format = None, 
   activation = None, 
   use_bias = True, 
   kernel_initializer = 'glorot_uniform', 
   bias_initializer = 'zeros', 
   kernel_regularizer = None, 
   bias_regularizer = None, 
   activity_regularizer = None, 
   kernel_constraint = None, 
   bias_constraint = None
)

Here,

  • kernel_initializer refers initializer for the kernel weights matrix

  • kernel_regularizer is used to apply regularize function to the kernel weights matrix.

  • bias_regularizer is used to apply regularizer function to the bias vector.

  • activity_regularizer is used to apply regularizer function to the output of the layer.

Similarly, we can use 2D and 3D layers as well.

Recurrent Layer

It is used in Recurrent neural networks(RNN). It is defined as shown below −

keras.engine.base_layer.wrapped_fn()

It supports the following parameters −

  • cell refers an instance.

  • return_sequences return the last output in the output sequence, or the full sequence.

  • return_state returns the last state in addition to the output.

  • go_backwards returns a boolean result. If the value is true, then process the input sequence backwards otherwise return the reversed sequence.

  • stateful refers the state for each index.

  • unroll specifies whether the network to be unrolled or not.

  • input_dim refers the input dimension.

  • input_length refers the length of input sequence.

Advertisements