text_plain_output_1.png
from keras.layers import Dense

from keras.layers import Dense

from keras_tuner import HyperModel

from sklearn.preprocessing import MinMaxScaler

from tcn import TCN

from tensorflow import keras

import keras

import kerastuner as kt

import numpy as np

import pandas

import tensorflow as tf

import tensorflow as tf
from tensorflow import keras
from keras.layers import Dense
from keras_tuner import HyperModel
import kerastuner as kt
from tcn import TCN
def tcnModel(trainData, validateData, numOfObservedRecords: int, numOfPredictingRecords: int):
    hyperModel = TCNModel(numOfObservedRecords=numOfObservedRecords, numOfPredictingRecords=numOfPredictingRecords, numOfFeatures=trainData.shape[2])
    bayesianTuner = kt.tuners.BayesianOptimization(hyperModel, objective='mse', max_trials=3, project_name='kerastuner_bayesian_poc', executions_per_trial=5, overwrite=True)
    bayesianTuner.search(trainData, validateData, epochs=100, validation_split=0.2, verbose=0)
    return bayesianTuner.get_best_models(num_models=1)[0]
class TCNModel(HyperModel):
    def __init__(self, numOfObservedRecords, numOfPredictingRecords, numOfFeatures):
        self.numOfObservedRecords = numOfObservedRecords
        self.numOfPredictingRecords = numOfPredictingRecords
        self.numOfFeatures = numOfFeatures
    def build(self, params):
        model = keras.Sequential()
        model.add(TCN(input_shape=(self.numOfObservedRecords, 1), kernel_size=params.Int('units', min_value=2, max_value=8, step=1), use_skip_connections=params.Boolean('use_skip_connections'), use_batch_norm=False, use_weight_norm=False, use_layer_norm=True, dropout_rate=params.Float('drop_out', 0, 0.5, 0.1), nb_filters=params.Int('units', min_value=32, max_value=512, step=32)))
        model.add(Dense(self.numOfPredictingRecords, activation=params.Choice('dense_activation', values=['relu', 'tanh', 'sigmoid'], default='relu')))
        model.compile(loss='mse', metrics=['mse'], optimizer=tf.keras.optimizers.Adam(params.Choice('learning_rate', values=[0.01, 0.001, 0.0001])))
        return model

from sklearn.preprocessing import MinMaxScaler
def scale3DArray(arr: np.ndarray, scaler: any=MinMaxScaler(feature_range=(-1, 1))) -> np.ndarray:
    scaledArr = np.reshape(arr, (arr.shape[0], arr.shape[1] * arr.shape[2]))
    scaledArr = scaler.fit_transform(scaledArr)
    scaledArr = np.reshape(scaledArr, tuple(arr.shape))
    return scaledArr
def scale3DArrayReturningScaler(arr: np.ndarray, scaler: any=MinMaxScaler(feature_range=(-1, 1))):
    scaledArr = np.reshape(arr, (arr.shape[0], arr.shape[1] * arr.shape[2]))
    scaledArr = scaler.fit_transform(scaledArr)
    scaledArr = np.reshape(scaledArr, tuple(arr.shape))
    return (scaledArr, scaler)

data = pandas.read_csv('../input/nyc-taxi-traffic/dataset.csv')
data = list(data['value'])[:10200]

data_train = data[:int(len(data) * 0.8)]
data_test = data[int(len(data) * 0.8):]
O_data_test = data_test

minmaxSc = MinMaxScaler()
data_train = np.array(data_train)
data_train = np.reshape(data_train, (int(data_train.shape[0] / 120), 120))
data_train = minmaxSc.fit_transform(data_train)
train, validate = np.array_split(data_train, [72], 1)
train = np.reshape(train, (train.shape[0], train.shape[1], 1))
validate = np.reshape(validate, (validate.shape[0], validate.shape[1], 1))

model = tcnModel(train, validate, 72, 48)