# 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]) ]) # Optimizer selection optimizer = st.sidebar.selectbox( "Select optimizer", ('sgd', 'adam', 'rmsprop') ) # Compile the model with an optimizer and loss function model.compile(optimizer=optimizer, loss='mse') # 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") # User input for epochs epochs = st.sidebar.slider("Number of epochs", 10, 500, 10) # Button to train the model and make prediction if st.button('Train Model and Predict Fahrenheit'): with st.spinner('Training...'): model.fit(celsius, fahrenheit, epochs=epochs) 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 plt.scatter(celsius, fahrenheit, label='Actual Conversion') plt.plot(celsius, predictions, color='red', label='Predicted Conversion') plt.xlabel('Celsius') plt.ylabel('Fahrenheit') plt.legend() st.pyplot(plt)