我无法理解为什么我得到这个错误。代码工作正常,不保存模型,但当我运行model.save()
时,它抛出异常。<<strong>错误/strong>
AttributeError:Traceback(最近一次调用最后一次)在()1 model.summary ()2 model.save(/内容/数据/chatbot_model, include_optimizer = False)
get_config(自我)
9 config = super(PositionalEncoding, self).get_config()
10 config.update({'position': self.position,
12 'd_model': self.d_model,
13
AttributeError: 'PositionalEncoding'对象没有属性'position'
这是位置编码类的类代码
def __init__(self, position, d_model):
super(PositionalEncoding, self).__init__()
self.pos_encoding = self.positional_encoding(position, d_model)
def get_config(self):
config = super(PositionalEncoding, self).get_config()
config.update({
'position': self.position,
'd_model': self.d_model,
})
return config
def get_angles(self, position, i, d_model):
angles = 1 / tf.pow(10000, (2 * (i // 2)) / tf.cast(d_model, tf.float32))
return position * angles
def positional_encoding(self, position, d_model):
angle_rads = self.get_angles(
position=tf.range(position, dtype=tf.float32)[:, tf.newaxis],
i=tf.range(d_model, dtype=tf.float32)[tf.newaxis, :],
d_model=d_model)
# apply sin to even index in the array
sines = tf.math.sin(angle_rads[:, 0::2])
# apply cos to odd index in the array
cosines = tf.math.cos(angle_rads[:, 1::2])
pos_encoding = tf.concat([sines, cosines], axis=-1)
pos_encoding = pos_encoding[tf.newaxis, ...]
return tf.cast(pos_encoding, tf.float32)
def call(self, inputs):
return inputs + self.pos_encoding[:, :tf.shape(inputs)[1], :]```
position
和d_model
在__init__
方法中没有定义为属性,在任何方法中都没有更新。