w3resource

Building a Feedforward neural network in TensorFlow

Python TensorFlow Building and Training a Simple Model: Exercise-4 with Solution

Write a Python program that builds a feedforward neural network using TensorFlow with one hidden layer and a sigmoid activation function.

From Wikipedia -
A feedforward neural network (FNN) is one of the two broad types of artificial neural network, characterized by the direction of information flow between its layers. Its flow is uni-directional, meaning that the information in the model flows in only one direction—forward—from the input nodes, through the hidden nodes (if any) and to the output nodes, without any cycles or loops, in contrast to recurrent neural networks, which have bi-directional flow. Modern feedforward networks are trained using the backpropagation method and are colloquially referred to as the "vanilla" neural networks.

Sample Solution:

Python Code:

from tensorflow import keras

# Replace these values with appropriate values for your problem
input_size = 100  # Number of input features
hidden_units = 32  # Number of units in the hidden layer
output_units = 1  # Number of output units (e.g., 1 for binary classification)

# Define the architecture of the neural network
model = keras.Sequential([
    # Input layer (specify input_shape for the first layer)
    keras.layers.Input(shape=(input_size,)),

    # Hidden layer with sigmoid activation
    keras.layers.Dense(units=hidden_units, activation='sigmoid'),

    # Output layer
    keras.layers.Dense(units=output_units, activation='sigmoid')
])

# Compile the model
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# Print the model summary
model.summary()

Explanation:

In the exercise above -

  • Import TensorFlow and TensorFlow's high-level API keras.
  • Define the neural network architecture using "keras.Sequential". The network consists of an input layer, one hidden layer with a sigmoid activation function, and an output layer with a sigmoid activation function.
  • Compile the model using the compile method. Here we choose an optimizer (e.g., 'adam', 'sgd', 'rmsprop'), a suitable loss function (e.g., 'binary_crossentropy' for binary classification), and any additional evaluation metrics (e.g., 'accuracy').
  • Finally, we print the summary of the model, which provides details about the network's architecture, including the number of parameters in each layer.

Output:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_4 (Dense)             (None, 32)                3232      
                                                                 
 dense_5 (Dense)             (None, 1)                 33        
                                                                 
=================================================================
Total params: 3265 (12.75 KB)
Trainable params: 3265 (12.75 KB)
Non-trainable params: 0 (0.00 Byte)

Explanation(Output):

In the exercise above -

Model: "sequential": This line displays the model's name, which is "sequential."

Layer (type) Output Shape Param #: This header row specifies the columns in the table below.

  • dense_4 (Dense) (None, 32) 3232: This row corresponds to the hidden layer:
    • dense_4 is the name of the layer.
    • (None, 32) indicates the output shape of the layer, where None represents the batch size, and 32 is the number of units in the layer.
    • 3232 is the total number of parameters (weights and biases) in this layer.
  • dense_5 (Dense) (None, 1) 33: This row corresponds to the output layer:
    • dense_5 is the name of the layer.
    • (None, 1) indicates the output shape of the layer.
    • 33 is the total number of parameters in this layer.

Finally:

  • Total params: 3265 (12.75 KB): This line shows the total number of parameters in the entire model and the approximate memory usage in kilobytes (KB).
  • Trainable params: 3265 (12.75 KB): This line specifies the number of trainable parameters, which are the ones updated during training.
  • Non-trainable params: 0 (0.00 Byte): This line indicates that there are no non-trainable parameters in the model.

The model summary provides a concise overview of the neural network architecture. This makes it easier to understand the model's structure and the number of parameters involved in the training process.

Python Code Editor:


Previous: Performing matrix multiplication with TensorFlow in Python.
Next: Creating a TensorFlow placeholder for 3D Images.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/machine-learning/tensorflow/python-tensorflow-building-and-training-exercise-4.php