Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save haseebpvt/f13cc57e608fda9d3c7056c1e2ce97c1 to your computer and use it in GitHub Desktop.

Select an option

Save haseebpvt/f13cc57e608fda9d3c7056c1e2ce97c1 to your computer and use it in GitHub Desktop.
Celsius to Fahrenheit conversion with Tesorflow
# F = C * 1.8 + 32
print('The program starts')
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
import numpy as np
import matplotlib.pyplot as plt
#Store farenhite and their curresponding celcius into an array
celcius_q = np.array([], dtype=float)
farenhite_a = np.array([], dtype=float)
for c in range(5,100,5):
f = c * 1.8 + 32
celcius_q = np.append(celcius_q, c)
farenhite_a = np.append(farenhite_a, f)
for i,e in enumerate(celcius_q):
print('Celcius {} is equal to {}'.format(celcius_q[i], farenhite_a[i]))
#Creating the model
l0 = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([l0])
#Compiling model
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))
#Training the model
history = model.fit(celcius_q, farenhite_a, epochs=500, verbose=False)
print('Finished training')
print('These are the layer weights {}'.format(l0.get_weights()))
print(model)
#Testing the model
while True:
x = input('Enter in celsius: ')
val = float(x)
print(model.predict([val]))
# plt.xlabel('Epoc number')
# plt.ylabel('Loss magnitude')
# plt.plot(history.history['loss'])
# plt.show(block=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment