This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Other metrics: | |
| train_pred = model.predict(x_train) | |
| val_pred = model.predict(validation['x']) | |
| test_pred = model.predict(test['x']) | |
| print("Displaying other metrics:") | |
| print("\t\tMedAE\tMAPE") | |
| print(f"Train:\t{round(median_absolute_error(y_train, train_pred) ,3)}\t{round(mean_absolute_percentage_error(y_train, train_pred), 3)}") | |
| print(f"Val :\t{round(median_absolute_error(validation['y'], val_pred) ,3)}\t{round(mean_absolute_percentage_error(validation['y'], val_pred), 3)}") | |
| print(f"Test :\t{round(median_absolute_error(test['y'], test_pred) ,3)}\t{round(mean_absolute_percentage_error(test['y'], test_pred), 3)}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from numpy.random import seed | |
| seed(1) | |
| from tensorflow import random, config | |
| random.set_seed(1) | |
| config.experimental.enable_op_determinism() | |
| import random | |
| random.seed(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tensorflow.python.keras import Input | |
| from tensorflow.python.keras.models import Sequential | |
| from tensorflow.python.keras.layers import Dense | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| from sklearn.metrics import median_absolute_error, mean_absolute_percentage_error | |
| from numpy import arange |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Evaluate on test set: | |
| test = pd.read_csv('dataset/regression/cosine/test.csv') | |
| test_results = model.evaluate(test['x'], test['y'], verbose=1) | |
| print(f'Test set: - loss: {test_results[0]} - mae: {test_results[1]}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Display function: | |
| plt.scatter(dataset['x'], dataset['y']) | |
| x = arange(0, 25, 0.1).tolist() | |
| plt.plot(x, model.predict(x), c='red') | |
| plt.xlabel('x') | |
| plt.ylabel('y') | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Display loss: | |
| plt.plot(history.history['loss']) | |
| plt.plot(history.history['val_loss']) | |
| plt.title('model loss') | |
| plt.ylabel('loss') | |
| plt.xlabel('epoch') | |
| plt.legend(['train', 'test']) | |
| plt.show() | |
| # Display metric: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Train: | |
| epochs = 1750 | |
| x_train, y_train = train['x'], train['y'] | |
| model.compile(loss='mse', optimizer='adam', metrics=['mae']) | |
| history = model.fit(x_train, y_train, epochs=epochs, batch_size=64, verbose=1, validation_data=(x_val, y_val)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create model: | |
| model = Sequential() | |
| model.add(Input(shape=(1,))) | |
| model.add(Dense(200, activation='sigmoid')) | |
| model.add(Dense(200, activation='sigmoid')) | |
| model.add(Dense(200, activation='sigmoid')) | |
| model.add(Dense(1, activation='linear')) | |
| print(model.summary()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def split_dataset(dataset, train_frac=0.7): | |
| train = dataset.sample(frac=train_frac) | |
| val = dataset.drop(train.index) | |
| return train, val | |
| # Split dataset into train and validation: | |
| train, validation = split_dataset(dataset, train_frac=0.7) | |
| plt.scatter(train['x'], train['y'], c='blue', alpha=0.4) | |
| plt.scatter(validation['x'], validation['y'], c='red', alpha=0.4) | |
| plt.xlabel('x') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Read dataset: | |
| dataset = pd.read_csv('dataset/regression/cosine/train.csv') | |
| print(f"There are {len(dataset.index)} instances.") | |
| print(dataset.head()) | |
| plt.scatter(dataset['x'], dataset['y']) | |
| plt.xlabel('x') | |
| plt.ylabel('y') | |
| plt.show() |
NewerOlder