How to utilize user survey answers and the actual usage in forecasting power usage using LSTM?
I have the pretrial survey and posttrial survey conducted of around 5000 users for Smart Meter installation.
With this I have power usage reading recorded every 30 min for around one and a half years.
survey csv is like this
User Question 1 Question 2 .......
1000 a a .......
1001 b a .......
. . . .......
. . . .......
. . . .......
5000 b a .......
power usage csv is like this
User date usage
1000 20001 0.003
1000 20002 0.025
.... ..... .....
.... ..... .....
.... ..... .....
.... ..... .....
.... ..... .....
1000 65047 0.52
1000 65048 0.14
I want to forecast power usage of the user based on the past power usage and the survey using LSTM. How to start with this?
See also questions close to this topic

CUDA_ERROR_OUT_OF_MEMORY tensorflow
As part of my study project, I try to train a neural network which makes a segmentation on images (based on FCN), and during the execution I received the following error message:
tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[1,67,1066,718] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
Note that I have fixed the batch_size to 1 and I have the same error even when I tried different image sizes , I put also just 1 image to train instead of 1600 still the same error! Could you help me to solve this problem ? What is it really about ?

Per image normalization vs Overall dataset normalization
I have a datasets of 1000 image.Using cnn For fingure gesture recognition. Should I normalize the Image by finding mean of that image only or the mean of entire dataset...and also suggest which library to use in python for the same

Softmax not resulting in a probability distribution in Python Implementation
I have a simple softmax implementation:
softmax = np.exp(x) / np.sum(np.exp(x), axis=0)
For x set as array here: https://justpaste.it/6wis7
You can load it as:
import numpy as np x = np.as (just copy and paste the content (starting from array))
I get:
softmax.mean(axis=0).shape (100,) # now all elements must be 1.0 here, since its a probability softmax.mean(axis=0) # all elements are not 1 array([0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158, 0.05263158])
Why is this implementation wrong? How to fix it?

Query about the input output shape of LSTM in Keras
I am trying to predict a time series with LSTM and writing my code in Keras with Tendorflow backend. I have 30 input features (continuous value) and 1 binary output. I would like to use 20 previous timesteps of each input feature to predict the output of next timestep. My batch size is 52. However, I am having difficulties when trying to define the shape of input layer.
As per the stacked LSTM example in Keras document, the last dimension of the 3D tensor will be 'data_dim'. Is it input dimension or output dimension? If this is output dimension, then I can't use more than one input feature as in my case the input_shape will be (batch_size=52,time_step=20,data_dim=1).
On the other hand, if the data_dim is input shape, then I have tried to define a four layer LSTM and the model shape is as follows.
Layer (type) Output Shape Param #
input_2 (InputLayer) (52, 20, 30) 0
lstm_3 (LSTM) (52, 20, 128) 81408
lstm_4 (LSTM) (52, 128) 131584
dense_2 (Dense) (52, 1) 129
Total params: 213,121 Trainable params: 213,121 Nontrainable params: 0
Have I written this part of code properly? My code snippet is as follows.
input_layer=Input(batch_shape=(batch_size,input_timesteps,input_dims)) lstm1=LSTM(num_neurons,activation = 'relu',dropout=0.0,stateful=False,return_sequences=True)(input_layer) lstm2=LSTM(num_neurons,activation = 'relu',dropout=0.0,stateful=False,return_sequences=False)(lstm1) output_layer=Dense(1, activation='sigmoid')(lstm2) model=Model(inputs=input_layer,outputs=output_layer)
I am not getting very good result and thus trying to understand whether there is any issue with my code.

Keras LSTM TypeError messages
I am trying to understand how to use keras for supply chain forecasting and i keep getting errors that i can't find help for elsewhere. I've tried to do similar tutorials; sunspot forecasting tutorial, pollution multivariate tutorial etc but i'm still not understanding how the input_shape argument works or how to organize my data to get it to be accepted by keras.
My dataset is a single time series describing the number of products we sold every month. I took that single time series, 107 months, and turned it into a 30 row, 77 column data set. I created a training set and test set from that.
but no matter what i do i can't get past even just creating a model without some kind of error.
Keras v#: 1.2.0
C:\Users\Ryan.B>python c "import keras; print(keras.version)"
Using TensorFlow backend.
1.2.0
Python Version: 3.5.4
Here's the code and respective errors i'm getting.
model = Sequential() model.add(LSTM(units=64, input_shape=(77, 1), output_dim=1)) C:\Python35\lib\sitepackages\keras\backend\tensorflow_backend.py in concatenate(tensors, axis) 1219 try: > 1220 return tf.concat_v2([to_dense(x) for x in tensors], axis) 1221 except AttributeError: AttributeError: module 'tensorflow' has no attribute 'concat_v2' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipythoninput2194f09519ff46> in <module>() 1 model = Sequential() > 2 model.add(LSTM(input_shape=(77, 1), output_dim = 1)) 3 #model.add(Dense(10, activation = 'relu')) 4 #model.add(Dense(1, activation = 'softmax')) C:\Python35\lib\sitepackages\keras\models.py in add(self, layer) 292 else: 293 input_dtype = None > 294 layer.create_input_layer(batch_input_shape, input_dtype) 295 296 if len(layer.inbound_nodes) != 1: C:\Python35\lib\sitepackages\keras\engine\topology.py in create_input_layer(self, batch_input_shape, input_dtype, name) 396 # and create the node connecting the current layer 397 # to the input layer we just created. > 398 self(x) 399 400 def add_weight(self, shape, initializer, name=None, C:\Python35\lib\sitepackages\keras\engine\topology.py in __call__(self, x, mask) 541 '`layer.build(batch_input_shape)`') 542 if len(input_shapes) == 1: > 543 self.build(input_shapes[0]) 544 else: 545 self.build(input_shapes) C:\Python35\lib\sitepackages\keras\layers\recurrent.py in build(self, input_shape) 761 self.W_f, self.U_f, self.b_f, 762 self.W_o, self.U_o, self.b_o] > 763 self.W = K.concatenate([self.W_i, self.W_f, self.W_c, self.W_o]) 764 self.U = K.concatenate([self.U_i, self.U_f, self.U_c, self.U_o]) 765 self.b = K.concatenate([self.b_i, self.b_f, self.b_c, self.b_o]) C:\Python35\lib\sitepackages\keras\backend\tensorflow_backend.py in concatenate(tensors, axis) 1220 return tf.concat_v2([to_dense(x) for x in tensors], axis) 1221 except AttributeError: > 1222 return tf.concat(axis, [to_dense(x) for x in tensors]) 1223 1224 C:\Python35\lib\sitepackages\tensorflow\python\ops\array_ops.py in concat(values, axis, name) 1041 ops.convert_to_tensor(axis, 1042 name="concat_dim", > 1043 dtype=dtypes.int32).get_shape( 1044 ).assert_is_compatible_with(tensor_shape.scalar()) 1045 return identity(values[0], name=scope) C:\Python35\lib\sitepackages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, preferred_dtype) 674 name=name, 675 preferred_dtype=preferred_dtype, > 676 as_ref=False) 677 678 C:\Python35\lib\sitepackages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype) 739 740 if ret is None: > 741 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 742 743 if ret is NotImplemented: C:\Python35\lib\sitepackages\tensorflow\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref) 111 as_ref=False): 112 _ = as_ref > 113 return constant(v, dtype=dtype, name=name) 114 115 C:\Python35\lib\sitepackages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name, verify_shape) 100 tensor_value = attr_value_pb2.AttrValue() 101 tensor_value.tensor.CopyFrom( > 102 tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 103 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype) 104 const_tensor = g.create_op( C:\Python35\lib\sitepackages\tensorflow\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape) 372 nparray = np.empty(shape, dtype=np_dt) 373 else: > 374 _AssertCompatible(values, dtype) 375 nparray = np.array(values, dtype=np_dt) 376 # check to them. C:\Python35\lib\sitepackages\tensorflow\python\framework\tensor_util.py in _AssertCompatible(values, dtype) 300 else: 301 raise TypeError("Expected %s, got %s of type '%s' instead." % > 302 (dtype.name, repr(mismatch), type(mismatch).__name__)) 303 304 TypeError: Expected int32, got "<"tf.Variable 'lstm_3_W_i:0' shape=(1, 1) dtype=float32_ref">" of type 'Variable' instead.
Any help resolving these errors, and understanding about how input_shape and output_dim work would be appreciated!
Eventually I want to start to use things like monthly marketing budget/metrics and sales team metrics as external regressors for multivariate forecasting but one step at a time. Thank you for your time and input!

Keras: Time CNN+LSTM for video recognition
I am trying to implement the Model shown in the above picture that basically consists of timedistributed CNNs followed by a sequence of LSTMs using Keras with TF. I have divided two types of class, and extract the frames from each video captured. The frames extract is variable, do not fix.
However, I am having a problem trying to figure out How can I load my image frames for each video in each class to become
x_train
,x_test
,y_train
,y_test
.model = Sequential() model.add( TimeDistributed( Conv2D(64, (3, 3), activation='relu'), input_shape=(data.num_frames, data.width, data.height, 1) ) )
I don't know how to type in the
data.num_frames
if each video contains n different number of frames extracted.The inputs are small videos just 38 seconds (i.e. a sequence of frames).

Predicting Multiple Future Values with LSTM Forecasting
I have a dataset of MxN and would like to use all the columns to predict a certain number of values into the future, for one of those columns. The code below is what I have already, but when you run it, it returns incorrectly scaled results (I think) and the predictions are terrible, no matter what I change or configure.
Could anyone please provide any hints or tips to improve the code, fix the code or change any of my settings?
By the time the data gets to the Neural Network, my train_X dataset has shape (320, 3, 3)
my train_Y dataset has shape (320, 5)
my test_X dataset has shape (36, 3, 3)
my test_Y dataset has shape (36, 5)
with:
num_columns = 3
num_forecasts = 5
time_lags = 3
number_rows_total = 320 + 36 = 356
It predicts one column to num_forecasts into the future. Therefore the results after model.predict(test_X) has shape (36, 5) with the 5 values being the 5 next forecasts for the chosen column.
import numpy as np from pandas import read_csv from sklearn.preprocessing import MinMaxScaler, StandardScaler from keras.models import Sequential from keras.layers import LSTM, Dense, Dropout, Input from pandas import DataFrame from numpy import concatenate import pandas as pd import matplotlib.pyplot as plt def series_to_supervised(data, _scaler, lags=1, y_column=1, num_forecasts=1): data = DataFrame(data) column_names = list(data.keys()) values = data.values # values = np.diff(np.array(data.values), n=0) values = _scaler.fit_transform(values) data = DataFrame(values) x_cols, y_cols, names = list(), list(), list() x_col_names, y_col_names = list(), list() for n in range(lags): for col in column_names: x_col_names.append("{}(t{})".format(col, n)) for n in range(num_forecasts): y_col_names.append("{}(t+{})".format(column_names[y_column], n)) for j in range(lags): x_cols.append(data.shift(j)) x_data = pd.concat(x_cols, axis=1) x_data.columns = x_col_names original_y_data = data[y_column] for k in range(num_forecasts): y_cols.append(original_y_data.shift(k)) y_data = pd.concat(y_cols, axis=1) y_data.columns = y_col_names x_data = x_data.dropna() y_data = y_data.dropna() x_data = np.array(x_data).reshape(len(x_data), lags, len(column_names)) y_data = np.array(y_data) x_data = x_data[:len(y_data)] y_data = y_data[:len(x_data)] return x_data, y_data def split_data(data_x, data_y, train_split=0.8): data_split_point = int(train_split * len(data_x)) train_X = data_x[:data_split_point] train_Y = data_y[:data_split_point] test_X = data_x[data_split_point:] test_y = data_y[data_split_point:] print(train_X.shape, train_Y.shape, test_X.shape, test_y.shape) return train_X, train_Y, test_X, test_y def create_model(): _model = Sequential() _model.add(LSTM(num_forecasts*2, input_shape=(lags, train_X.shape[2]), return_sequences=True)) _model.add(Dropout(0.2)) _model.add(LSTM(num_forecasts*5, input_shape=(lags, train_X.shape[2]), return_sequences=False, activation='relu')) _model.add(Dropout(0.2)) _model.add(Dense(num_forecasts)) _model.compile(loss="mae", optimizer="adam") return _model np.random.seed(50) lags = 3 num_forecasts = 5 col_to_predict = 2 batch_size = 4 scaler = MinMaxScaler(feature_range=(0, 1)) train_test_split = 0.9 input_dataset = read_csv('../data/full_data_1989_onwards_large_cols.csv', header=0, index_col=0) col = np.array(input_dataset[input_dataset.keys()[col_to_predict]]) X, Y = series_to_supervised(input_dataset, _scaler=scaler, lags=lags, y_column=col_to_predict, num_forecasts=num_forecasts) train_X, train_Y, test_X, test_y = split_data(X, Y, train_split=train_test_split) model = create_model() model.fit(train_X, train_Y, epochs=50, validation_split=0.2, verbose=2, batch_size=batch_size) print("Evaluation result:", model.evaluate(test_X, test_y)) predicted = model.predict(test_X, batch_size=batch_size) # This is rescaling the data with scaling value for the specified column scaler_val = scaler.scale_.data[col_to_predict] print(scaler_val) for arr_index in range(len(predicted)): for el_index in range(len(predicted[arr_index])): predicted[arr_index][el_index] = predicted[arr_index][el_index]/scaler_val print(predicted) train_length = int(len(col)*train_test_split) plt.title(input_dataset.keys()[col_to_predict]) plt.plot(list(range(len(col))), col) # This to show the last value's prediction # plt.plot(list(range(train_length + len(predicted)1, train_length + num_forecasts + len(predicted)1)), predicted[1]) # This to show all values' predictions i = 0 for ar in predicted: plt.plot(list(range(train_length + i, train_length + num_forecasts + i)), ar) i += 1 plt.show()
I would expect the prediction line to start near the original data set but it is printed very far off. I also, no matter how long I train it for, it doesn't predict even in roughly the right direction. Instead, the predictions go down very fast when the data is clearly got an uptrend.
Graph displaying prediction and real values
The little orange line is the last value's prediction of 5 forecasts.
Does anyone have any idea why this prediction is so far off the real values?
Note: I have used the scaler.inverse_transform but that resulted in the same values.

Hierarchical forecasting with userdefined function in R, arima with fourier terms
I'm trying out topdown method for forecasting demand of products in a retail store.
fourier_forecasts = forecast(sales_weekly_hts, h=12,method="tdfp", FUN=function(x) auto.arima(x, xreg=fourier(x, K=12), seasonal=FALSE))
sales_weekly_hts is an hts object containing 2.5 years of weekly sales data.
It gives me the error :
"Error in forecast.Arima(models, h = h) : No regressors provided"
I'm guessing that error is because its not able to obtain the fourier terms for out of sample forecast but I don't get how to resolve this. Is it not able to know how many periods to forecast into the future?
Minimum reproducible example:
library(dplyr) library(hts) # creating a time series matrix containing 4 series and 133 weeks random data min_rep_eg = matrix(data = rnorm(n = 133*4 ,mean = 2), nrow = 133, ncol = 4) %>% ts(frequency = 365.25/7) # giving names to the 5 time series. These names are used to create the hierarchy. colnames(min_rep_eg) = c("10011001","10011003","10031021","10031031") # creating the hts. min_rep_eg_hts = hts(min_rep_eg, characters = c(4, 4)) min_rep_eg_hts_fc = forecast(min_rep_eg_hts, h=2,method="tdfp", FUN=function(x) auto.arima(x, xreg=fourier(x, K=12), seasonal=FALSE))

Differences between ts vs xts
I have a couple of questions: What is the difference between a ts object vs an xts or zoo one? can I do the same as you do with a ts but with an xts one?