A neural network helps a computer learn patterns from data to make decisions or predictions.
First neural network in 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.
model = models.Sequential([
layers.Dense(5, activation='relu', input_shape=(3,)),
layers.Dense(1, activation='sigmoid')
])model.compile(optimizer='sgd', loss='mean_squared_error', metrics=['mse'])
model.fit(x_train, y_train, epochs=10, batch_size=32)
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.
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}")
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.
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.