Calling a stateful LSTM as a functional model?

2024/9/30 13:24:44

I have a stateful LSTM defined as a Sequential model:

model = Sequential()
model.add(LSTM(..., stateful=True))
...

Later, I use it as a Functional model:

input_1, input_2 = Input(...), Input(...)
output_1 = model(input_1)
output_2 = model(input_2)  # Is the state from input_1 preserved?

Is the state from input_1 preserved when we apply model again on input_2? If yes, how can I reset the model state in between the calls?

Answer

Following Note on using statefulness in RNNs from this link and Keras implementation the answer is yes if:

  1. The batch_size in both models is the same (it's important due to the way Keras computes the inner states).
  2. You would first build and compile both models and then use them - for some reason Keras is resetting the inner states during the build of a layer (you can check it here by looking for reset_states method).

If you want to reset states you could call reset_states method on each recurrent layer you want ot reset states on.

https://en.xdnf.cn/q/71076.html

Related Q&A

How to cluster Gantt bars without overlap?

Using create_gantt I have overlapping start and end dates: import plotly.plotly as py import plotly.figure_factory as ff import plotlydf = [dict(Task="Milestone A", Start=2017-01-01, Finish=2…

Fail to install lxml using pip

This is the command I used to install lxml:sudo pip install lxmlAnd I got the following message in the Cleaning Up stage:Cleaning up... Command /usr/bin/python -c "import setuptools, tokenize;…

Python 3.x list comprehension VS tuple generator

Is there any reason for memory, speed or whatever, that I would want to use:tuple(i for i in range(5000))instead of:[i for i in range(5000)]If I didnt mind the immutability of tuples

Using Sphinx with a distutils-built C extension

I have written a Python module including a submodule written in C: the module itself is called foo and the C part is foo._bar. The structure looks like:src/ foo/__init__.py <- contains the public …

PyYAML error: Could not determine a constructor for the tag !vault

I am trying to read a YAML file that has the tag !vault in it. I get the error:could not determine a constructor for the tag !vaultUpon reading a couple of blogs, I understood that I need to specify so…

Accessing a XAMPP mysql via Python

Im attempting to use mysql after only having worked with sqlite in the past.Ive installed XAMPP on Linux (ubuntu) and have mysql up and running fine (seems like that with phpMyadmin at least). However,…

How to use deep learning models for time-series forecasting?

I have signals recorded from machines (m1, m2, so on) for 28 days. (Note: each signal in each day is 360 length long).machine_num, day1, day2, ..., day28 m1, [12, 10, 5, 6, ...], [78, 85, 32, 12, ...],…

Python - Create Shortcut with arguments

Using win32com.client, Im attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.Traceback (most recent call …

How to replace a range of values with NaN in Pandas data-frame?

I have a huge data-frame. How should I replace a range of values (-200, -100) with NaN?

django 1.10 Exception while resolving variable is_popup in template admin/login.html

I create a new django project with python3.5 and django1.10.0,I keep getting an error in the admin whenever I want access localhost:8000/admin, He`res the error:[DEBUG]- Exception while resolving varia…