timeseries = np.asarray(df.Close) timeseries = np.atleast_2d(timeseries) if timeseries.shape[0] == 1: timeseries = timeseries.T X = np.atleast_3d( np.array([ timeseries[start:start + look_back] for start in range(0, timeseries.shape[0] - look_back) ])) y = timeseries[look_back:] predictors = ['Close'] model = Sequential() model.add( LSTM(input_shape=(1, ), input_dim=1, output_dim=6, return_sequences=True)) model.add( LSTM(input_shape=(1, ), input_dim=1, output_dim=6, return_sequences=False)) model.add(Dense(1)) model.add(Activation('linear')) model.compile(loss="mse", optimizer="rmsprop") model.fit(X, y, epochs=1000, batch_size=80, verbose=1, shuffle=False) df['Pred'] = df.loc[df.index[0], 'Close'] for i in range(len(df.index)): if i <= look_back: continue a = None for c in predictors: b = df.loc[df.index[i - look_back:i], c].as_matrix() if a is None:
a = data[i:(i + look_back), 0] dataX.append(a) dataY.append(data[i + look_back, 0]) return np.array(dataX), np.array(dataY) data = np.reshape(time_series_data.values, (-1, 1)) scaler = MinMaxScaler(feature_range=(0, 1)) data = scaler.fit_transform(data) X, y = create_dataset(data, look_back) # reshape input to be [samples, time steps, features] X = np.reshape(X, (X.shape[0], 1, X.shape[1])) # create model and fit the LSTM network model = Sequential() model.add(LSTM(50, input_shape=(1, look_back))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X, y, epochs=50, batch_size=1, verbose=2) # make prediction val = data[-look_back:] # last batch in the training data predictions = [] # forecast the next 200 data points for _ in range(200): pred = model.predict(val.reshape(1, 1, look_back)) predictions = np.append(predictions, pred) val = np.append(np.delete( val, 0), pred) # update the data batch to be fed in the next iteration predictions = predictions.reshape(predictions.shape[0], 1)
X_as_ds, y_as_ds = timeseries_to_dataset( all_df['Monthly Mean Total Sunspot Number'].values, TIME_STEPS) X, y = X_as_ds[:-TEST_PERIOD], y_as_ds[:-TEST_PERIOD] X_test, y_test = X_as_ds[-TEST_PERIOD:], y_as_ds[-TEST_PERIOD:] print(X.shape, X_test.shape, y.shape, y_test.shape) from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense model = tf.keras.Sequential() model.add(LSTM(2, activation = 'relu', return_sequences = True, input_shape = X.shape[-2:])) model.add(LSTM(12, activation = 'relu')) model.add(Dense(1)) model.compile(optimizer = 'adam', loss = 'mse') model.summary() history = model.fit(x = X, y = y, epochs = 20, validation_split = 0.15, verbose = 0) rcParams['figure.figsize'] = 8, 6 plot_loss(history)
X = X.reshape((X.shape[0], X.shape[1], n_features)) print(X) # + from keras.models import Sequential from keras.layers import LSTM from keras.layers import Dense # Vanilla LSTM model = Sequential() #model.add(LSTM(200,activation='relu', input_shape=(n_steps, n_features))) model.add( LSTM(200, activation='relu', return_sequences=True, input_shape=(n_steps, n_features))) model.add(LSTM(200, activation='relu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # - # Fit model model.fit(X, y, epochs=500, verbose=0) x_input = X #x_input = x_input.reshape((1,n_steps,n_features)) yhat = model.predict(x_input, verbose=0) print(yhat)
plt.plot(df['Close'][3237:],color = 'darkgreen',label='True') plt.legend(loc='upper left') plt.title("Forecast VN Index") plt.show() fc_series # build neural network to forecast #Dependencies import keras from keras.models import Sequential from keras.layers import Dense from keras.optimizers import Adam # Neural network model = Sequential() model.add(Dense(8, input_dim=11, activation='relu')) model.add(Dense(4, activation='relu')) model.add(Dense(1, activation='linear')) model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.008)) x_train=df.loc[:3236,:].drop(['Label','Close'],axis=1) x_test=df.loc[3237:4622,:].drop(['Label','Close'],axis=1) y_train=df.loc[1:3237,'Close'] y_test=df.loc[3238:,'Close'] history = model.fit(x_train, y_train,validation_data = (x_test,y_test), epochs=100, batch_size=30) # import matplotlib.pyplot as plt # plt.plot(history.history['acc']) # plt.plot(history.history['val_acc'])
x = [] y = [] for i in range(len(train_data) - window_input - 1): x.append(train_data[i:i + window_input]) y.append(train_data[i + 1:i + window_input + 1]) x_train = np.array(x) y_train = np.array(y) # x_train = np.reshape(x_train, (x_train.shape[0], window_input, 1)) model = Sequential() model.add( Dense(16, input_dim=window_input, kernel_initializer='normal', activation='relu')) model.add(Dropout(0.5)) model.add(Dense(16, kernel_initializer='normal', activation='relu')) model.add(Dense(window_input, kernel_initializer='normal')) model.compile(loss="mean_squared_error", optimizer="adam") model.fit(x_train, y_train, epochs=200, batch_size=32, shuffle=True, verbose=0) preds = train_data[-window_input:]
train = values[:n_train_time_per_frame, :] test = values[n_train_time_per_frame:, :] n_features = latent_dim n_obs = n_steps * n_features train_X, train_y = train[:, :n_obs], train[:, n_obs:] test_X, test_y = test[:, :n_obs], test[:, n_obs:] train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1])) test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1])) logger_lstm1 = keras.callbacks.TensorBoard(log_dir = "log_lstm1", write_graph = True, histogram_freq = 40) # design model 1 model = Sequential() model.add(LSTM(latent_dim * 8, input_shape=(train_X.shape[1], train_X.shape[2]))) model.add(Dense(latent_dim * 4, activation='relu')) model.add(Dense(latent_dim * 4, activation='relu')) model.add(Dense(latent_dim * 2, activation='relu')) model.add(Dense(latent_dim * 2, activation='relu')) model.add(Dense(latent_dim)) model.compile(loss='mae', optimizer='adam') # fit network history = model.fit(train_X, train_y, epochs=200, batch_size=50, validation_data=(test_X, test_y), verbose=2, callbacks = [logger_lstm1], shuffle=False)
modelz = ['VRNN', 'LSTM', 'GRU'] for typez in modelz: #data for lstm trainX = X[indX < '2017-09-01'] testX = X[indX >= '2017-09-01'] trainY = Y[indY < '2017-09-03'] testY = Y[indY >= '2017-09-03'] trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) print('\n\n Running Model type: {}'.format(typez)) model = Sequential() if typez == 'VRNN': model.add( SimpleRNN(units=100, activation='tanh', input_shape=(1, 1), return_sequences=False)) elif typez == 'LSTM': model.add( LSTM(units=100, activation='tanh', input_shape=(1, 1), return_sequences=False)) elif typez == 'GRU': model.add( GRU(units=100, activation='tanh', input_shape=(1, 1), return_sequences=False)) else:
class Model(Config): def __init__(self, m, station, var, predictives): self.var = var self.station = station self.predictives = predictives if var in self.predictives: self.predictives.remove(var) self.alg = m['alg'](**m["args"]) self.date = None self.param_grid = m['param_grid'] if 'param_grid' in m else None self.actual_holdout = None self.pred_holdout = None self.actual_train = None self.actual_test = None self.pred = None self.whole_pred = None self.se = None self.conf = None self.metrics = {} self.holdout_metrics = {} self.created = datetime.now() self.model_data = None self.lstm_history = None self.lstm_train_pred = None self.lstm_test_pred = None self.supervised_learn = None def set_props(self, alg, df): self.algorithm = alg self.start_time = df['Date'].min() self.end_time = df['Date'].max() self.n_records = df.shape[0] def get_meta(self): return dict( algorithm=self.algorithm, supervised_learning=self.supervised_learn, predictives=self.predictives, start_time=self.start_time, end_time=self.end_time, n_records=self.n_records, metrics=self.metrics, created=self.created, ) def dataset_split( self, data, ratio, supervised_learning=Config.MODELLING_CONFIG["SUPERVISED_LEARNING"], shuffle_data=False): ratio = ratio or self.MODELLING_CONFIG["SPLIT_RATIO"] if supervised_learning == False: self.logger.info( " Intitiate forecasting model train-test split ...") train_size = int( len(data) * Config.MODELLING_CONFIG["SPLIT_RATIO"]) train, test = data.iloc[0:train_size], data.iloc[ train_size:len(data)] self.logger.info( " Training dataset: {}, Testing dataset: {}".format( train.shape, test.shape)) elif supervised_learning == True: self.logger.info( " Intitiate supervised learning model train-test split ...") train, test = train_test_split( data, test_size=ratio, shuffle=shuffle_data, random_state=self.MODELLING_CONFIG["RANDOM_STATE"], ) self.logger.info( " Training dataset: {}, Testing dataset: {}".format( train.shape, test.shape)) return train, test def dl_univariate_data(self, data, start_index, end_index, history_size, target_size): data = [] labels = [] start_index = start_index + history_size if end_index is None: end_index = len(data) - target_size for i in range(start_index, end_index): indices = range(i - history_size, i) # # Reshape data from (history_size,) to (history_size, 1) data.append(np.reshape(data[indices], (history_size, 1))) labels.append(data[i + target_size]) return np.array(data), np.array(labels) def multivariate_data(self, data, target, start_index, end_index, history_size, target_size, step, single_step=False): data = [] labels = [] start_index = start_index + history_size if end_index is None: end_index = len(data) - target_size for i in range(start_index, end_index): indices = range(i - history_size, i, step) data.append(data[indices]) if single_step: labels.append(target[i + target_size]) else: labels.append(target[i:i + target_size]) return np.array(data), np.array(labels) @staticmethod def mean_absolute_percentage_error(y_true, y_pred): mape = np.mean(np.abs((y_true - y_pred) / y_true + 1e-6)) * 100 if type(mape) == pd.Series: mape = mape[0] return mape @staticmethod def root_mean_square_error(y_true, y_pred): rmse = math.sqrt(mean_squared_error(y_true, y_pred)) return rmse @staticmethod def mean_absolute_error(y_true, y_pred): mae = mean_absolute_error(y_true, y_pred) return mae def evaluate(self, actual, pred): r2score = r2_score(actual, pred) MAPE = Model.mean_absolute_percentage_error(actual, pred) MAE = mean_absolute_error(actual, pred) rmse = Model.root_mean_square_error(actual, pred) metrics = dict(MAE=MAE, MAPE=MAPE, RMSE=rmse) # R2_Score=r2score return metrics def predict(self, X_test): """predict new cases""" if all(p in X_test for p in self.predictives): X_test = X_test[self.predictives].astype(float) X_test.fillna(method=self.MODELLING_CONFIG["STATUS_MISSING_FILL"], inplace=True) if any(X_test.isnull().values.all(axis=0)): return [np.nan] * X_test.shape[0] preds = self.alg.predict(X_test.dropna()) return preds else: return [np.nan] * X_test.shape[0] def regression_scalar(self, data): """Regression using linear algorithms""" df = data[self.predictives + [self.var, "Date"]] print(self.predictives) print(len(self.predictives)) scaler = MinMaxScaler() scaler.fit(df.drop(columns=[self.var, "Date"]).values) train, test = self.dataset_split(df) self.date = test["Date"] X_train = train.drop(columns=[self.var, "Date"]) X_test = test.drop(columns=[self.var, "Date"]) y_train = train[[self.var]] self.actual = test[[self.var]].values.ravel() self.example = X_train.iloc[0].values # # scaling X_train = scaler.transform(X_train) X_test = scaler.transform(X_test) self.alg.fit(X_train, y_train.values.ravel()) self.pred = self.alg.predict(X_test) self.metrics = self.evaluate(self.actual, self.pred) def regression_tree(self, data, metric_eval, cv_type): """regression using tree-based algrithms""" df = data[self.predictives + [self.var, "Date"]] # Train/Test if metric_eval == "test": if self.MODELLING_CONFIG["HOLDOUT_PERCENT"] != 0: n_holdout = max( 1, int(df.shape[0] * self.MODELLING_CONFIG["HOLDOUT_PERCENT"])) holdout = df.iloc[-n_holdout:, ] X_holdout = holdout.drop(columns=[self.var, "Date"]) self.actual_holdout = holdout[[self.var]].values.ravel() df = df.iloc[:-n_holdout, ] train, test = self.dataset_split( df, ratio=Config.MODELLING_CONFIG["SPLIT_RATIO"]) self.date = test["Date"] X_train = train.drop(columns=[self.var, "Date"]) X_test = test.drop(columns=[self.var, "Date"]) y_train = train[[self.var]] self.actual = test[[self.var]].values.ravel() self.example = X_train.iloc[0].values if self.param_grid != None: #print(" Running Grid Search...") param_grid_1 = { k: v for k, v in self.param_grid.items() if k in ["max_depth", "num_leaves", "n_estimators"] } n_folds = int(100 / (100 * self.MODELLING_CONFIG["SPLIT_RATIO"])) + 1 grid_search_rf = GridSearchCV(estimator=self.alg, param_grid=param_grid_1, scoring='r2', cv=n_folds, n_jobs=8) grid_search_rf.fit(X_train, y_train.values.ravel()) print(' Best Params: ', grid_search_rf.best_params_) print(' R2-Score: ', grid_search_rf.best_score_) self.alg = self.alg.set_params(**grid_search_rf.best_params_) self.alg.fit(X_train, y_train.values.ravel()) self.pred = self.alg.predict(X_test) self.metrics = self.evaluate(self.actual, self.pred) if self.MODELLING_CONFIG["HOLDOUT_PERCENT"] != 0: self.pred_holdout = self.alg.predict(X_holdout) self.metrics_holdout = self.evaluate(self.actual_holdout, self.pred_holdout) # Cross-validation elif (metric_eval == "cv"): if self.MODELLING_CONFIG["HOLDOUT_PERCENT"] != 0: n_holdout = int(df.shape[0] * self.MODELLING_CONFIG["HOLDOUT_PERCENT"]) holdout = df.iloc[-n_holdout:, ] X_holdout = holdout.drop(columns=[self.var, "Date"]) self.actual_holdout = holdout[[self.var]].values.ravel() df = df.iloc[:-n_holdout, ] X_train = df.drop(columns=[self.var, "Date"]) y_train = df[[self.var]] self.actual = df[[self.var]].values.ravel() self.date = df["Date"] self.example = X_train.iloc[0].values fold = LeaveOneOut() if cv_type == "loo" else int( 100 / (100 * self.MODELLING_CONFIG["SPLIT_RATIO"])) if self.param_grid != None: print(" Running Grid Search...") param_grid_1 = { k: v for k, v in self.param_grid.items() if k in ["max_depth", "num_leaves", "n_estimators"] } n_folds = int(100 / (100 * self.MODELLING_CONFIG["SPLIT_RATIO"])) + 1 grid_search_rf = GridSearchCV(estimator=self.alg, param_grid=param_grid_1, scoring='r2', cv=n_folds, n_jobs=8) grid_search_rf.fit(X_train, y_train.values.ravel()) print(' Best Params: ', grid_search_rf.best_params_) print(' R2-Score: ', grid_search_rf.best_score_) self.alg = self.alg.set_params(**grid_search_rf.best_params_) self.alg.fit(X_train, y_train.values.ravel()) self.pred = cross_val_predict(estimator=self.alg, X=X_train, y=y_train.values.ravel(), cv=fold, n_jobs=-1) self.metrics = self.evaluate(self.actual, self.pred) if self.MODELLING_CONFIG["HOLDOUT_PERCENT"] != 0: self.pred_holdout = self.alg.predict(X_holdout) self.metrics_holdout = self.evaluate(self.actual_holdout, self.pred_holdout) elif (metric_eval == "cv"): if self.MODELLING_CONFIG["HOLDOUT_PERCENT"] != 0: self.n_holdout = int(df.shape[0] * self.MODELLING_CONFIG["HOLDOUT_PERCENT"]) holdout = df.iloc[-self.n_holdout:, ] X_holdout = holdout.drop(columns=[self.var, "Date"]) self.actual_holdout = holdout[[self.var]].values.ravel() df = df.iloc[:-self.n_holdout, ] train, test = self.dataset_split(df) self.date = test["Date"] X_train = train.drop(columns=[self.var, "Date"]) X_test = test.drop(columns=[self.var, "Date"]) y_train = train[[self.var]] self.actual = test[[self.var]].values.ravel() if self.param_grid != None: print(" Running Grid Search...") param_grid_1 = { k: v for k, v in self.param_grid.items() if k in ["max_depth", "num_leaves", "n_estimators"] } n_folds = int(100 / (100 * self.MODELLING_CONFIG["SPLIT_RATIO"])) + 1 grid_search_rf = GridSearchCV(estimator=self.alg, param_grid=param_grid_1, scoring='r2', cv=n_folds, n_jobs=8) grid_search_rf.fit(X_train, y_train.values.ravel()) ## Second pass for grid search on learning params print(' Best Params: ', grid_search_rf.best_params_) print(' R2-Score: ', grid_search_rf.best_score_) self.alg = self.alg.set_params(**grid_search_rf.best_params_) self.alg.fit(X_train, y_train.values.ravel()) self.pred = self.alg.predict(X_test) self.metrics = self.evaluate(self.actual, self.pred) if self.MODELLING_CONFIG["HOLDOUT_PERCENT"] != 0: self.pred_holdout = self.alg.predict(X_holdout) self.metrics_holdout = self.evaluate(self.actual_holdout, self.pred_holdout) def forecast_model(self, data, seasonal=Config.MODELLING_CONFIG["SEASONAL_OPTION"]): df = data[self.predictives] train, test = self.dataset_split( df, self.MODELLING_CONFIG["SPLIT_RATIO"], supervised_learning=Config.MODELLING_CONFIG["SUPERVISED_LEARNING"]) history = [x for x in train] prediction_list = list() if seasonal == True: if self.alg == 'SARIMA': train, test = np.log10(train), np.log10(test) self.alg = pm.auto_arima(train, start_p=1, d=0, start_q=1, max_p=5, max_d=2, max_q=5, m=7, start_P=0, D=0, start_Q=0, max_P=5, max_D=2, max_Q=5, seasonal=True, trace=True, error_action='ignore', suppress_warnings=True, stepwise=True) for data in range(len(test)): self.alg = self.alg.fit(disp=1) self.pred = self.alg.predict(n_periods=1) prediction_list.append(self.pred) self.pred self.actual_test = test[data] history.append(self.actual_test) elif self.alg == 'HOLT_WINTER': self.alg = self.alg( train, seasonal_periods=Config. MODELLING_CONFIG["HOLT_WINTER_SEASON"], trend=Config.MODELLING_CONFIG["HOLT_WINTER_TREND"], seasonal=Config.MODELLING_CONFIG["HOLT_WINTER_SEASONAL"]) self.pred = self.alg.forecast(len(test)) elif seasonal == False: for data in range(len(test)): self.alg = ARIMA(train, order=(Config.MODELLING_CONFIG['ARIMA_P'], Config.MODELLING_CONFIG['ARIMA_D'], Config.MODELLING_CONFIG['ARIMA_Q'])) self.alg = self.alg.fit(disp=1) self.pred, self.se, self.conf = self.alg.forecast() prediction_list.append(self.pred) self.actual_test = test[data] history.append(self.actual_test) self.metrics = self.evaluate(self.actual_test, self.pred) def lstm_model(self, data): df = data[self.predictives] train, test = self.dataset_split(df, self.MODELLING_CONFIG["SPLIT_RATIO"]) scaler = MinMaxScaler() scaler.fit(train) train = scaler.transform(train) test = scaler.transform(test) X_train, y_train = self.create_dataset( train, time_steps=Config.RNN_CONFIG["TIME_STEPS"]) X_test, y_test = self.create_dataset( test, time_steps=Config.RNN_CONFIG["TIME_STEPS"]) self.actual_train = y_train self.actual_test = y_test X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1])) X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1])) self.alg = keras.Sequential() self.alg.add( LSTM(units=Config.RNN_CONFIG["UNITS"], input_shape=(X_train.shape[1], X_train.shape[2]))) self.alg.add(Dropout(rate=Config.RNN_CONFIG["DROPOUT_RATE"])) self.alg.add(Dense(units=Config.RNN_CONFIG["DENSE_UNIT"])) self.alg.compile(loss=Config.RNN_CONFIG["LOSS_FUNC"], optimizer=Config.RNN_CONFIG["OPTIMIZER"]) self.lstm_history = self.alg.fit( X_train, y_train, epochs=Config.RNN_CONFIG["EPOCHS"], batch_size=Config.RNN_CONFIG["BATCH_SIZE"], validation_split=Config.RNN_CONFIG["VALIDATION_SPLIT"], shuffle=Config.RNN_CONFIG["SHUFFLE"], validation_data=(X_test, y_test), verbose=1, ) self.logger.info(self.alg.summary()) self.lstm_train_pred = self.alg.predict(X_train) self.lstm_test_pred = self.alg.predict(X_test) self.lstm_train_pred = scaler.inverse_transform(self.lstm_train_pred) y_train = scaler.inverse_transform([y_train]) self.lstm_test_pred = scaler.inverse_transform(self.lstm_test_pred) y_test = scaler.inverse_transform([y_test]) self.metrics = self.evaluate(self.actual_test[0], self.pred[:0]) def feature_importance_plot(self): fig, ax = plt.subplots(figsize=(10, len(self.predictives) / 2)) s = pd.Series(self.alg.feature_importances_, index=self.predictives) ax = s.sort_values(ascending=False).plot.barh() ax.invert_yaxis() patches = [ mpatches.Patch(label="Test Size: {}".format(self.actual.shape[0]), color='none') ] for alg, val in self.metrics.items(): patches.append( mpatches.Patch( label="{}: {:0.2f}".format(alg, val), color='none', )) plt.legend(handles=patches, loc='lower right') return fig def residual_plot(self): fig = plt.figure(figsize=(10, 6)) gs = gridspec.GridSpec(nrows=1, ncols=2, width_ratios=[3, 1]) ax1 = fig.add_subplot(gs[0]) residual = self.actual - self.pred sns.residplot(x=self.pred, y=residual, ax=ax1) ax1.set_ylabel("Residual") ax1.set_xlabel("Predict") ax1.set_title(self.station) ax2 = fig.add_subplot(gs[1], sharey=ax1) ax2.hist(residual, orientation="horizontal") ax2.set_xlabel('Residual Distribution') return fig
print(trainX[1], trainY[1]) # RESHAPING TRAIN AND TEST DATA trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1])) trainX.shape, testX.shape # # LSTM 모델 (전모델로) 돌려보는 구간 # # In[47]: # LSTM MODEL step_size = 1 model = Sequential() model.add(LSTM(32, input_shape=(1, step_size), return_sequences=True)) model.add(LSTM(16)) model.add(Dense(1)) model.add(Activation('linear')) # In[48]: # MODEL COMPILING AND TRAINING model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae']) # Try mae, adam, adagrad and compare!!! model.fit(trainX, trainY, epochs=30, batch_size=1, verbose=2) # In[49]: # MAE : 예측과 타깃 사이 거리의 절댓값으로 여기서 MAE가 0.5면 정규처리된 가격이 0.5 -> 예를들어 1000이 정규화되어서 0.01.정도가 된거면 0.5 0> 50000
from kapre.time_frequency import Melspectrogram from kapre.utils import Normalization2D from kapre.augmentation import AdditiveNoise # 6 channels (!), maybe 1-sec audio signal, for an example. #input_shape = (6, 44100) sr = len(data) input_shape = (1, sr) model = Sequential() # A mel-spectrogram layer model.add(Melspectrogram(n_dft=512, n_hop=256, input_shape=input_shape, padding='same', sr=sr, n_mels=128, fmin=0.0, fmax=sr/2, power_melgram=1.0, return_decibel_melgram=False, trainable_fb=False, trainable_kernel=False, name='trainable_stft')) # add some additive white noise model.add(AdditiveNoise(power=0.2)) # we normalise it per frequency model.add(Normalization2D(str_axis='freq')) # or 'channel', 'time', 'batch', 'data_sample' # After this, it's just a usual keras workflow. For example.. # Add some layers, e.g., model.add(some convolution layers..) # Compile the model #model.compile('adam', 'categorical_crossentropy') # if single-label classification
#converting dataset into x_train and y_train scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(dataset) x_train, y_train = [], [] for i in range(60, len(train)): x_train.append(scaled_data[i - 60:i, 0]) y_train.append(scaled_data[i, 0]) x_train, y_train = np.array(x_train), np.array(y_train) x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) # create and fit the LSTM network model = Sequential() model.add( LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1))) model.add(LSTM(units=50)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(x_train, y_train, epochs=150, batch_size=10, verbose=2) #predicting 246 values, using past 60 from the train data inputs = new_data[len(new_data) - len(valid) - 60:].values inputs = inputs.reshape(-1, 1) inputs = scaler.transform(inputs) X_test = [] for i in range(60, inputs.shape[0]): X_test.append(inputs[i - 60:i, 0]) X_test = np.array(X_test)
# ### Neural Network # # 1. Create a neural network using Tensorflow-Keras # 2. Split the dataset into training and test sets # 3. Train the neural network using the training set. # 4. Evaluate the neural network using the test set. # # # + colab={"base_uri": "https://localhost:8080/", "height": 227} colab_type="code" id="Iv6XG7Hb6m48" outputId="c507bcc0-37a0-4769-9982-2ee6040283f2" # https://www.tensorflow.org/guide/keras/overview # Create a simple Neural Network with 2 Dense Layers from tensorflow.keras import layers model = tf.keras.Sequential() model.add(layers.Dense(16, input_shape=(2, ), activation='relu')) model.add(layers.Dense(1)) model.summary() # + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="AIo_kr226s3t" outputId="6cdf884f-971a-4cac-f795-1a12c9160ee5" #X_train, X_test, y_train, y_test = train_test_split(X, y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, shuffle=False) X_train.shape, X_test.shape, y_train.shape, y_test.shape # + colab={"base_uri": "https://localhost:8080/", "height": 429} colab_type="code" id="zQv6v9mM5i7g" outputId="6149f417-e2f0-442c-9fb6-d93c034b614f" # Note that the data is shuffled in time # This is okay because we already preserved the history!
#converting dataset into x_train and y_train scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(dataset) x_train, y_train = [], [] for i in range(60,len(train)): x_train.append(scaled_data[i-60:i,0]) y_train.append(scaled_data[i,0]) x_train, y_train = np.array(x_train), np.array(y_train) x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1)) # create and fit the LSTM network model = Sequential() model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1))) model.add(LSTM(units=50)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(x_train, y_train, epochs=1, batch_size=1, verbose=2) #predicting 246 values, using past 60 from the train data inputs = new_data[len(new_data) - len(valid) - 60:].values inputs = inputs.reshape(-1,1) inputs = scaler.transform(inputs) X_test = [] for i in range(60,inputs.shape[0]): X_test.append(inputs[i-60:i,0]) X_test = np.array(X_test)