{"id":4151,"date":"2023-11-04T23:14:05","date_gmt":"2023-11-04T23:14:05","guid":{"rendered":"http:\/\/localhost:10003\/building-neural-networks-with-tensorflow\/"},"modified":"2023-11-05T05:47:59","modified_gmt":"2023-11-05T05:47:59","slug":"building-neural-networks-with-tensorflow","status":"publish","type":"post","link":"http:\/\/localhost:10003\/building-neural-networks-with-tensorflow\/","title":{"rendered":"Building neural networks with TensorFlow"},"content":{"rendered":"
Artificial Neural Networks, also referred to as neural networks or ANNs, are a subset of machine learning models that are designed to recognize patterns and relationships in data. They are modeled after the human brain and consist of a series of interconnected nodes that process and transmit information.<\/p>\n
TensorFlow is an open-source software library created by Google for numerical computation and machine learning. It provides a flexible platform for building and training neural networks, making it the go-to tool for anyone looking to get started with artificial intelligence.<\/p>\n
In this tutorial, we will take a deep dive into building neural networks with TensorFlow. We’ll cover the basics of neural networks, explore the TensorFlow framework, and create a simple neural network that can recognize handwritten digits.<\/p>\n
Before we get started, make sure you have the following prerequisites installed:<\/p>\n
Before we dive into building a neural network with TensorFlow, let’s first understand what a neural network is and how it works.<\/p>\n
As previously mentioned, a neural network is designed to recognize patterns and relationships in data. The basic building block of a neural network is called a neuron. Neurons are connected to each other through nodes, which are similar to the synapses in the human brain.<\/p>\n
Neurons compute using an input signal and a set of trainable parameters, producing an output signal. Neural networks consist of layers of interconnected neurons, each layer processing the output signals of the previous layer. The final layer produces the output of the neural network, which is the result of the computation.<\/p>\n
There are several types of neural networks, but the most commonly used ones are:<\/p>\n
A feedforward neural network is the most basic type of neural network. Data flows from the input layer, through the hidden layers, and to the output layer. The nodes in the hidden layers perform computations on the input and produce an output, which in turn feeds into the next layer.<\/p>\n
Convolutional neural networks, or CNNs, are commonly used in image recognition. They use convolutional layers to filter the inputs and identify features in the data. The output of the convolutional layer is then passed through one or more fully connected layers to produce the final output.<\/p>\n
Recurrent neural networks, or RNNs, are commonly used in natural language processing and speech recognition. They are designed to process sequences of data, where the output of one state is fed back into the input of the next state. This allows the network to remember previous inputs and output sequences instead of just individual inputs.<\/p>\n
Neural networks learn by adjusting the values of the parameters in the nodes. The process of adjusting these parameters is called training.<\/p>\n
The training process involves providing the neural network with a large amount of labeled data and adjusting the weights of the nodes to minimize the error between the predicted output and the actual output. This is done using an optimization algorithm such as stochastic gradient descent.<\/p>\n
There are several other techniques that can be used to improve the learning process, such as regularization, dropout, and batch normalization.<\/p>\n
Now that we have a good understanding of neural networks, let’s start building one with TensorFlow.<\/p>\n
To install TensorFlow, open up a terminal or command prompt and enter the following command:<\/p>\n
pip install tensorflow\n<\/code><\/pre>\nCreating a Neural Network<\/h3>\n
Let’s create a simple neural network that can recognize handwritten digits from the MNIST dataset.<\/p>\n
Loading the Data<\/h4>\n
First, we need to load the data. TensorFlow provides a convenient function for loading the MNIST dataset:<\/p>\n
import tensorflow as tf\nfrom tensorflow import keras\n\n(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n<\/code><\/pre>\nThis will load the data into four NumPy arrays:<\/p>\n
\nx_train<\/code> – training images<\/li>\ny_train<\/code> – training labels<\/li>\nx_test<\/code> – testing images<\/li>\ny_test<\/code> – testing labels<\/li>\n<\/ul>\nPreprocessing the Data<\/h4>\n
Next, we need to preprocess the data. We’ll do this by normalizing the pixel values to be between 0 and 1:<\/p>\n
x_train = x_train.astype(\"float32\") \/ 255\nx_test = x_test.astype(\"float32\") \/ 255\n<\/code><\/pre>\nWe’ll also convert the labels to one-hot encoding using the to_categorical<\/code> function:<\/p>\ny_train = keras.utils.to_categorical(y_train)\ny_test = keras.utils.to_categorical(y_test)\n<\/code><\/pre>\nDefining the Model<\/h4>\n
Now, we can define the model. We’ll start with a simple feedforward neural network with two hidden layers:<\/p>\n
model = keras.Sequential([\n keras.layers.Flatten(input_shape=(28, 28)),\n keras.layers.Dense(units=128, activation=\"relu\"),\n keras.layers.Dense(units=64, activation=\"relu\"),\n keras.layers.Dense(units=10, activation=\"softmax\")\n])\n<\/code><\/pre>\nThis model has the following layers:<\/p>\n
\n- Flatten – reshapes the 28×28 input image into a 1D array<\/li>\n
- Dense – a fully connected layer with 128 units and ReLU activation<\/li>\n
- Dense – a fully connected layer with 64 units and ReLU activation<\/li>\n
- Dense – a fully connected layer with 10 units (one for each digit) and softmax activation<\/li>\n<\/ul>\n
Compiling the Model<\/h4>\n
Now that we’ve defined the model, we need to compile it. We’ll use categorical cross-entropy loss for the loss function and the Adam optimizer:<\/p>\n
model.compile(\n loss=\"categorical_crossentropy\",\n optimizer=\"adam\",\n metrics=[\"accuracy\"]\n)\n<\/code><\/pre>\nTraining the Model<\/h4>\n
Finally, we can train the model. We’ll use a batch size of 32, train for 10 epochs, and use the test set for validation:<\/p>\n
model.fit(\n x_train,\n y_train,\n batch_size=32,\n epochs=10,\n validation_data=(x_test, y_test)\n)\n<\/code><\/pre>\nAfter training, we can evaluate the model on the test set:<\/p>\n
test_loss, test_acc = model.evaluate(x_test, y_test)\nprint(\"Test accuracy:\", test_acc)\n<\/code><\/pre>\nOur final accuracy on the test set should be around 98%.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we learned the basics of neural networks and how to build one using TensorFlow. We created a simple feedforward neural network to recognize handwritten digits and achieved an accuracy of around 98% on the test set.<\/p>\n
TensorFlow provides a powerful platform for building and training neural networks, and with the techniques discussed in this tutorial, you should be able to build more complex models and improve their performance.<\/p>\n","protected":false},"excerpt":{"rendered":"
Artificial Neural Networks, also referred to as neural networks or ANNs, are a subset of machine learning models that are designed to recognize patterns and relationships in data. They are modeled after the human brain and consist of a series of interconnected nodes that process and transmit information. TensorFlow is Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[373,39,230,41,337,1481,322,1480],"yoast_head":"\nBuilding neural networks with TensorFlow - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n