我实现了一个名为“ EmbeddingSimilarity”的自定义层。当我尝试使用它时,这导致AttributeError:'NoneType'对象没有属性'_inbound_nodes'。
user = CustomUser.objects.get(id=1)
user_a_references = Relationship.objects.filter(user_a=user)
user_b_references = Relationship.objects.filter(user_b=user)
all_relation_ships = user_a_reference.union(user_b_references)
一个简单的例子:
class EmbeddingSimilarity(Layer):
"""Calculate similarity between features and token embeddings with bias term."""
def __init__(self,
initializer='zeros',
regularizer=None,
constraint=None,
**kwargs):
"""Initialize the layer.
:param output_dim: Same as embedding output dimension.
:param initializer: Initializer for bias.
:param regularizer: Regularizer for bias.
:param constraint: Constraint for bias.
:param kwargs: Arguments for parent class.
"""
super(EmbeddingSimilarity, self).__init__(**kwargs)
self.supports_masking = True
self.initializer = initializers.get(initializer)
self.regularizer = regularizers.get(regularizer)
self.constraint = constraints.get(constraint)
self.bias = None
def get_config(self):
config = {
'initializer': initializers.serialize(self.initializer),
'regularizer': regularizers.serialize(self.regularizer),
'constraint': constraints.serialize(self.constraint),
}
base_config = super(EmbeddingSimilarity, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def build(self, input_shape):
self.bias = self.add_weight(
shape=(int(input_shape[1][0]),),
initializer=self.initializer,
regularizer=self.regularizer,
constraint=self.constraint,
name='bias',
)
super(EmbeddingSimilarity, self).build(input_shape)
def compute_output_shape(self, input_shape):
return input_shape[0][:2] + (input_shape[1][0],)
def compute_mask(self, inputs, mask=None):
return mask[0]
def call(self, inputs, mask=None, **kwargs):
inputs, embeddings = inputs
outputs = K.bias_add(K.dot(inputs, K.transpose(embeddings)), self.bias)
return activations.softmax(outputs)
错误信息:
import keras.backend as K
from keras import Input, Model, losses
from keras.layers import Embedding
from keras.optimizers import SGD
import EmbeddingSimilarity
if __name__ == "__main__":
input = Input(shape=(256,))
word_Embedding = Embedding(1024, 256, input_length=256,
mask_zero=False, trainable=True,
name="Embedding-word")
input_embedding = word_Embedding(input)
embed_weights = K.identity(word_Embedding.embeddings)
mlm_pred_layer = EmbeddingSimilarity(name='MLM-Sim')([input_embedding, embed_weights])
model = Model(inputs=input, outputs=mlm_pred_layer)
model.compile(optimizer=SGD(), loss=losses.sparse_categorical_crossentropy)