Neural Network LSTM input shape from dataframe

Below is an example that sets up time series data to train an LSTM. The model output is nonsense as I only set it up to demonstrate how to build the model. import pandas as pd import numpy as np # Get some time series data df = pd.read_csv(“https://raw.githubusercontent.com/plotly/datasets/master/timeseries.csv”) df.head() Time series dataframe: Date A … Read more

TensorFlow: Remember LSTM state for next batch (stateful LSTM)

I found out it was easiest to save the whole state for all layers in a placeholder. init_state = np.zeros((num_layers, 2, batch_size, state_size)) … state_placeholder = tf.placeholder(tf.float32, [num_layers, 2, batch_size, state_size]) Then unpack it and create a tuple of LSTMStateTuples before using the native tensorflow RNN Api. l = tf.unpack(state_placeholder, axis=0) rnn_tuple_state = tuple( [tf.nn.rnn_cell.LSTMStateTuple(l[idx][0], … Read more

When does keras reset an LSTM state?

Cheking with some tests, I got to the following conclusion, which is according to the documentation and to Nassim’s answer: First, there isn’t a single state in a layer, but one state per sample in the batch. There are batch_size parallel states in such a layer. Stateful=False In a stateful=False case, all the states are … Read more

Tensorflow – ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float)

TL;DR Several possible errors, most fixed with x = np.asarray(x).astype(‘float32′). Others may be faulty data preprocessing; ensure everything is properly formatted (categoricals, nans, strings, etc). Below shows what the model expects: [print(i.shape, i.dtype) for i in model.inputs] [print(o.shape, o.dtype) for o in model.outputs] [print(l.name, l.input_shape, l.dtype) for l in model.layers] The problem’s rooted in using … Read more

Understanding Keras LSTMs

As a complement to the accepted answer, this answer shows keras behaviors and how to achieve each picture. General Keras behavior The standard keras internal processing is always a many to many as in the following picture (where I used features=2, pressure and temperature, just as an example): In this image, I increased the number … Read more