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

Q2.ipynb - Colaboratory

The document describes creating a simple linear regression model in TensorFlow to predict a set of y-values from a set of x-values. It defines a model with a single dense layer, compiles it using mean squared error loss, fits the model to training data over 500 epochs, and prints the prediction for an input of 7.

Uploaded by

ujjwal
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)
51 views3 pages

Q2.ipynb - Colaboratory

The document describes creating a simple linear regression model in TensorFlow to predict a set of y-values from a set of x-values. It defines a model with a single dense layer, compiles it using mean squared error loss, fits the model to training data over 500 epochs, and prints the prediction for an input of 7.

Uploaded by

ujjwal
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

2/3/23, 3:21 PM Q2.

ipynb - Colaboratory

import tensorflow as tf
import numpy as np
from tensorflow import keras

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

model.compile(optimizer='sgd', loss='mean_squared_error')

xs = np.arange(1, 11, dtype = float) # set up range from 1 to 10 with increments of 1 - the num
print(xs)

[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]

start=1
step=0.5
num=10

ys = np.arange(0,num)*step+start
print(ys)

[1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5]

model.fit(xs, ys, epochs=500) # Simple, taken from reading materials

https://colab.research.google.com/drive/1tWOj-jRhgP3_lQmPHne2i_WUlpI0fgca#scrollTo=J8kY1xMgrdYP 1/3
2/3/23, 3:21 PM Q2.ipynb - Colaboratory
Epoch 478/500
1/1 [==============================] - 0s 5ms/step - loss: 0.0012
Epoch 479/500
1/1 [==============================] - 0s 5ms/step - loss: 0.0011
Epoch 480/500
1/1 [==============================] - 0s 4ms/step - loss: 0.0011
Epoch 481/500

print(model.predict([7.0]))

1/1 [==============================] - 0s 84ms/step


[[4.0003657]]

https://colab.research.google.com/drive/1tWOj-jRhgP3_lQmPHne2i_WUlpI0fgca#scrollTo=J8kY1xMgrdYP 2/3
2/3/23, 3:21 PM Q2.ipynb - Colaboratory

check 0s completed at 3:21 PM

https://colab.research.google.com/drive/1tWOj-jRhgP3_lQmPHne2i_WUlpI0fgca#scrollTo=J8kY1xMgrdYP 3/3

You might also like