first commit
Browse files- app.py +87 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from sklearn import datasets
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
import numpy as np
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from tensorflow.keras.layers import Dense
|
8 |
+
from tensorflow.keras.models import Sequential
|
9 |
+
from sklearn.preprocessing import OneHotEncoder
|
10 |
+
from sklearn.metrics import mean_squared_error
|
11 |
+
|
12 |
+
# Define the custom loss function
|
13 |
+
def soft_quantized_influence_measure(y_true, y_pred, threshold=0.1):
|
14 |
+
y_global_mean = tf.reduce_mean(y_true)
|
15 |
+
y_std = tf.math.reduce_std(y_true)
|
16 |
+
error = y_true - y_pred
|
17 |
+
abs_error = tf.abs(error)
|
18 |
+
is_small_error = abs_error <= threshold
|
19 |
+
n1 = tf.reduce_sum(tf.cast(is_small_error, tf.float32))
|
20 |
+
n2 = tf.reduce_sum(tf.cast(~is_small_error, tf.float32))
|
21 |
+
true_error_loss = tf.square(error) * n1 ** 2
|
22 |
+
false_error_loss = tf.square(error) * n2 ** 2
|
23 |
+
final = tf.where(is_small_error, true_error_loss, false_error_loss)
|
24 |
+
final = tf.reduce_mean(final) / (tf.square(y_std) ** 2)
|
25 |
+
return final
|
26 |
+
|
27 |
+
# Function to create datasets
|
28 |
+
def create_datasets(n_samples=1500):
|
29 |
+
noisy_circles = datasets.make_circles(n_samples=n_samples, factor=0.5, noise=0.05, random_state=170)
|
30 |
+
noisy_moons = datasets.make_moons(n_samples=n_samples, noise=0.05, random_state=170)
|
31 |
+
blobs = datasets.make_blobs(n_samples=n_samples, random_state=170)
|
32 |
+
rng = np.random.RandomState(170)
|
33 |
+
no_structure = rng.rand(n_samples, 2), None
|
34 |
+
X, y = datasets.make_blobs(n_samples=n_samples, random_state=170)
|
35 |
+
transformation = [[0.6, -0.6], [-0.4, 0.8]]
|
36 |
+
X_aniso = np.dot(X, transformation)
|
37 |
+
aniso = (X_aniso, y)
|
38 |
+
return [noisy_circles, noisy_moons, blobs, no_structure, aniso]
|
39 |
+
|
40 |
+
# Function to create a simple one-layer neural network
|
41 |
+
def create_model(input_shape):
|
42 |
+
model = Sequential([Dense(1, input_shape=input_shape, activation='sigmoid')])
|
43 |
+
return model
|
44 |
+
|
45 |
+
# Main app
|
46 |
+
def main():
|
47 |
+
st.title("Classification Data and Model Training Visualization")
|
48 |
+
|
49 |
+
# Data generation
|
50 |
+
datasets = create_datasets()
|
51 |
+
dataset_names = ["Noisy Circles", "Noisy Moons", "Blobs", "No Structure", "Anisotropic"]
|
52 |
+
|
53 |
+
# Streamlit selections for visualization
|
54 |
+
selected_dataset = st.selectbox("Select a Dataset", options=dataset_names)
|
55 |
+
|
56 |
+
# Placeholder for plots
|
57 |
+
fig, axs = plt.subplots(3, len(datasets), figsize=(15, 9))
|
58 |
+
|
59 |
+
# Encoder for labels
|
60 |
+
encoder = OneHotEncoder(sparse=False)
|
61 |
+
|
62 |
+
for i, dataset in enumerate(datasets):
|
63 |
+
X, y = dataset
|
64 |
+
if y is not None:
|
65 |
+
y = encoder.fit_transform(y.reshape(-1, 1))
|
66 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
67 |
+
|
68 |
+
# First row - Original datasets
|
69 |
+
if y is not None:
|
70 |
+
axs[0, i].scatter(X[:, 0], X[:, 1], c=np.argmax(y, axis=1))
|
71 |
+
else:
|
72 |
+
axs[0, i].scatter(X[:, 0], X[:, 1])
|
73 |
+
axs[0, i].set_title(dataset_names[i])
|
74 |
+
|
75 |
+
# Create model
|
76 |
+
model = create_model((2,))
|
77 |
+
|
78 |
+
# MSE Loss
|
79 |
+
model.compile(optimizer='sgd', loss='mean_squared_error')
|
80 |
+
history_mse = model.fit(X_train, y_train, validation_split=0.2, epochs=20, verbose=0)
|
81 |
+
|
82 |
+
# Soft Quantized Influence Measure Loss
|
83 |
+
model.compile(optimizer='sgd', loss=lambda y_true, y_pred: soft_quantized_influence_measure(y_true, y_pred, 0.1))
|
84 |
+
history_sqim = model.fit(X_train, y_train, validation_split=0.2, epochs=20, verbose=0)
|
85 |
+
|
86 |
+
# Second row - MSE Loss
|
87 |
+
axs[1, i].plot
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
tensorflow
|
3 |
+
scikit-learn
|
4 |
+
matplotlib
|
5 |
+
numpy
|