Spaces:
Sleeping
Sleeping
File size: 2,076 Bytes
d36cc65 5928544 e30d640 d36cc65 e30d640 92000d4 5928544 7e75af6 5928544 d69c975 eae814a 5928544 d36cc65 f0cb89a 5928544 d36cc65 5928544 d36cc65 5928544 d69c975 eae814a 5928544 7c589a0 f0cb89a 7c589a0 f0cb89a 5928544 d36cc65 5928544 92000d4 d36cc65 92000d4 5928544 c38a687 92000d4 c38a687 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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)
|