Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

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

Open
Yang-never opened this issue Jul 14, 2021 · 3 comments
Open

Comments

@Yang-never
Copy link

Yang-never commented Jul 14, 2021

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
graph_function, _ = 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
Copy link

RanaBan commented Jul 18, 2021

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

@Starrylun
Copy link

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

@RanaBan
Copy link

RanaBan commented Oct 1, 2021

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)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants