Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# A simple Linear Regression example with TensorFlow
|
| 2 |
+
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Define the model
|
| 9 |
+
model = tf.keras.Sequential([
|
| 10 |
+
tf.keras.layers.Dense(units=1, input_shape=[1])
|
| 11 |
+
])
|
| 12 |
+
|
| 13 |
+
# Compile the model with an optimizer and loss function
|
| 14 |
+
model.compile(optimizer='sgd', loss='mse')
|
| 15 |
+
|
| 16 |
+
# Training data
|
| 17 |
+
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
|
| 18 |
+
ys = np.array([1.5, 2.0, 2.5, 3.0, 3.5], dtype=float)
|
| 19 |
+
|
| 20 |
+
# Streamlit UI
|
| 21 |
+
st.title('Simple Linear Regression with TensorFlow')
|
| 22 |
+
|
| 23 |
+
# User input for the new value to predict
|
| 24 |
+
input_value = st.number_input('Enter your input value:', value=1.0, format="%.1f")
|
| 25 |
+
|
| 26 |
+
# User input for epochs
|
| 27 |
+
epochs = st.sidebar.slider("Number of epochs", 10, 100, 10)
|
| 28 |
+
|
| 29 |
+
# Button to train the model and make prediction
|
| 30 |
+
if st.button('Train Model and Predict'):
|
| 31 |
+
with st.spinner('Training...'):
|
| 32 |
+
model.fit(xs, ys, epochs=epochs)
|
| 33 |
+
st.success('Training completed!')
|
| 34 |
+
|
| 35 |
+
# Make prediction
|
| 36 |
+
prediction = model.predict([input_value])
|
| 37 |
+
st.write(f'For input {input_value}, the prediction is {prediction[0][0]}')
|
| 38 |
+
|
| 39 |
+
# Predictions for visualization
|
| 40 |
+
predictions = model.predict(xs)
|
| 41 |
+
|
| 42 |
+
# Plotting
|
| 43 |
+
plt.scatter(xs, ys, label='Actual')
|
| 44 |
+
plt.plot(xs, predictions, color='red', label='Predicted')
|
| 45 |
+
plt.xlabel('Input Feature')
|
| 46 |
+
plt.ylabel('Output Value')
|
| 47 |
+
plt.legend()
|
| 48 |
+
st.pyplot(plt)
|