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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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",
|
| 18 |
+
"Single big 13", "Double small 8", "Single small 5", "Double big 14", "Single big 11",
|
| 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:]
|
| 146 |
+
last_sequence = np.array(encoder.transform(last_sequence)).reshape((1, sequence_length))
|
| 147 |
+
prediction = model.predict(last_sequence)
|
| 148 |
+
predicted_label = encoder.inverse_transform([np.argmax(prediction)])
|
| 149 |
+
return predicted_label[0]
|
| 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."
|
| 178 |
+
|
| 179 |
+
data = update_data(data, outcome)
|
| 180 |
+
|
| 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."
|
| 192 |
+
|
| 193 |
+
data = update_data(data, actual_next)
|
| 194 |
+
|
| 195 |
+
if len(data) < sequence_length + 1:
|
| 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")
|
| 216 |
+
predicted_output = gr.Textbox(label="Predicted Next Outcome")
|
| 217 |
+
with gr.Row():
|
| 218 |
+
actual_input = gr.Textbox(label="Actual Next Outcome")
|
| 219 |
+
update_button = gr.Button("Update Model")
|
| 220 |
+
update_output = gr.Textbox(label="Update Status")
|
| 221 |
+
|
| 222 |
+
predict_button.click(gradio_predict, inputs=outcome_input, outputs=predicted_output)
|
| 223 |
+
update_button.click(gradio_update, inputs=actual_input, outputs=update_output)
|
| 224 |
+
|
| 225 |
+
demo.launch()
|