Spaces:
Configuration error
Configuration error
Update README.md
Browse files
README.md
CHANGED
|
@@ -8,6 +8,319 @@ sdk_version: 1.32.0
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: creativeml-openrail-m
|
| 11 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: creativeml-openrail-m
|
| 11 |
+
---from keras.models import Sequential
|
| 12 |
+
from keras.layers import Dense
|
| 13 |
+
|
| 14 |
+
# Create a sequential model - this is a linear stack of layers
|
| 15 |
+
model = Sequential()
|
| 16 |
+
|
| 17 |
+
# Add an input layer to our model. In this case we will use a dense layer, which is a fully-connected neural layer. We have 10 inputs (features) and we want to use the ReLU activation function
|
| 18 |
+
model.add(Dense(10, activation='relu', input_shape=(10,)))
|
| 19 |
+
|
| 20 |
+
# Add a hidden layer. This will also be a dense layer with 5 neurons and a ReLU activation
|
| 21 |
+
|
| 22 |
+
python dataset.py
|
| 23 |
+
# Load the inputs and targets from separate files
|
| 24 |
+
X = np.load('inputs.npy')
|
| 25 |
+
y = np.load('targets.npy')
|
| 26 |
+
|
| 27 |
+
# Convert categorical labels to one-hot encoding
|
| 28 |
+
enc = OneHotEncoder()
|
| 29 |
+
y = enc.fit_transform(y[:,np.newaxis]).toarray()
|
| 30 |
+
np.save('inputs.npy', X)
|
| 31 |
+
np.save('targets.npy', y)
|
| 32 |
+
python neural_network.py
|
| 33 |
+
|
| 34 |
+
import numpy as np
|
| 35 |
+
from keras.models import Sequential
|
| 36 |
+
from keras.layers import Dense
|
| 37 |
+
from sklearn.datasets import load_iris
|
| 38 |
+
from sklearn.model_selection import train_test_split
|
| 39 |
+
from sklearn.metrics import classification_report
|
| 40 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 41 |
+
|
| 42 |
+
# Load the iris dataset
|
| 43 |
+
iris = load_iris()
|
| 44 |
+
X = iris['data']
|
| 45 |
+
y = iris['target']
|
| 46 |
+
|
| 47 |
+
# Convert categorical labels to one-hot encoding
|
| 48 |
+
enc = OneHotEncoder()
|
| 49 |
+
y = enc.fit_transform(y[:,np.newaxis]).toarray()
|
| 50 |
+
|
| 51 |
+
# Split the data into training and test sets
|
| 52 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 53 |
+
|
| 54 |
+
# Define the model architecture
|
| 55 |
+
model = Sequential()
|
| 56 |
+
model.add(Dense(64, activation='relu', input_dim=X_train.shape[1]))
|
| 57 |
+
model.add(Dense(64, activation='relu'))
|
| 58 |
+
model.add(Dense(y_train.shape[1], activation='softmax'))
|
| 59 |
+
|
| 60 |
+
# Compile the model with the adam optimizer and categorical cross entropy loss
|
| 61 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 62 |
+
|
| 63 |
+
# Perform grid search to find the optimal hyperparameters
|
| 64 |
+
from sklearn.model_selection import GridSearchCV
|
| 65 |
+
param_grid = {'batch_size': [10, 20, 30], 'epochs': [10, 20]}
|
| 66 |
+
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
|
| 67 |
+
grid_result = grid.fit(X_train, y_train)
|
| 68 |
+
|
| 69 |
+
# Extract the best hyperparameters
|
| 70 |
+
best_batch_size = grid_result.best_params_['batch_size']
|
| 71 |
+
best_epochs = grid_result.best_params_['epochs']
|
| 72 |
+
|
| 73 |
+
# Retrain the model using the best hyperparameters found
|
| 74 |
+
model.fit(X_train, y_train, batch_size=best_batch_size, epochs=best_epochs, verbose=1, validation_data=(X_test, y_test))
|
| 75 |
+
|
| 76 |
+
# Evaluate the model on the test set
|
| 77 |
+
scores = model.evaluate(X_test, y_test, verbose=1)
|
| 78 |
+
print('Test loss:', scores[0])
|
| 79 |
+
print('Test accuracy:', scores[1])
|
| 80 |
+
|
| 81 |
+
# Generate a classification report
|
| 82 |
+
y_pred = model.predict_classes(X_test)
|
| 83 |
+
print(classification_report(np.argmax(y_test, axis=1), y_pred))
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
np.save('inputs.npy', X)
|
| 87 |
+
np.save('targets.npy', y)
|
| 88 |
+
|
| 89 |
+
# Load the inputs and targets from separate files
|
| 90 |
+
X = np.load('inputs.npy')
|
| 91 |
+
y = np.load('targets.npy')
|
| 92 |
+
|
| 93 |
+
# Convert categorical labels to one-hot encoding
|
| 94 |
+
enc = OneHotEncoder()
|
| 95 |
+
y = enc.fit_transform(y[:,np.newaxis]).toarray(
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
python simulation.py
|
| 99 |
+
Train on 120 samples, validate on 30 samples
|
| 100 |
+
Epoch 1/10
|
| 101 |
+
120/120 [==============================] - 0s 798us/step - loss: 1.1511 - accuracy: 0.3667 - val_loss: 1.1095 - val_accuracy: 0.3667
|
| 102 |
+
Epoch 2/10
|
| 103 |
+
120/120 [==============================] - 0s 53us/step - loss: 1.0961 - accuracy: 0.4833 - val_loss: 1.0573 - val_accuracy: 0.5333
|
| 104 |
+
...
|
| 105 |
+
Epoch 9/10
|
| 106 |
+
120/120 [==============================] - 0s 53us/step - loss: 0.7153 - accuracy: 0.8500 - val_loss: 0.7523 - val_accuracy: 0.8000
|
| 107 |
+
Epoch 10/10
|
| 108 |
+
120/120 [==============================] - 0s 53us/step - loss: 0.6701 - accuracy: 0.8833 - val_loss: 0.7155 - val_accuracy: 0.8333
|
| 109 |
+
Test loss: 0.715541832447052
|
| 110 |
+
Test accuracy: 0.8333333333333334
|
| 111 |
+
precision recall f1-score support
|
| 112 |
+
0 0.83 1.00 0.91 9
|
| 113 |
+
1 0.80 0.75 0.78 11
|
| 114 |
+
2 0.86 0.75 0.80 9
|
| 115 |
+
micro avg 0.83 0.83 0.83 29
|
| 116 |
+
macro avg 0.83 0.83 0.83 29
|
| 117 |
+
weighted avg 0.83 0.83 0.83 29
|
| 118 |
+
|
| 119 |
+
<keras.callbacks.History at 0x1a2b3c4d5e>
|
| 120 |
+
|
| 121 |
+
import numpy as np
|
| 122 |
+
from keras.models import Sequential
|
| 123 |
+
from keras.layers import Dense
|
| 124 |
+
from sklearn.datasets import load_iris
|
| 125 |
+
from sklearn.model_selection import train_test_split
|
| 126 |
+
from sklearn.metrics import classification_report
|
| 127 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 128 |
+
|
| 129 |
+
# Load the iris dataset
|
| 130 |
+
iris = load_iris()
|
| 131 |
+
X = iris['data']
|
| 132 |
+
y = iris['target']
|
| 133 |
+
|
| 134 |
+
# Convert categorical labels to one-hot encoding
|
| 135 |
+
enc = OneHotEncoder()
|
| 136 |
+
y = enc.fit_transform(y[:,np.newaxis]).toarray()
|
| 137 |
+
|
| 138 |
+
# Split the data into training and test sets
|
| 139 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 140 |
+
|
| 141 |
+
# Define the model architecture
|
| 142 |
+
model = Sequential()
|
| 143 |
+
model.add(Dense(64, activation='relu', input_dim=X_train.shape[1]))
|
| 144 |
+
model.add(Dense(64, activation='relu'))
|
| 145 |
+
model.add(Dense(y_train.shape[1], activation='softmax'))
|
| 146 |
+
|
| 147 |
+
# Compile the model with the adam optimizer and categorical cross entropy loss
|
| 148 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 149 |
+
|
| 150 |
+
# Perform grid search to find the optimal hyperparameters
|
| 151 |
+
from sklearn.model_selection import GridSearchCV
|
| 152 |
+
param_grid = {'batch_size': [10, 20, 30], 'epochs': [10, 20]}
|
| 153 |
+
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
|
| 154 |
+
grid_result = grid.fit(X_train, y_train)
|
| 155 |
+
|
| 156 |
+
# Extract the best hyperparameters
|
| 157 |
+
best_batch_size = grid_result.best_params_['batch_size']
|
| 158 |
+
best_epochs = grid_result.best_params_['epochs']
|
| 159 |
+
|
| 160 |
+
# Retrain the model using the best hyperparameters found
|
| 161 |
+
model.fit(X_train, y_train, batch_size=best_batch_size, epochs=best_epochs, verbose=1, validation_data=(X_test, y_test))
|
| 162 |
+
|
| 163 |
+
# Evaluate the model on the test set
|
| 164 |
+
scores = model.evaluate(X_test, y_test, verbose=1)
|
| 165 |
+
print('Test loss:', scores[0])
|
| 166 |
+
print('Test accuracy:', scores[1])
|
| 167 |
+
|
| 168 |
+
# Generate a classification report
|
| 169 |
+
y_pred = model.predict_classes(X_test)
|
| 170 |
+
print(classification_report(np.argmax(y_test, axis=1), y_pred))
|
| 171 |
+
from keras.models import Sequential
|
| 172 |
+
from keras.layers import Dense
|
| 173 |
+
|
| 174 |
+
# Create a sequential model - this is a linear stack of layers
|
| 175 |
+
model = Sequential()
|
| 176 |
+
|
| 177 |
+
# Add an input layer to our model. In this case we will use a dense layer, which is a fully-connected neural layer. We have 10 inputs (features) and we want to use the ReLU activation function
|
| 178 |
+
model.add(Dense(10, activation='relu', input_shape=(10,)))
|
| 179 |
+
import numpy as np
|
| 180 |
+
from keras.models import Sequential
|
| 181 |
+
from keras.layers import Dense
|
| 182 |
+
from sklearn.datasets import load_iris
|
| 183 |
+
from sklearn.model_selection import train_test_split
|
| 184 |
+
from sklearn.metrics import classification_report
|
| 185 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 186 |
+
|
| 187 |
+
# Load the iris dataset
|
| 188 |
+
iris = load_iris()
|
| 189 |
+
X = iris['data']
|
| 190 |
+
y = iris['target']
|
| 191 |
+
|
| 192 |
+
# Convert categorical labels to one-hot encoding
|
| 193 |
+
enc = OneHotEncoder()
|
| 194 |
+
y = enc.fit_transform(y[:,np.newaxis]).toarray()
|
| 195 |
+
|
| 196 |
+
# Split the data into training and test sets
|
| 197 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 198 |
+
|
| 199 |
+
# Define the model architecture
|
| 200 |
+
model = Sequential()
|
| 201 |
+
model.add(Dense(64, activation='relu', input_dim=X_train.shape[1]))
|
| 202 |
+
model.add(Dense(64, activation='relu'))
|
| 203 |
+
model.add(Dense(y_train.shape[1], activation='softmax'))
|
| 204 |
+
|
| 205 |
+
# Compile the model with the adam optimizer and categorical cross entropy loss
|
| 206 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 207 |
+
|
| 208 |
+
# Perform grid search to find the optimal hyperparameters
|
| 209 |
+
from sklearn.model_selection import GridSearchCV
|
| 210 |
+
param_grid = {'batch_size': [10, 20, 30], 'epochs': [10, 20]}
|
| 211 |
+
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
|
| 212 |
+
grid_result = grid.fit(X_train, y_train)
|
| 213 |
+
|
| 214 |
+
# Extract the best hyperparameters
|
| 215 |
+
best_batch_size = grid_result.best_params_['batch_size']
|
| 216 |
+
best_epochs = grid_result.best_params_['epochs']
|
| 217 |
+
|
| 218 |
+
# Retrain the model using the best hyperparameters found
|
| 219 |
+
model.fit(X_train, y_train, batch_size=best_batch_size, epochs=best_epochs, verbose=1, validation_data=(X_test, y_test))
|
| 220 |
+
|
| 221 |
+
# Evaluate the model on the test set
|
| 222 |
+
scores = model.evaluate(X_test, y_test, verbose=1)
|
| 223 |
+
print('Test loss:', scores[0])
|
| 224 |
+
print('Test accuracy:', scores[1])
|
| 225 |
+
|
| 226 |
+
# Generate a classification report
|
| 227 |
+
y_pred = model.predict_classes(X_test)
|
| 228 |
+
print(classification_report(np.argmax(y_test, axis=1), y_pred))
|
| 229 |
+
pi3
|
| 230 |
+
|
| 231 |
+
import numpy as np
|
| 232 |
+
from keras.models import Sequential
|
| 233 |
+
from keras.layers import Dense
|
| 234 |
+
from sklearn.datasets import load_iris
|
| 235 |
+
from sklearn.model_selection import train_test_split
|
| 236 |
+
from sklearn.metrics import classification_report
|
| 237 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 238 |
+
|
| 239 |
+
# Load the iris dataset
|
| 240 |
+
iris = load_iris()
|
| 241 |
+
X = iris['data']
|
| 242 |
+
y = iris['target']
|
| 243 |
+
|
| 244 |
+
# Convert categorical labels to one-hot encoding
|
| 245 |
+
enc = OneHotEncoder()
|
| 246 |
+
y = enc.fit_transform(y[:,np.newaxis]).toarray()
|
| 247 |
+
|
| 248 |
+
# Split the data into training and test sets
|
| 249 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 250 |
+
|
| 251 |
+
# Define the model architecture
|
| 252 |
+
model = Sequential()
|
| 253 |
+
model.add(Dense(64, activation='relu', input_dim=X_train.shape[1]))
|
| 254 |
+
model.add(Dense(64, activation='relu'))
|
| 255 |
+
model.add(Dense(y_train.shape[1], activation='softmax'))
|
| 256 |
+
|
| 257 |
+
# Compile the model with the adam optimizer and categorical cross entropy loss
|
| 258 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 259 |
+
|
| 260 |
+
# Perform grid search to find the optimal hyperparameters
|
| 261 |
+
from sklearn.model_selection import GridSearchCV
|
| 262 |
+
param_grid = {'batch_size': [10, 20, 30], 'epochs': [10, 20]}
|
| 263 |
+
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
|
| 264 |
+
grid_result = grid.fit(X_train, y_train)
|
| 265 |
+
|
| 266 |
+
# Extract the best hyperparameters
|
| 267 |
+
best_batch_size = grid_result.best_params_['batch_size']
|
| 268 |
+
best_epochs = grid_result.best_params_['epochs']
|
| 269 |
+
|
| 270 |
+
# Retrain the model using the best hyperparameters found
|
| 271 |
+
model.fit(X_train, y_train, batch_size=best_batch_size, epochs=best_epochs, verbose=1, validation_data=(X_test, y_test))
|
| 272 |
+
|
| 273 |
+
# Evaluate the model on the test set
|
| 274 |
+
scores = model.evaluate(X_test, y_test, verbose=1)
|
| 275 |
+
print('Test loss:', scores[0])
|
| 276 |
+
print('Test accuracy:', scores[1])
|
| 277 |
+
|
| 278 |
+
# Generate a classification report
|
| 279 |
+
y_pred = model.predict_classes(X_test)
|
| 280 |
+
print(classification_report(np.argmax(y_test, axis=1), y_pred))
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
# Define the model architecture
|
| 284 |
+
model = Sequential()
|
| 285 |
+
model.add(Dense(64, activation='relu', input_dim=input_dim))
|
| 286 |
+
model.add(Dense(64, activation='relu'))
|
| 287 |
+
model.add(Dense(output_dim, activation='softmax'))
|
| 288 |
+
|
| 289 |
+
# Specify the ranges for the hyperparameters to explore
|
| 290 |
+
batch_sizes = [10, 20, 30, 40, 50]
|
| 291 |
+
learning_rates = [0.001, 0.01, 0.1]
|
| 292 |
+
regularizations = [0.0, 0.001, 0.01]
|
| 293 |
+
|
| 294 |
+
# Use GridSearchCV to find the optimal hyperparameters
|
| 295 |
+
from sklearn.model_selection import GridSearchCV
|
| 296 |
+
param_grid = dict(batch_size=batch_sizes, learning_rate=learning_rates, regularization=regularizations)
|
| 297 |
+
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3)
|
| 298 |
+
grid_result = grid.fit(X_train, y_train)
|
| 299 |
+
|
| 300 |
+
# Print the best hyperparameters and corresponding score
|
| 301 |
+
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
|
| 302 |
+
|
| 303 |
+
# Import necessary modules from Keras
|
| 304 |
+
from keras.models import Sequential
|
| 305 |
+
from keras.layers import Dense
|
| 306 |
+
|
| 307 |
+
# Create a sequential model
|
| 308 |
+
model = Sequential()
|
| 309 |
+
|
| 310 |
+
# Add an input layer with 10 neurons and the ReLU activation function
|
| 311 |
+
model.add(Dense(10, activation='relu', input_shape=(10,)))
|
| 312 |
+
|
| 313 |
+
# Add a hidden layer with 5 neurons and the ReLU activation function
|
| 314 |
+
model.add(Dense(5, activation='relu'))
|
| 315 |
+
|
| 316 |
+
# Add an output layer with 1 neuron and the sigmoid activation function
|
| 317 |
+
model.add(Dense(1, activation='sigmoid'))
|
| 318 |
+
|
| 319 |
+
# Compile the model by specifying the loss function and optimizer
|
| 320 |
+
model.compile(loss='binary_crossentropy', optimizer='adam')
|
| 321 |
+
|
| 322 |
+
# Train the model using the fit method
|
| 323 |
+
# Pass the training data (X_train, y_train), specify the number of epochs (10), and the batch size (32)
|
| 324 |
+
model.fit(X_train, y_train, epochs=10, batch_size=32)
|
| 325 |
+
|
| 326 |
|
|
|