I am working through the tutorial here. I just cleaned up the code for myself but other than that code is as in original tutorial.

I am using the pollution data from the following link

When I try to clear session after each calibration, I get the error:

ValueError: Tensor Tensor("dense_1/BiasAdd:0", shape=(?, 1), dtype=float32) is not an element of this graph.

My code is:

import sys
from math import sqrt
import numpy as np
from matplotlib import pyplot
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from datetime import datetime

def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = pd.DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        if i == 0:
            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    # put it all together
    agg = pd.concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
    return agg

def parseHrtr(x):
    return datetime.strptime(x, '%Y %m %d %H')

def Fn_trainHere(train_X, train_y, test_X, test_y):
    from keras.models import Sequential
    from keras.layers import Dense
    from keras.layers import LSTM
    from datetime import datetime
    from keras import backend as K

    model = Sequential()
    model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2])))
    model.compile(loss='mae', optimizer='adam')
    model.fit(train_X, train_y, epochs=5, batch_size=72,
                             validation_data=(test_X, test_y), verbose=2, shuffle=False)
    return model

def Fn_predictHere(calibParams, nn, test_X, lagHours, numOfFeatures, scaler, test_y):
    import keras
    model = calibParams[nn]
    yhat = model.predict(test_X)
    test_X = test_X.reshape((test_X.shape[0], lagHours * numOfFeatures))
    inv_yhat = np.concatenate((yhat, test_X[:, -(numOfFeatures - 1):]), axis=1)
    inv_yhat = scaler.inverse_transform(inv_yhat)
    inv_yhat = inv_yhat[:, 0]

    test_y = test_y.reshape((len(test_y), 1))
    inv_y = np.concatenate((test_y, test_X[:, -(numOfFeatures - 1):]), axis=1)
    inv_y = scaler.inverse_transform(inv_y)
    inv_y = inv_y[:, 0]
    return sqrt(mean_squared_error(inv_y, inv_yhat))

class lstmOO():
    def __init__(self, lagHours, numOfFeatures, numOfTrainDays):
        self.lagHours, self.numOfFeatures, self.numOfTrainDays = lagHours, numOfFeatures, numOfTrainDays

    def readData(self):
        dataset = pd.read_csv(r'C:\pmiData\raw.csv',
                           parse_dates=[['year', 'month', 'day', 'hour']], index_col=0, date_parser=parseHrtr)
        dataset.drop('No', axis=1, inplace=True)
        dataset.columns = ['pollution', 'dew', 'temp', 'press', 'wnd_dir', 'wnd_spd', 'snow', 'rain']
        dataset.index.name = 'date'
        dataset['pollution'].fillna(0, inplace=True)
        # drop the first 24 hours
        dataset = dataset[24:]
        # save to file
        dataset = pd.read_csv(r'C:\pmiData\pollution.csv', header=0, index_col=0)
        values = dataset.values
        # integer encode direction
        encoder = LabelEncoder()
        values[:, 4] = encoder.fit_transform(values[:, 4])
        values = values.astype('float32')
        # normalize features
        self.scaler = MinMaxScaler(feature_range=(0, 1))
        scaled = self.scaler.fit_transform(values)
        # frame as supervised learning
        reframed = series_to_supervised(scaled, self.lagHours, 1)

        values = reframed.values
        n_train_hours = self.numOfTrainDays * 24
        self.train = values[:n_train_hours, :]
        self.test = values[n_train_hours:, :]

    def networkDesign(self):
        n_obs = self.lagHours * self.numOfFeatures
        train_X, train_y = self.train[:, :n_obs], self.train[:, -self.numOfFeatures]
        train_X = train_X.reshape((train_X.shape[0], self.lagHours, self.numOfFeatures))

        test_X, self.test_y = self.test[:, :n_obs], self.test[:, -self.numOfFeatures]
        self.test_X = test_X.reshape((test_X.shape[0], self.lagHours, self.numOfFeatures))

        self.calibParams = {}
        for nn in range(1, 3):
            model = Fn_trainHere(train_X, train_y, self.test_X, self.test_y)
            self.calibParams[nn] = model

    def forecast(self):
        for nn in range(1, 3):
            rmse = Fn_predictHere(self.calibParams, nn, self.test_X,
                                  self.lagHours, self.numOfFeatures, self.scaler, self.test_y)
            print('Test RMSE: %.3f' % rmse)

if __name__ == '__main__':
    lagHours, numOfFeatures, numOfTrainDays = 3, 8, 365
    lstmOO(lagHours, numOfFeatures, numOfTrainDays)

However, code runs fine when I comment out the line:

  • $\begingroup$ Welcome to SE:AI. Check out Data Science and Cross Validated--this type of question is more suitable for those stacks. (That said, if the answer you got here is a useful, do vote for it.) $\endgroup$ – DukeZhou May 17 '19 at 20:29

K.clear_session will also destroy the graph, so no nodes will exist afterwards. Having it there is a bug. So removing it will fix it


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.