Spaces:
Sleeping
Sleeping
File size: 1,787 Bytes
d36cc65 5928544 e30d640 d36cc65 e30d640 d69c975 5928544 7e75af6 5928544 d69c975 7e75af6 7c589a0 5928544 d36cc65 f0cb89a 5928544 d36cc65 5928544 d36cc65 5928544 d69c975 7e75af6 5928544 7c589a0 f0cb89a 7c589a0 f0cb89a 5928544 d36cc65 5928544 d36cc65 5928544 |
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 |
# 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 with a different weight initializer
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1])
])
# Compile the model with the Adam optimizer and loss function
optimizer = tf.keras.optimizers.Adam(0.01)
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")
# Button to train the model and make prediction
if st.button('Train Model and Predict Fahrenheit'):
with st.spinner('Training...'):
# Fit the model
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
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)
|