B.
Tech Artificial intelligence and data science
EX NO:1 B IMPLEMENTATION OF SINGLE PERCEPTRON BOOLEAN
DATE :26.6.23 FUNCTION USING TENSORFLOW
AIM:
To implement the single perceptron boolean function using tensorflow.
PROCEDURE:
● Importing the tensorflow package as tf.
● Create a class called perceptron.
● Create a constructor having features, learning rate, weights and bias.
● Create a function to activate
● Updating the weight and bias
● Passing the train and test data for prediction.
PROGRAM:
class perceptron:
def __init__(self,num_features,learning_rate):
self.num_features=num_features
self.learning_rate=learning_rate
self.weights=[0.0]*num_features
self.bias=0.0
def predict(self,inputs):
activation=sum(w*x for w,x in zip(self.weights,inputs))
return 1 if activation>=0 else 0
def train(self,training_data,num_epoch):
for _ in range(num_epoch):
for inputs,target in training_data:
prediction=self.predict(inputs)
error = target - prediction
if error !=0:
#update the weight and bias
self.weights=[w+self.learning_rate*error*x for w,x in zip(self.weights,inputs)]
self.bias=self.bias+self.learning_rate *error
training_data=[([0,0],0),
717821i139-Nandhitha
B.Tech Artificial intelligence and data science
([0,1],1),
([1,0],1),
([1,1],(1))]
p=perceptron(2,0.1)
p.train(training_data,10)
test_data=[([0,0]),
([0,1]),
([1,0]),
([1,1])]
for inputs in test_data:
prediction=p.predict(inputs)
print(f"inputs:{inputs} prediction:{prediction}")
OUTPUT:
RESULT:
Thus the program to implement arithmetic operation in tensorflow has been executed and
completed successfully.
717821i139-Nandhitha
B.Tech Artificial intelligence and data science
EX NO: 2 A IMPLEMENTATION OF REGRESSION MODELS IN KERAS
DATE:10.7.23
AIM:
To write a code to implement regression models in keras using python.
PROCEDURE:
❖ Import the data set .
❖ Check the null values in the data set.
❖ Split the data into x ,y and split test and train the model.
❖ Import tensorflow and keras
❖ Declare the hidden layers of neurons.
❖ Build the model using optimizer, loss and metrics.
❖ Plot the train and test data of the model.
PROGRAM:
#Importing the necessary library
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
#Import the google drive
from google.colab import drive
drive.mount('/content/drive')
#importing the data
data=pd.read_csv('drive/My Drive/test.csv')
#checking the null values
data.isnull().sum()
#splitting x and y
x=data.iloc[:,0:1]
717821i139-Nandhitha
B.Tech Artificial intelligence and data science
y=data.iloc[:,1:]
Y
#train and test the data
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)
#defining the hidden layers of neurons using keras
model=keras.Sequential()
model.add(keras.layers.Dense(100,input_dim=1,activation='relu'))
model.add(keras.layers.Dense(200,input_dim=100,activation='relu'))
model.add(keras.layers.Dense(200,input_dim=200,activation='relu'))
model.add(keras.layers.Dense(1,input_dim=200))
#viewing the summary
model.summary()
717821i139-Nandhitha
B.Tech Artificial intelligence and data science
#Compiling the model
model.compile(optimizer='adam',loss='mean_squared_error',metrics='mse')
model.fit(x_train,y_train,epochs=100)
#plotting test model
plt.title("test data")
plt.xlabel("x_test")
plt.ylabel("y_test")
plt.scatter(x_test,y_test)
plt.plot(x_test,model.predict(x_test))
717821i139-Nandhitha
B.Tech Artificial intelligence and data science
#plotting the trained model
plt.title("train data")
plt.xlabel("x_train")
plt.ylabel("y_train")
plt.scatter(x_train,y_train)
plt.plot(x_train,model.predict(x_train))
RESULT:
Thus, the implementation of regression models using keras in python has been executed
successfully.
717821i139-Nandhitha