0% found this document useful (0 votes)
4 views3 pages

Experiment 6

This document outlines an experiment using TensorFlow to predict car sales based on features from a dataset. It includes data preprocessing, model definition, training using a neural network with ReLU activation, and evaluation of the model's performance using Mean Squared Error. The model is trained over 50 epochs with a batch size of 32.

Uploaded by

22pa1a4229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Experiment 6

This document outlines an experiment using TensorFlow to predict car sales based on features from a dataset. It includes data preprocessing, model definition, training using a neural network with ReLU activation, and evaluation of the model's performance using Mean Squared Error. The model is trained over 50 epochs with a batch size of 32.

Uploaded by

22pa1a4229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment-6

import tensorflow as tf

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import StandardScaler

# Load dataset (Assuming a CSV file with relevant features and target column
'PurchaseAmount')

data = pd.read_csv("car_sales.csv")

# Preprocess the dataset

X = data.drop(columns=["PurchaseAmount"]).values

y = data["PurchaseAmount"].values.reshape(-1, 1)

scaler_X = StandardScaler()

scaler_y = StandardScaler()

X = scaler_X.fit_transform(X)

y = scaler_y.fit_transform(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define model parameters

input_size = X_train.shape[1]

hidden_size = 64

output_size = 1

learning_rate = 0.01

epochs = 50
batch_size = 32

# Initialize weights and biases

W1 = tf.Variable(tf.random.normal([input_size, hidden_size]))

b1 = tf.Variable(tf.zeros([hidden_size]))

W2 = tf.Variable(tf.random.normal([hidden_size, output_size]))

b2 = tf.Variable(tf.zeros([output_size]))

# Activation function (ReLU for hidden layer)

def relu(x):

return tf.maximum(0, x)

# Forward propagation

def forward(x):

hidden_layer = relu(tf.matmul(x, W1) + b1)

output_layer = tf.matmul(hidden_layer, W2) + b2

return output_layer

# Loss function (Mean Squared Error)

def compute_loss(y_true, y_pred):

return tf.reduce_mean(tf.square(y_true - y_pred))

# Training loop

for epoch in range(epochs):

for i in range(0, len(X_train), batch_size):

x_batch = X_train[i:i+batch_size]

y_batch = y_train[i:i+batch_size]
with tf.GradientTape() as tape:

output_layer = forward(x_batch)

loss = compute_loss(y_batch, output_layer)

gradients = tape.gradient(loss, [W1, b1, W2, b2])

W1.assign_sub(learning_rate * gradients[0])

b1.assign_sub(learning_rate * gradients[1])

W2.assign_sub(learning_rate * gradients[2])

b2.assign_sub(learning_rate * gradients[3])

print(f"Epoch {epoch+1}, Loss: {loss.numpy():.4f}")

# Evaluate the model

y_pred = forward(X_test)

y_pred = scaler_y.inverse_transform(y_pred.numpy())

y_test = scaler_y.inverse_transform(y_test)

mse = np.mean((y_test - y_pred) ** 2)

print(f"Test Mean Squared Error: {mse:.4f}")

You might also like