Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Commit
•
9f3e74e
1
Parent(s):
bc94e08
Update pages/42_regression.py
Browse files- pages/42_regression.py +67 -77
pages/42_regression.py
CHANGED
@@ -1,79 +1,69 @@
|
|
|
|
1 |
import numpy as np
|
2 |
import matplotlib.pyplot as plt
|
3 |
-
import
|
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 |
-
for
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
plt.legend()
|
70 |
-
|
71 |
-
# Plot training loss
|
72 |
-
plt.subplot(1, 2, 2)
|
73 |
-
plt.plot(losses)
|
74 |
-
plt.title('Training Loss')
|
75 |
-
plt.xlabel('Epoch')
|
76 |
-
plt.ylabel('Loss')
|
77 |
-
|
78 |
-
plt.tight_layout()
|
79 |
-
plt.show()
|
|
|
1 |
+
import streamlit as st
|
2 |
import numpy as np
|
3 |
import matplotlib.pyplot as plt
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras.models import Sequential
|
6 |
+
from tensorflow.keras.layers import Dense
|
7 |
+
|
8 |
+
# Title
|
9 |
+
st.title("Neural Network Line Fitting")
|
10 |
+
|
11 |
+
# Sidebar sliders for generating synthetic data
|
12 |
+
st.sidebar.header("Synthetic Data Controls")
|
13 |
+
true_w = st.sidebar.slider('True W (slope)', min_value=-10.0, max_value=10.0, value=2.0, step=0.1)
|
14 |
+
true_b = st.sidebar.slider('True B (intercept)', min_value=-10.0, max_value=10.0, value=1.0, step=0.1)
|
15 |
+
num_points = st.sidebar.slider('Number of data points', min_value=10, max_value=1000, value=100, step=10)
|
16 |
+
|
17 |
+
# Generate synthetic data
|
18 |
+
np.random.seed(0)
|
19 |
+
x_data = np.random.uniform(-100, 100, num_points)
|
20 |
+
noise = np.random.normal(0, 10, num_points)
|
21 |
+
y_data = true_w * x_data + true_b + noise
|
22 |
+
|
23 |
+
# Neural network model
|
24 |
+
model = Sequential([
|
25 |
+
Dense(1, input_dim=1)
|
26 |
+
])
|
27 |
+
|
28 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
29 |
+
|
30 |
+
# Train the model
|
31 |
+
model.fit(x_data, y_data, epochs=100, verbose=0)
|
32 |
+
|
33 |
+
# Get the trained parameters
|
34 |
+
trained_w = model.layers[0].get_weights()[0][0][0]
|
35 |
+
trained_b = model.layers[0].get_weights()[1][0]
|
36 |
+
|
37 |
+
# Make predictions
|
38 |
+
x_pred = np.linspace(-100, 100, 200)
|
39 |
+
y_pred = model.predict(x_pred)
|
40 |
+
|
41 |
+
# Plot the results
|
42 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
43 |
+
|
44 |
+
# Plot for the x-axis (bottom line)
|
45 |
+
ax.hlines(-1, -100, 100, color='blue', linestyle='--') # X-axis
|
46 |
+
|
47 |
+
# Plot for the y-axis (top line)
|
48 |
+
ax.hlines(1, -100, 100, color='blue', linestyle='--') # Y-axis
|
49 |
+
|
50 |
+
# Plot the synthetic data points
|
51 |
+
ax.scatter(x_data, y_data, color='gray', alpha=0.5, label='Data points')
|
52 |
+
|
53 |
+
# Plot the prediction line
|
54 |
+
ax.plot(x_pred, y_pred, color='red', label=f'Fitted line: y = {trained_w:.2f}x + {trained_b:.2f}')
|
55 |
+
|
56 |
+
# Update the layout
|
57 |
+
ax.set_xlim(-100, 100)
|
58 |
+
ax.set_ylim(-2, 2)
|
59 |
+
ax.set_xlabel('X-axis and Y-axis')
|
60 |
+
ax.set_yticks([]) # Hide y-axis ticks
|
61 |
+
ax.set_title('Neural Network Line Fitting')
|
62 |
+
ax.legend()
|
63 |
+
ax.grid(True)
|
64 |
+
|
65 |
+
# Display the plot in Streamlit
|
66 |
+
st.pyplot(fig)
|
67 |
+
|
68 |
+
# Display the trained parameters
|
69 |
+
st.write(f'Trained parameters: w = {trained_w:.2f}, b = {trained_b:.2f}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|