# A simple Linear Regression example for Celsius to Fahrenheit conversion with TensorFlow import tensorflow as tf import numpy as np import streamlit as st import matplotlib.pyplot as plt # Streamlit UI st.title('Celsius to Fahrenheit Conversion with TensorFlow') # Define the model model = tf.keras.Sequential([ tf.keras.layers.Dense(units=1, input_shape=[1]) ]) # Compile the model with the Adam optimizer and loss function model.compile(optimizer=tf.keras.optimizers.Adam(0.7), loss='mean_squared_error') # Training data (Celsius to Fahrenheit) celsius = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float) fahrenheit = np.array([-40, 14, 32, 46.4, 59, 71.6, 100.4], dtype=float) # User input for the Celsius value to predict Fahrenheit input_celsius = st.number_input('Enter Celsius value:', value=0.0, format="%.1f") # Button to train the model and make prediction if st.button('Train Model and Predict Fahrenheit'): with st.spinner('Training...'): # Fit the model history = model.fit(celsius, fahrenheit, epochs=500) st.success('Training completed!') # Make prediction predicted_fahrenheit = model.predict([input_celsius])[0][0] actual_fahrenheit = input_celsius * 9/5 + 32 st.write(f'For input of {input_celsius}°C, the predicted Fahrenheit value is {predicted_fahrenheit:.1f}°F') st.write(f'Actual Fahrenheit value (by formula) is {actual_fahrenheit:.1f}°F') # Predictions for visualization predictions = model.predict(celsius) # Plotting Conversion Graph plt.figure(figsize=(8, 4)) plt.scatter(celsius, fahrenheit, label='Actual Conversion') plt.plot(celsius, predictions, color='red', label='Predicted Conversion') plt.xlabel('Celsius') plt.ylabel('Fahrenheit') plt.title('Celsius to Fahrenheit Conversion') plt.legend() st.pyplot(plt) # Plotting Training Loss Graph plt.figure(figsize=(8, 4)) plt.plot(history.history['loss']) plt.title('Model Training Loss') plt.xlabel('Epoch Number') plt.ylabel("Loss Magnitude") st.pyplot(plt)