'AttributeError: 'Node' object has no attribute 'input_masks'
I created a network, but got the error: AttributeError: in user code:
C:\Users\LocalAdmin\.conda\envs\newenvt\lib\site-packages\keras_contrib\metrics\crf_accuracies.py:23 crf_viterbi_accuracy *
mask = crf._inbound_nodes[idx].input_masks[0]
AttributeError: 'Node' object has no attribute 'input_masks'
My Code:
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from tqdm import tqdm
from tensorflow.keras import Input,Model
from tensorflow.keras.layers import Dense, TimeDistributed, SpatialDropout1D, Bidirectional, LSTM, Lambda
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.compat.v1.keras import backend as K
from keras_contrib.layers import CRF
np.random.seed(1234567890)
# Neural Network
input_text = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(max_len, 961), trainable=False)(input_text)
x = Bidirectional(LSTM(units=496, return_sequences=True, recurrent_dropout=0.1, dropout=0.1))(embedding)
model = TimeDistributed(Dense(50, activation="relu"))(x)
crf = CRF(n_tags, sparse_target=True) # CRF layer, n_tags+1(PAD)
out = crf(model) # output
model = Model(input_text, out)
model.compile(optimizer="rmsprop", loss=crf.loss_function, metrics=[crf.accuracy])
model.summary()
history = model.fit(np.array(x_train), y_train, batch_size=batch_size, epochs=1, verbose=1)
Does anyone know how to fix it? My python version ist 3.8 and my tensorflow version is 2.4.0. Thank you in advance for your help!
Solution 1:[1]
keras-contrib is deprecated and unmaintained, and incompatible with TF2:
Keras-contrib is deprecated. Use TensorFlow Addons.
Use TensorFlow Addons (tfa
) instead, in your case tfa.layers.CRF
Solution 2:[2]
I have encountered the same problem as you. I still have problems after replacing the new version of CRF. got a error: TypeError: Value passed to parameter 'x' has DataType bool not in list of allowed values: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, float16, uint32, uint64
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Lescurel |
Solution 2 | cgp1997 |