【问题标题】:AttributeError: 'PositionalEncoding' object has no attribute 'position'AttributeError:“PositionalEncoding”对象没有属性“位置”
【发布时间】:2021-10-05 17:51:37
【问题描述】:

我无法理解为什么会出现此错误。代码在不保存模型的情况下工作正常,但是当我运行 model.save() 时会引发异常。 错误是

AttributeError:Traceback(最近一次调用最后一次) 在 () 1 模型.summary() 2 model.save('/content/Data/chatbot_model',include_optimizer=False)

在 get_config(self) 中

  9         config = super(PositionalEncoding, self).get_config()
 10         config.update({'position': self.position,
 12             'd_model': self.d_model,
 13 

AttributeError: 'PositionalEncoding' 对象没有属性 'position'

这里是Positional Encoding Class的类代码


 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], :]```

【问题讨论】:

  • 请附上完整的堆栈跟踪,因为它将包含整个调用序列和导致错误的确切行的信息。

标签: python tensorflow machine-learning chatbot transformer


【解决方案1】:

positiond_model 没有在__init__ 方法中定义为属性,也没有在任何方法中更新。

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2012-12-01
    • 2021-04-19
    • 2013-05-03
    • 2012-10-02
    • 2020-11-06
    • 2022-07-08
    • 2022-07-11
    • 2017-02-27
    相关资源
    最近更新 更多