0
0
TensorFlowml~5 mins

First neural network in TensorFlow

Choose your learning style9 modes available
Introduction

A neural network helps a computer learn patterns from data to make decisions or predictions.

When you want to recognize handwritten numbers.
When you want to predict if an email is spam or not.
When you want to classify images into categories like cats or dogs.
When you want to predict house prices based on features.
When you want to understand simple relationships in data.
Syntax
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Dense(units=10, activation='relu', input_shape=(784,)),
    layers.Dense(units=1, activation='sigmoid')
])

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

model.fit(x_train, y_train, epochs=5)

Sequential means layers are stacked one after another.

Dense is a fully connected layer where each input connects to each output.

Examples
A small network with 3 inputs, one hidden layer with 5 neurons, and one output neuron.
TensorFlow
model = models.Sequential([
    layers.Dense(5, activation='relu', input_shape=(3,)),
    layers.Dense(1, activation='sigmoid')
])
Compile model for regression using mean squared error loss and stochastic gradient descent optimizer.
TensorFlow
model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['mse'])
Train the model for 10 rounds with batches of 32 samples.
TensorFlow
model.fit(x_train, y_train, epochs=10, batch_size=32)
Sample Model

This program creates a tiny dataset where the output is 1 if the sum of inputs is greater than or equal to 1, else 0. It builds a small neural network with one hidden layer, trains it, and shows predictions and accuracy.

TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np

# Create simple data: inputs are 2 numbers, output is 1 if sum >= 1 else 0
x_train = np.array([[0,0],[0,1],[1,0],[1,1]], dtype=float)
y_train = np.array([0,1,1,1], dtype=float)

# Build model
model = models.Sequential([
    layers.Dense(4, activation='relu', input_shape=(2,)),
    layers.Dense(1, activation='sigmoid')
])

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

# Train model
history = model.fit(x_train, y_train, epochs=20, verbose=0)

# Predict on training data
predictions = model.predict(x_train)

# Print predictions rounded to 2 decimals
for i, pred in enumerate(predictions):
    print(f"Input: {x_train[i]}, Prediction: {pred[0]:.2f}, Actual: {y_train[i]}")

# Print final accuracy
final_acc = history.history['accuracy'][-1]
print(f"Final training accuracy: {final_acc:.2f}")
OutputSuccess
Important Notes

Use relu activation to help the network learn complex patterns.

sigmoid activation is good for outputs between 0 and 1, like yes/no.

Training for more epochs usually improves accuracy but watch out for overfitting.

Summary

A neural network learns from data by adjusting connections between layers.

Start with a simple model: input layer, one hidden layer, and output layer.

Use compile to set how the model learns and fit to train it.