Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,9 +15,22 @@ from keras.losses import MeanSquaredError, BinaryCrossentropy
|
|
| 15 |
from keras.regularizers import l2, l1
|
| 16 |
from keras.callbacks import Callback
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Set wide layout and TensorFlow Playground CSS
|
| 23 |
st.set_page_config(layout="wide")
|
|
@@ -111,7 +124,7 @@ with st.container():
|
|
| 111 |
with col4:
|
| 112 |
activation = st.selectbox("Activation", ["ReLU", "Sigmoid", "Tanh"], index=2)
|
| 113 |
with col5:
|
| 114 |
-
batch_size = st.slider("Batch Size", 1,
|
| 115 |
|
| 116 |
# Row 2
|
| 117 |
col6, col7, col8, col9, col10 = st.columns(5)
|
|
@@ -127,28 +140,28 @@ with st.container():
|
|
| 127 |
st.button("Reset", key="reset_global", on_click=reset_session)
|
| 128 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 129 |
|
| 130 |
-
# Dataset generation
|
| 131 |
-
def generate_xor(n_samples):
|
| 132 |
X = np.random.rand(n_samples, 2) * 2 - 1
|
| 133 |
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0).astype(int)
|
| 134 |
return X, y
|
| 135 |
|
| 136 |
-
def generate_sine_wave(n_samples, noise):
|
| 137 |
X = np.linspace(-3, 3, n_samples).reshape(-1, 1)
|
| 138 |
y = np.sin(X) + np.random.normal(0, noise / 100, X.shape)
|
| 139 |
return np.hstack([X, X**2]), y.ravel()
|
| 140 |
|
| 141 |
if problem_type == "Classification":
|
| 142 |
if dataset_type == "Blobs":
|
| 143 |
-
fv, cv = make_blobs(n_samples=
|
| 144 |
elif dataset_type == "Circles":
|
| 145 |
-
fv, cv = make_circles(n_samples=
|
| 146 |
elif dataset_type == "Spirals":
|
| 147 |
-
fv, cv = make_moons(n_samples=
|
| 148 |
elif dataset_type == "XOR":
|
| 149 |
-
fv, cv = generate_xor(
|
| 150 |
else:
|
| 151 |
-
fv, cv = generate_sine_wave(
|
| 152 |
|
| 153 |
# Feature preprocessing
|
| 154 |
std = StandardScaler()
|
|
@@ -362,7 +375,7 @@ with col_right:
|
|
| 362 |
model = create_model(len(selected_features), st.session_state.hidden_layer_neurons)
|
| 363 |
callback = OutputCallback(selected_data, cv)
|
| 364 |
callback.model = model # Explicitly set the model for the callback
|
| 365 |
-
model.fit(selected_data, cv, epochs=
|
| 366 |
batch_size=batch_size, validation_split=1-train_ratio,
|
| 367 |
callbacks=[callback], verbose=0)
|
| 368 |
except Exception as e:
|
|
|
|
| 15 |
from keras.regularizers import l2, l1
|
| 16 |
from keras.callbacks import Callback
|
| 17 |
|
| 18 |
+
# Check TensorFlow and Keras versions with fallback
|
| 19 |
+
try:
|
| 20 |
+
tf_version = tf.__version__
|
| 21 |
+
# Try multiple ways to get Keras version, accounting for TensorFlow integration
|
| 22 |
+
keras_version = None
|
| 23 |
+
if hasattr(tf.keras, '__version__'):
|
| 24 |
+
keras_version = tf.keras.__version__
|
| 25 |
+
elif hasattr(tf, 'keras') and hasattr(tf.keras, 'version'):
|
| 26 |
+
keras_version = tf.keras.version.__version__
|
| 27 |
+
else:
|
| 28 |
+
keras_version = "Keras version not available (bundled with TensorFlow)"
|
| 29 |
+
st.write(f"TensorFlow version: {tf_version}")
|
| 30 |
+
st.write(f"Keras version: {keras_version}")
|
| 31 |
+
except AttributeError as e:
|
| 32 |
+
st.error(f"Error checking versions: {e}")
|
| 33 |
+
st.write("Falling back to default versions: TensorFlow ~2.15, Keras ~2.15")
|
| 34 |
|
| 35 |
# Set wide layout and TensorFlow Playground CSS
|
| 36 |
st.set_page_config(layout="wide")
|
|
|
|
| 124 |
with col4:
|
| 125 |
activation = st.selectbox("Activation", ["ReLU", "Sigmoid", "Tanh"], index=2)
|
| 126 |
with col5:
|
| 127 |
+
batch_size = st.slider("Batch Size", 1, 10, 5) # Reduced max batch size for Spaces
|
| 128 |
|
| 129 |
# Row 2
|
| 130 |
col6, col7, col8, col9, col10 = st.columns(5)
|
|
|
|
| 140 |
st.button("Reset", key="reset_global", on_click=reset_session)
|
| 141 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 142 |
|
| 143 |
+
# Dataset generation (reduced sample size for performance)
|
| 144 |
+
def generate_xor(n_samples=400): # Reduced from 800 for performance
|
| 145 |
X = np.random.rand(n_samples, 2) * 2 - 1
|
| 146 |
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0).astype(int)
|
| 147 |
return X, y
|
| 148 |
|
| 149 |
+
def generate_sine_wave(n_samples=400, noise): # Reduced from 800
|
| 150 |
X = np.linspace(-3, 3, n_samples).reshape(-1, 1)
|
| 151 |
y = np.sin(X) + np.random.normal(0, noise / 100, X.shape)
|
| 152 |
return np.hstack([X, X**2]), y.ravel()
|
| 153 |
|
| 154 |
if problem_type == "Classification":
|
| 155 |
if dataset_type == "Blobs":
|
| 156 |
+
fv, cv = make_blobs(n_samples=400, centers=2, n_features=2, cluster_std=1.5 + noise_level / 50, random_state=42)
|
| 157 |
elif dataset_type == "Circles":
|
| 158 |
+
fv, cv = make_circles(n_samples=400, noise=noise_level / 250, factor=0.2)
|
| 159 |
elif dataset_type == "Spirals":
|
| 160 |
+
fv, cv = make_moons(n_samples=400, noise=noise_level / 250)
|
| 161 |
elif dataset_type == "XOR":
|
| 162 |
+
fv, cv = generate_xor(400)
|
| 163 |
else:
|
| 164 |
+
fv, cv = generate_sine_wave(400, noise_level)
|
| 165 |
|
| 166 |
# Feature preprocessing
|
| 167 |
std = StandardScaler()
|
|
|
|
| 375 |
model = create_model(len(selected_features), st.session_state.hidden_layer_neurons)
|
| 376 |
callback = OutputCallback(selected_data, cv)
|
| 377 |
callback.model = model # Explicitly set the model for the callback
|
| 378 |
+
model.fit(selected_data, cv, epochs=50, # Further reduced for Spaces
|
| 379 |
batch_size=batch_size, validation_split=1-train_ratio,
|
| 380 |
callbacks=[callback], verbose=0)
|
| 381 |
except Exception as e:
|