## Introduction to Keras Dense

Keras dense is one of the widely used layers inside the keras model or neural network where all the connections are made very deeply. In other words, the neurons in the dense layer get their source of input data from all the other neurons of the previous layer of the network. In this article, we will study keras dense and focus on the pointers like What is keras dense, keras dense network output, keras dense common methods, keras dense Parameters, Keras dense Dense example, and Conclusion about the same.

### What is keras dense?

Keras dense is one of the available layers in keras models, most oftenly added in the neural networks. This layer contains densely connected neurons. Each of the individual neurons of the layer takes the input data from all the other neurons before a currently existing one.

Internally, the dense layer is where various multiplication of matrix vectors is carried out. We can train the values inside the matrix as they are nothing but the parameters. We can even update these values using a methodology called backpropagation.

The dense layer produces the resultant output as the vector, which is m dimensional in size. This is why the dense layer is most often used for vector manipulation to change the dimensions of the vectors. The dense layer can also perform the vectors’ translation, scaling, and rotation operations.

### keras dense network output

The dense layer of keras gives the following output after operating activation, as shown by the below equation –

`Output of the keras dense = activation (dot (kernel, input) +bias)`

The above formula uses a kernel, which is used for the generated weight matrix from the layer, activation helps in carrying out the activation in element-wise format, and the bias value is the vector of bias generated by the dense layer.

The output of the dense layer is the dot product of the weight matrix or kernel and tensor passed as input. Further, the value of the bias vector is added to the output value, and then it goes through the activation process carried out element-wise.

### keras dense common methods

The dense layer has the following methods that are used for its manipulations and operations –

**Get_weights method**– It has the syntax as sampleLayer. Get_weights() helps retrieve the current weights as arrays of NumPy associated with the layers. This method returns the output containing the weight values in the list format of numPy arrays.**Set_weights method**– This method is used for setting the value of weights for the layer in the form of NumPy arrays. The syntax of the method is sampleEducbaLayer. Set_weights( value_of_weights). The parameter value_of_weights is the list of NumPy arrays. Note that here the shape and the number of array values specified must match the dimension of the weight. That is, nothing but the size of the array should match the resultant generated from the get_weights method.**Get_config method**– This method is used for retrieving the configurations of the layer and has its syntax as sampleEducbaLayer.get_config().**Add_loss method**– This method adds the loss tensor or tensors most potentially dependent on the input layers. It has the syntax as sampleEducbaLayer.add_loss (losses, **kwargs)**Add_metric method**– The method proves helpful when adding metric tensor to the existing created layer. The syntax of the method is sampleEducbaLayer.add_metric( value, **kwargs, name = None)

### keras dense Parameters

The syntax of the dense layer is as shown below –

`Keras. layers. Dense( bias_initializer = ‘zeros’, use_bias = True, activation = None, units, kernel_initializer = ‘glorot_uniform’, bias_constraint = None, activity_regularizer = None, kernel_regularizer = None, kernel_constraint = None, bias_regularizer = None)`

Let us study all the parameters that are passed to the Dense layer and their relevance in detail –

- Activation – It has a key role in applying element-wise activation function execution. When not specified, the default value is linear activation, and the same is used, but it is free for a change. We can change this activation to any other per requirement by using many available options in keras.
- Units – It is a positive integer and a basic parameter used to specify the size of the output generated from the layer. It has relevance in the weight matrix, which helps specify its size and the bias vector.
- Use_bias – This parameter is used to decide whether to include the value of the bias vector in the manipulations and calculations that will be done or not. The default value is true when we don’t specify its value.
- Regularizers – It has three parameters for performing penalty or regularization of the built model. Usually not used, but when specified helps in the model generalization.
- Initializers – It provides the input values for deciding how layer values will be initialized. We have the bias vector and weight matrix in the dense layer, which should be initialized.
- Constraints – These parameters help specify if the bias vector or weight matrix will consider any applied constraints.

### Keras Dense example

Let us consider a sample example to demonstrate the creation of the sequential model in which we will add two layers of the dense layer in the model –

`sampleEducbaModel = tensorflow.keras.models.Sequential()`

sampleEducbaModel.add(tensorflow.keras.Input(shape=(16,)))

sampleEducbaModel.add(tensorflow.keras.layers.Dense(32, activation='relu'))

sampleEducbaModel.add(tensorflow.keras.layers.Dense(32))

print(sampleEducbaModel.output_shape)

print(sampleEducbaModel.layers)

print(sampleEducbaModel.compute_output_signature)

The output of the code snippet after execution is as shown below –

### Conclusion

Keras Dense layer is the layer that contains all the neurons that are deeply connected within themselves. This means that every neuron in the dense layer takes the input from all the other neurons of the previous layer. We can add as many dense layers as required. It is one of the most commonly used layers.

### Recommended Articles

This is a guide to Keras Dense. Here we discuss keras dense network output, keras dense common methods, Parameters, Keras Dense example, and Conclusion. You may also look at the following articles to learn more –