LSTM don't work with RuntimeError: Attempting to capture an EagerTensor without building a function, but GRU work #42515
Labels
comp:keras
Keras related issues
stale
This label marks the issue/pr stale - to be closed automatically if no activity
stat:awaiting response
Status - Awaiting response from author
TF 2.1
for tracking issues in 2.1 release
type:support
Support issues
System information*
-Keras 2.3.1
Describe the current behavior
LSTM occur error:"raise RuntimeError("Attempting to capture an EagerTensor without "
RuntimeError: Attempting to capture an EagerTensor without building a function."
However, If I change LSTM to GRU(as shown in comment code), it work!
Standalone code to reproduce the issue
from Logger import log
from keras.models import Model
from keras.layers import LSTM, Dense, Conv1D, Flatten, Reshape,Lambda, dot, Activation, concatenate, Bidirectional, GRU
from keras.utils import print_summary, plot_model
import numpy as np
import keras.backend as K
import os
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
F = 16
K = 4
H = 512
#window_length = L = 1536
reshape = Reshape((window_length, 1),
)(input_tensor)
cnn1 = Conv1D(F, K, activation="linear", strides=1)(reshape)
cnn2 = Conv1D(F, K, activation="linear", strides=1)(cnn1)
# Bi-directional LSTM
# lstm1 = Bidirectional(GRU(H, activation='tanh',return_sequences=True), merge_mode='concat')(cnn2)
lstm1 = Bidirectional(LSTM(H, return_sequences=True))(cnn2)
attention1 = attention_3d_block(lstm1)
dense1 = Dense(window_lengthF, use_bias=False, activation='relu')(attention1)
dense2 = Dense(window_lengthF, use_bias=False, activation='relu')(dense1)
r = Reshape((window_length*F, 1))(dense2)
cnn3 = Conv1D(F, K, activation="linear", strides=1)(r)
cnn4 = Conv1D(1, K, activation="linear", strides=1)(cnn3)
# x = Reshape((window_length,), input_shape=(window_length, 1))(cnn4)
flat = Flatten(name='flatten')(cnn4)
d_out = Dense(window_length)(flat)
model = Model(inputs=input_tensor, outputs=d_out)
Other info / logs
2020-08-20 22:15:03,052 [INFO ] Parameters:
2020-08-20 22:15:03,053 [INFO ] Machine name: Ziyues-MacBook-Pro.local
Using TensorFlow backend.
2020-08-20 22:15:05,618 [WARNI] From /Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/compat/v2_compat.py:88: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
2020-08-20 22:15:05,669 [DEBUG] CACHEDIR=/Users/Ziyue/.matplotlib
2020-08-20 22:15:05,673 [DEBUG] Using fontManager instance from /Users/Ziyue/.matplotlib/fontlist-v310.json
2020-08-20 22:15:05,799 [DEBUG] Loaded backend module://backend_interagg version unknown.
2020-08-20 22:15:05,802 [INFO ] Arguments:
2020-08-20 22:15:05,802 [INFO ] Namespace(appliance_name='washingmachine', batchsize=128, cnn='kettle', crop_dataset=None, datadir='./dataset_management/redd/', dense_layers=1, gpus=-1, n_epoch=2, pretrainedmodel_dir='./pretrained_model', ram=500000, save_dir='./trained_model_s2swA', save_model=-1, transfer_cnn=False, transfer_model=False)
2020-08-20 22:15:05.803552: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-08-20 22:15:05.817249: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7ff2a6b92740 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-20 22:15:05.817263: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2020-08-20 22:15:05,817 [INFO ] Training dataset: ./dataset_management/redd/washingmachine/washingmachine_training_.csv
2020-08-20 22:15:05,817 [INFO ] washingmachine_validation_.csv
2020-08-20 22:15:05,817 [INFO ] Validation dataset: ./dataset_management/redd/washingmachine/washingmachine_validation_.csv
2020-08-20 22:15:05,838 [WARNI] From /Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1635: calling BaseResourceVariable.init (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Traceback (most recent call last):
File "/Users/Ziyue/Desktop/BitcnNILM-master/s2swA_train.py", line 189, in
pretrainedmodel_dir=args.pretrainedmodel_dir)
File "/Users/Ziyue/Desktop/BitcnNILM-master/s2swA_Model.py", line 70, in get_model
lstm1 = Bidirectional(LSTM(512, return_sequences=True, stateful=True))(cnn2)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/layers/wrappers.py", line 437, in call
return super(Bidirectional, self).call(inputs, **kwargs)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper
return func(*args, **kwargs)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/engine/base_layer.py", line 463, in call
self.build(unpack_singleton(input_shapes))
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/layers/wrappers.py", line 581, in build
self.forward_layer.build(input_shape)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/layers/recurrent.py", line 502, in build
self.cell.build(step_input_shape)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/layers/recurrent.py", line 1942, in build
constraint=self.bias_constraint)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/engine/base_layer.py", line 282, in add_weight
constraint=constraint)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 620, in variable
value, dtype=dtype, name=name, constraint=constraint)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py", line 814, in variable
constraint=constraint)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/variables.py", line 260, in call
return cls._variable_v2_call(*args, **kwargs)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/variables.py", line 254, in _variable_v2_call
shape=shape)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/variables.py", line 235, in
previous_getter = lambda **kws: default_variable_creator_v2(None, **kws)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/variable_scope.py", line 2645, in default_variable_creator_v2
shape=shape)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/variables.py", line 262, in call
return super(VariableMetaclass, cls).call(*args, **kwargs)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py", line 1411, in init
distribute_strategy=distribute_strategy)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py", line 1543, in _init_from_args
name="initial_value", dtype=dtype)
File "/Users/Ziyue/opt/anaconda3/envs/nilmtk-env/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1280, in convert_to_tensor
raise RuntimeError("Attempting to capture an EagerTensor without "
RuntimeError: Attempting to capture an EagerTensor without building a function.
The text was updated successfully, but these errors were encountered: