keras-team / keras-contrib

Keras community contributions
MIT License
1.59k stars 654 forks source link

AttributeError: 'Tensor' object has no attribute '_keras_history' #561

Open Yang-never opened 2 years ago

Yang-never commented 2 years ago

Hi, I am using CRF from keras_contrib,but I get this error " AttributeError: 'Tensor' object has no attribute '_keras_history'", when model.fit() is called. Any help would be highly appreciated This is my import import numpy as np import os import tensorflow as tf

from scipy.io import loadmat

from tensorflow import keras from tensorflow.keras import backend as K from tensorflow.keras.layers import Dense,Dropout,TimeDistributed,Bidirectional,LSTM,Input from tensorflow.keras.utils import to_categorical from tensorflow.keras.models import Sequential from tensorflow.keras.callbacks import ModelCheckpoint from keras_contrib.layers.crf import CRF from keras_contrib.losses import crf_loss from keras_contrib.metrics import crf_viterbi_accuracy from tensorflow.keras import Model This my partial code model = Sequential() model.add(Dense(4,activation='softmax')) model.add(CRF(4)) model.compile(optimizer='RMsprop', loss=crf_loss, metrics=[crf_viterbi_accuracy])

model.fit(train_data, train_label, batch_size=64, epochs=10,
                  validation_split=0.2)

model.summary()

Traceback (most recent call last): File "D:/CRF+LSTM/test1.py", line 426, in Linear_crf(train_data,train_label,test_data,test_label) File "D:/CRF+LSTM/test1.py", line 420, in Linear_crf validation_split=0.2, callbacks=[cp_callback]) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1183, in fit tmp_logs = self.train_function(iterator) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in call result = self._call(*args, kwds) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call self._initialize(args, kwds, add_initializers_to=initializers) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\def_function.py", line 764, in _initialize *args, *kwds)) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\function.py", line 3050, in _get_concrete_function_internal_garbage_collected graphfunction, = self._maybe_define_function(args, kwargs) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\function.py", line 3289, in _create_graph_function capture_by_value=self._capture_by_value), File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\framework\func_graph.py", line 999, in func_graph_from_py_func func_outputs = python_func(func_args, func_kwargs) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn out = weak_wrapped_fn().wrapped(*args, **kwds) File "D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper raise e.ag_error_metadata.to_exception(e) AttributeError: in user code:

D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\keras\engine\training.py:855 train_function  *
    return step_function(self, iterator)
D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\keras_contrib-2.0.8-py3.7.egg\keras_contrib\losses\crf_losses.py:54 crf_loss  *
    crf, idx = y_pred._keras_history[:2]
D:\machine_learning\Anaconda_3\envs\cc\lib\site-packages\tensorflow\python\framework\ops.py:401 __getattr__
    self.__getattribute__(name)

AttributeError: 'Tensor' object has no attribute '_keras_history'

tensorflow-gpu==2.2.0 kears==2.3.1 keras_contrib=2.0.8 CUDA=10.1 cudnn=7.6.5 if I use from kears.layers import ,it not can use my GPU accelerate my code, use my CPU,but i use from tensorflow.keras.layers import , it can use GPU accelerate, but have a error: The added layer must be an instance of class Layer. Found: <keras_contrib.layers.crf.CRF object at 0x12bfa4550>

RanaBan commented 2 years ago

Had the same error " AttributeError: 'Tensor' object has no attribute '_keras_history'".

Starrylun commented 2 years ago

Hi, my problem is same as you, could you tell me how to slove the problem? please.

RanaBan commented 2 years ago

As an alternative, try this

pip install tf2crf
from tf2crf import CRF, ModelWithCRFLoss

## use the below lines as a tf layer

layer = tfa.layers.CRF(units=n_tags)
decoded_sequence, potentials, sequence_length, chain_kernel = layer(intermediate)