Post

Hello World in TensorFLow

Hello World in TensorFLow

Hello World in Tensorflow

Medium article image

Writing a “ Hello World “ in Tensorflow

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

Explanation : This is written usin python and tensorflow and an API in tensorflow called keras , in Keras Dense is used to define a layer of connected neurons, Unit =1 means it has 1 neuron , sequential as the layers are successive , shape of input =1

1
model.commpile(optimizer='sgd',loss='mean_squared_error')

the loss functions is inbuilt , ‘sgd’=stochastic gradient descent

Input = [-1.0,0.0,1.0,2.0,3.0,4.0]

and this is the desired output figure out the logic

Output=[-3.0,-1.0,1.0,3.0,5.0,7.0]

1
xs=np.array([-1.0,0.0,1.0,2.0,3.0,4.0], dtype=float)ys=np.array([-3.0,-1.0,1.0,3.0,5.0,7.0], dtype=float)
1
model.fit(xs,ys,epochs=500)#fit x->y iteratrions=500

Now using the gained intelligence to predict

1
print(model.predict([10.0]))

Complete code to Implement in colab would be like :

1
import tensorflow as tfimport numpy as npfrom tensorflow import keras
1
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
1
model.compile(optimizer='sgd', loss='mean_squared_error')
1
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
1
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
1
model.fit(xs, ys, epochs=500)
1
print(model.predict([10.0]))

Reference

This post is licensed under CC BY 4.0 by the author.