Artificial-superintelligence commited on
Commit
63df486
·
verified ·
1 Parent(s): 7a89958

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -134
app.py CHANGED
@@ -2,16 +2,14 @@ import numpy as np
2
  import pandas as pd
3
  from sklearn.preprocessing import LabelEncoder
4
  from sklearn.model_selection import train_test_split
5
- from tensorflow.keras.models import Sequential, Model
6
- from tensorflow.keras.layers import Dense, Dropout, Input, LayerNormalization, MultiHeadAttention, GlobalAveragePooling1D, Embedding, Layer, LSTM, Bidirectional, Conv1D
7
  from tensorflow.keras.optimizers import Adam
8
  from tensorflow.keras.utils import to_categorical
9
- from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau
10
- import tensorflow as tf
11
- import optuna
12
  import gradio as gr
13
 
14
- # Combined data set
15
  data = [
16
  "Double big 12", "Single big 11", "Single big 13", "Double big 12", "Double small 10",
17
  "Double big 12", "Double big 12", "Single small 7", "Single small 5", "Single small 9",
@@ -19,127 +17,40 @@ data = [
19
  "Double big 14", "Single big 17", "Triple 9", "Double small 6", "Single big 13",
20
  "Double big 14", "Double small 8", "Double small 8", "Single big 13", "Single small 9",
21
  "Double small 8", "Double small 8", "Single big 12", "Double small 8", "Double big 14",
22
- "Double small 10", "Single big 13", "Single big 11", "Double big 14", "Double big 14",
23
- "Double small", "Single big", "Double big", "Single small", "Single small",
24
- "Double small", "Single small", "Single small", "Double small", "Double small",
25
- "Double big", "Single big", "Triple", "Double big", "Single big", "Single big",
26
- "Double small", "Single small", "Double big", "Double small", "Double big",
27
- "Single small", "Single big", "Double small", "Double big", "Double big",
28
- "Double small", "Single big", "Double big", "Triple", "Single big", "Double small",
29
- "Single big", "Single small", "Double small", "Single big", "Single big",
30
- "Single big", "Double small", "Double small", "Single big", "Single small",
31
- "Single big", "Single small", "Single small", "Double small", "Single small",
32
- "Single big"
33
  ]
34
 
35
- # Counting the data points
36
- num_data_points = len(data)
37
- print(f'Total number of data points: {num_data_points}')
38
-
39
  # Encoding the labels
40
  encoder = LabelEncoder()
41
  encoded_data = encoder.fit_transform(data)
42
 
43
  # Create sequences
44
- sequence_length = 10
45
  X, y = [], []
46
  for i in range(len(encoded_data) - sequence_length):
47
  X.append(encoded_data[i:i + sequence_length])
48
  y.append(encoded_data[i + sequence_length])
49
 
50
  X = np.array(X)
51
- y = to_categorical(np.array(y), num_classes=len(encoder.classes_))
52
-
53
- print(f'Input shape: {X.shape}')
54
- print(f'Output shape: {y.shape}')
55
-
56
- class TransformerBlock(Layer):
57
- def __init__(self, embed_dim, num_heads, ff_dim, rate=0.1):
58
- super(TransformerBlock, self).__init__()
59
- self.att = MultiHeadAttention(num_heads=num_heads, key_dim=embed_dim)
60
- self.ffn = Sequential([
61
- Dense(ff_dim, activation="relu"),
62
- Dense(embed_dim),
63
- ])
64
- self.layernorm1 = LayerNormalization(epsilon=1e-6)
65
- self.layernorm2 = LayerNormalization(epsilon=1e-6)
66
- self.dropout1 = Dropout(rate)
67
- self.dropout2 = Dropout(rate)
68
-
69
- def call(self, inputs, training=False):
70
- attn_output = self.att(inputs, inputs)
71
- attn_output = self.dropout1(attn_output, training=training)
72
- out1 = self.layernorm1(inputs + attn_output)
73
- ffn_output = self.ffn(out1)
74
- ffn_output = self.dropout2(ffn_output, training=training)
75
- return self.layernorm2(out1 + ffn_output)
76
-
77
- def build_model(trial):
78
- embed_dim = trial.suggest_int('embed_dim', 64, 256, step=32)
79
- num_heads = trial.suggest_int('num_heads', 2, 8, step=2)
80
- ff_dim = trial.suggest_int('ff_dim', 128, 512, step=64)
81
- rate = trial.suggest_float('dropout', 0.1, 0.5, step=0.1)
82
- num_transformer_blocks = trial.suggest_int('num_transformer_blocks', 1, 3)
83
-
84
- inputs = Input(shape=(sequence_length,))
85
- embedding_layer = Embedding(input_dim=len(encoder.classes_), output_dim=embed_dim)
86
- x = embedding_layer(inputs)
87
-
88
- for _ in range(num_transformer_blocks):
89
- transformer_block = TransformerBlock(embed_dim, num_heads, ff_dim, rate)
90
- x = transformer_block(x)
91
-
92
- x = Conv1D(128, 3, activation='relu')(x)
93
- x = Bidirectional(LSTM(128, return_sequences=True))(x)
94
- x = GlobalAveragePooling1D()(x)
95
- x = Dropout(rate)(x)
96
- x = Dense(ff_dim, activation="relu")(x)
97
- x = Dropout(rate)(x)
98
- outputs = Dense(len(encoder.classes_), activation="softmax")(x)
99
-
100
- model = Model(inputs=inputs, outputs=outputs)
101
-
102
- optimizer = Adam(learning_rate=trial.suggest_float('lr', 1e-5, 1e-2, log=True))
103
- model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
104
-
105
  return model
106
 
107
- def objective(trial):
108
- model = build_model(trial)
109
-
110
- early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
111
- reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6)
112
-
113
- history = model.fit(
114
- X, y,
115
- epochs=100,
116
- batch_size=64,
117
- validation_split=0.2,
118
- callbacks=[early_stopping, reduce_lr],
119
- verbose=0
120
- )
121
-
122
- val_accuracy = max(history.history['val_accuracy'])
123
- return val_accuracy
124
-
125
  # Initialize the model
126
- study = optuna.create_study(direction='maximize')
127
- study.optimize(lambda trial: objective(trial), n_trials=10)
128
- best_trial = study.best_trial
129
- print(f'Best hyperparameters: {best_trial.params}')
130
-
131
- best_model = build_model(best_trial)
132
- early_stopping = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True)
133
- reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=10, min_lr=1e-6)
134
-
135
- history = best_model.fit(
136
- X, y,
137
- epochs=500,
138
- batch_size=64,
139
- validation_split=0.2,
140
- callbacks=[early_stopping, reduce_lr],
141
- verbose=2
142
- )
143
 
144
  def predict_next(model, data, sequence_length, encoder):
145
  last_sequence = data[-sequence_length:]
@@ -150,28 +61,15 @@ def predict_next(model, data, sequence_length, encoder):
150
 
151
  def update_data(data, new_outcome):
152
  data.append(new_outcome)
153
- if len(data) > sequence_length:
154
- data.pop(0)
155
  return data
156
 
157
  def retrain_model(model, X, y, epochs=10):
158
  early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
159
- reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, min_lr=1e-6)
160
-
161
- X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
162
-
163
- model.fit(
164
- X_train, y_train,
165
- epochs=epochs,
166
- batch_size=64,
167
- validation_data=(X_val, y_val),
168
- callbacks=[early_stopping, reduce_lr],
169
- verbose=0
170
- )
171
  return model
172
 
173
  def gradio_predict(outcome):
174
- global data, best_model
175
 
176
  if outcome not in encoder.classes_:
177
  return "Invalid outcome. Please try again."
@@ -181,11 +79,11 @@ def gradio_predict(outcome):
181
  if len(data) < sequence_length:
182
  return "Not enough data to make a prediction."
183
 
184
- predicted_next = predict_next(best_model, data, sequence_length, encoder)
185
  return f'Predicted next outcome: {predicted_next}'
186
 
187
  def gradio_update(actual_next):
188
- global data, X, y, best_model
189
 
190
  if actual_next not in encoder.classes_:
191
  return "Invalid outcome. Please try again."
@@ -196,20 +94,23 @@ def gradio_update(actual_next):
196
  return "Not enough data to update the model."
197
 
198
  # Update X and y
199
- new_X = encoder.transform(data[-sequence_length-1:-1]).reshape(1, -1)
200
- new_y = to_categorical(encoder.transform([data[-1]]), num_classes=len(encoder.classes_))
 
 
 
201
 
202
- X = np.vstack([X, new_X])
203
- y = np.vstack([y, new_y])
204
 
205
  # Retrain the model
206
- best_model = retrain_model(best_model, X, y, epochs=10)
207
 
208
  return "Model updated with new data."
209
 
210
  # Gradio interface
211
  with gr.Blocks() as demo:
212
- gr.Markdown("## Outcome Prediction with Enhanced Transformer")
213
  with gr.Row():
214
  outcome_input = gr.Textbox(label="Current Outcome")
215
  predict_button = gr.Button("Predict Next")
 
2
  import pandas as pd
3
  from sklearn.preprocessing import LabelEncoder
4
  from sklearn.model_selection import train_test_split
5
+ from tensorflow.keras.models import Sequential
6
+ from tensorflow.keras.layers import Dense, LSTM, Embedding
7
  from tensorflow.keras.optimizers import Adam
8
  from tensorflow.keras.utils import to_categorical
9
+ from tensorflow.keras.callbacks import EarlyStopping
 
 
10
  import gradio as gr
11
 
12
+ # Initial data set
13
  data = [
14
  "Double big 12", "Single big 11", "Single big 13", "Double big 12", "Double small 10",
15
  "Double big 12", "Double big 12", "Single small 7", "Single small 5", "Single small 9",
 
17
  "Double big 14", "Single big 17", "Triple 9", "Double small 6", "Single big 13",
18
  "Double big 14", "Double small 8", "Double small 8", "Single big 13", "Single small 9",
19
  "Double small 8", "Double small 8", "Single big 12", "Double small 8", "Double big 14",
20
+ "Double small 10", "Single big 13", "Single big 11", "Double big 14", "Double big 14"
 
 
 
 
 
 
 
 
 
 
21
  ]
22
 
 
 
 
 
23
  # Encoding the labels
24
  encoder = LabelEncoder()
25
  encoded_data = encoder.fit_transform(data)
26
 
27
  # Create sequences
28
+ sequence_length = 5
29
  X, y = [], []
30
  for i in range(len(encoded_data) - sequence_length):
31
  X.append(encoded_data[i:i + sequence_length])
32
  y.append(encoded_data[i + sequence_length])
33
 
34
  X = np.array(X)
35
+ y = to_categorical(y, num_classes=len(encoder.classes_))
36
+
37
+ # Build the model
38
+ def build_model(vocab_size, sequence_length):
39
+ model = Sequential([
40
+ Embedding(vocab_size, 50, input_length=sequence_length),
41
+ LSTM(100),
42
+ Dense(vocab_size, activation='softmax')
43
+ ])
44
+ model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.001), metrics=['accuracy'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  return model
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # Initialize the model
48
+ vocab_size = len(encoder.classes_)
49
+ model = build_model(vocab_size, sequence_length)
50
+
51
+ # Train the model
52
+ early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
53
+ history = model.fit(X, y, epochs=100, validation_split=0.2, callbacks=[early_stopping], verbose=0)
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  def predict_next(model, data, sequence_length, encoder):
56
  last_sequence = data[-sequence_length:]
 
61
 
62
  def update_data(data, new_outcome):
63
  data.append(new_outcome)
 
 
64
  return data
65
 
66
  def retrain_model(model, X, y, epochs=10):
67
  early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
68
+ model.fit(X, y, epochs=epochs, validation_split=0.2, callbacks=[early_stopping], verbose=0)
 
 
 
 
 
 
 
 
 
 
 
69
  return model
70
 
71
  def gradio_predict(outcome):
72
+ global data, model
73
 
74
  if outcome not in encoder.classes_:
75
  return "Invalid outcome. Please try again."
 
79
  if len(data) < sequence_length:
80
  return "Not enough data to make a prediction."
81
 
82
+ predicted_next = predict_next(model, data, sequence_length, encoder)
83
  return f'Predicted next outcome: {predicted_next}'
84
 
85
  def gradio_update(actual_next):
86
+ global data, X, y, model
87
 
88
  if actual_next not in encoder.classes_:
89
  return "Invalid outcome. Please try again."
 
94
  return "Not enough data to update the model."
95
 
96
  # Update X and y
97
+ new_X = []
98
+ new_y = []
99
+ for i in range(len(data) - sequence_length):
100
+ new_X.append(encoder.transform(data[i:i + sequence_length]))
101
+ new_y.append(encoder.transform([data[i + sequence_length]])[0])
102
 
103
+ X = np.array(new_X)
104
+ y = to_categorical(new_y, num_classes=len(encoder.classes_))
105
 
106
  # Retrain the model
107
+ model = retrain_model(model, X, y, epochs=10)
108
 
109
  return "Model updated with new data."
110
 
111
  # Gradio interface
112
  with gr.Blocks() as demo:
113
+ gr.Markdown("## Outcome Prediction Model")
114
  with gr.Row():
115
  outcome_input = gr.Textbox(label="Current Outcome")
116
  predict_button = gr.Button("Predict Next")