Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# A simple Linear Regression example with TensorFlow
|
| 2 |
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
|
@@ -6,7 +6,7 @@ import streamlit as st
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
# Streamlit UI
|
| 9 |
-
st.title('
|
| 10 |
|
| 11 |
# Define the model
|
| 12 |
model = tf.keras.Sequential([
|
|
@@ -16,37 +16,37 @@ model = tf.keras.Sequential([
|
|
| 16 |
# Compile the model with an optimizer and loss function
|
| 17 |
model.compile(optimizer='sgd', loss='mse')
|
| 18 |
|
| 19 |
-
# Training data
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
# Display example input and output
|
| 24 |
-
st.write("Example
|
| 25 |
-
st.write("
|
| 26 |
|
| 27 |
-
# User input for the
|
| 28 |
-
|
| 29 |
|
| 30 |
# User input for epochs
|
| 31 |
epochs = st.sidebar.slider("Number of epochs", 10, 500, 10)
|
| 32 |
|
| 33 |
# Button to train the model and make prediction
|
| 34 |
-
if st.button('Train Model and Predict'):
|
| 35 |
with st.spinner('Training...'):
|
| 36 |
-
model.fit(
|
| 37 |
st.success('Training completed!')
|
| 38 |
|
| 39 |
# Make prediction
|
| 40 |
-
|
| 41 |
-
st.write(f'For input {
|
| 42 |
|
| 43 |
# Predictions for visualization
|
| 44 |
-
predictions = model.predict(
|
| 45 |
|
| 46 |
# Plotting
|
| 47 |
-
plt.scatter(
|
| 48 |
-
plt.plot(
|
| 49 |
-
plt.xlabel('
|
| 50 |
-
plt.ylabel('
|
| 51 |
plt.legend()
|
| 52 |
st.pyplot(plt)
|
|
|
|
| 1 |
+
# A simple Linear Regression example for Celsius to Fahrenheit conversion with TensorFlow
|
| 2 |
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
# Streamlit UI
|
| 9 |
+
st.title('Celsius to Fahrenheit Conversion with TensorFlow')
|
| 10 |
|
| 11 |
# Define the model
|
| 12 |
model = tf.keras.Sequential([
|
|
|
|
| 16 |
# Compile the model with an optimizer and loss function
|
| 17 |
model.compile(optimizer='sgd', loss='mse')
|
| 18 |
|
| 19 |
+
# Training data (Celsius to Fahrenheit)
|
| 20 |
+
celsius = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float) # Celsius
|
| 21 |
+
fahrenheit = np.array([-40, 14, 32, 46.4, 59, 71.6, 100.4], dtype=float) # Corresponding Fahrenheit
|
| 22 |
|
| 23 |
# Display example input and output
|
| 24 |
+
st.write("Example Celsius values (input):", celsius)
|
| 25 |
+
st.write("Corresponding Fahrenheit values (output):", fahrenheit)
|
| 26 |
|
| 27 |
+
# User input for the Celsius value to predict Fahrenheit
|
| 28 |
+
input_celsius = st.number_input('Enter Celsius value:', value=0.0, format="%.1f")
|
| 29 |
|
| 30 |
# User input for epochs
|
| 31 |
epochs = st.sidebar.slider("Number of epochs", 10, 500, 10)
|
| 32 |
|
| 33 |
# Button to train the model and make prediction
|
| 34 |
+
if st.button('Train Model and Predict Fahrenheit'):
|
| 35 |
with st.spinner('Training...'):
|
| 36 |
+
model.fit(celsius, fahrenheit, epochs=epochs)
|
| 37 |
st.success('Training completed!')
|
| 38 |
|
| 39 |
# Make prediction
|
| 40 |
+
predicted_fahrenheit = model.predict([input_celsius])
|
| 41 |
+
st.write(f'For input of {input_celsius}°C, the predicted Fahrenheit value is {predicted_fahrenheit[0][0]:.1f}°F')
|
| 42 |
|
| 43 |
# Predictions for visualization
|
| 44 |
+
predictions = model.predict(celsius)
|
| 45 |
|
| 46 |
# Plotting
|
| 47 |
+
plt.scatter(celsius, fahrenheit, label='Actual Conversion')
|
| 48 |
+
plt.plot(celsius, predictions, color='red', label='Predicted Conversion')
|
| 49 |
+
plt.xlabel('Celsius')
|
| 50 |
+
plt.ylabel('Fahrenheit')
|
| 51 |
plt.legend()
|
| 52 |
st.pyplot(plt)
|