|
|
|
|
|
import tensorflow as tf |
|
import numpy as np |
|
import streamlit as st |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
model = tf.keras.Sequential([ |
|
tf.keras.layers.Dense(units=1, input_shape=[1]) |
|
]) |
|
|
|
|
|
model.compile(optimizer='sgd', loss='mse') |
|
|
|
|
|
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=float) |
|
ys = np.array([1.5, 2.0, 2.5, 3.0, 3.5], dtype=float) |
|
|
|
|
|
st.title('Simple Linear Regression with TensorFlow') |
|
|
|
|
|
input_value = st.number_input('Enter your input value:', value=1.0, format="%.1f") |
|
|
|
|
|
epochs = st.sidebar.slider("Number of epochs", 10, 100, 10) |
|
|
|
|
|
if st.button('Train Model and Predict'): |
|
with st.spinner('Training...'): |
|
model.fit(xs, ys, epochs=epochs) |
|
st.success('Training completed!') |
|
|
|
|
|
prediction = model.predict([input_value]) |
|
st.write(f'For input {input_value}, the prediction is {prediction[0][0]}') |
|
|
|
|
|
predictions = model.predict(xs) |
|
|
|
|
|
plt.scatter(xs, ys, label='Actual') |
|
plt.plot(xs, predictions, color='red', label='Predicted') |
|
plt.xlabel('Input Feature') |
|
plt.ylabel('Output Value') |
|
plt.legend() |
|
st.pyplot(plt) |
|
|