DEV Community

Cover image for TensorFlow for Beginners: Your First Neural Network
Raj Prajapati
Raj Prajapati

Posted on

TensorFlow for Beginners: Your First Neural Network

Introduction
So, you're curious about artificial intelligence and want to dive into the world of deep learning? Great choice! TensorFlow, a powerful open-source platform, is a fantastic starting point. In this post, we'll walk you through building your very first neural network. No prior experience? Don't worry, we'll break it down step-by-step.

What is TensorFlow?

TensorFlow is like a versatile toolkit for creating and training various machine learning models. It's used for everything from image recognition to natural language processing. Think of it as your trusty companion on this AI journey.

Setting Up Your Environment
Before we start coding, let's ensure you have the right tools. You'll need:

Python installed
TensorFlow installed (use pip install tensorflow in your terminal)
A basic understanding of Python programming

Building Your First Neural Network

  1. Import Necessary Libraries:

Python

import tensorflow as tf
import numpy as np

Prepare Your Data:

For simplicity, we'll use a small dataset. Imagine you want to predict house prices based on square footage.

Python

Sample data

houses = np.array([1000, 1500, 2000, 2500, 3000])
prices = np.array([100, 150, 200, 250, 300])

Create the Model:

Python
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=[1])
])

Compile the Model:

Python
model.compile(loss='mean_squared_error', optimizer='adam')

Train the Model:

Python
model.fit(houses, prices, epochs=100)

Make Predictions:

Python
new_house = np.array([1800])
predicted_price = model.predict(new_house)
print(predicted_price)

Understanding the Code

  • We imported TensorFlow and NumPy.
  • Created sample house sizes and prices.
  • Defined a simple neural network with one layer.
  • Compiled the model with a loss function and optimizer.
  • Trained the model on our data.
  • Predicted the price of a new house.

Conclusion

Congratulations! You've built your first neural network using TensorFlow. While this is a basic example, it lays the foundation for more complex models. Remember, practice is key. Experiment with different datasets, architectures, and hyperparameters to improve your skills.

Top comments (0)