gpt4 book ai didi

python - 传递给 `fit` 的模型只能将 `training` 和 `call` 中的第一个参数作为位置参数,发现

转载 作者:行者123 更新时间:2023-12-03 14:23:40 25 4
gpt4 key购买 nike

我正在尝试遵循此代码但在另一个数据集上:https://www.tensorflow.org/tutorials/text/transformer#encoder_layer
我需要编译和拟合模型。但是,我在运行时收到此错误;我不知道这是什么意思:

 Models passed to `fit` can only have `training` and the first argument in `call` as positional arguments, found: ['tar', 'enc_padding_mask', 'look_ahead_mask', 'dec_padding_mask'].

这是模型:
class Transformer(tf.keras.Model):
def __init__(self, num_layers, d_model, num_heads, dff, input_vocab_size,
target_vocab_size, pe_input, pe_target, rate=0.1,**kwargs,):
super(Transformer, self).__init__(**kwargs)

self.encoder = Encoder(num_layers, d_model, num_heads, dff,
input_vocab_size, pe_input, rate)

self.decoder = Decoder(num_layers, d_model, num_heads, dff,
target_vocab_size, pe_target, rate)

self.final_layer = tf.keras.layers.Dense(target_vocab_size)
def get_config(self):

config = super().get_config().copy()
config.update({
'dff':self.dff,
'input_vocab_size':self.input_vocab_size,
'target_vocab_size':self.target_vocab_size,
'pe_input':self.pe_input,
'pe_target':self.pe_target,
#'vocab_size': self.vocab_size,
'num_layers': self.num_layers,
#'units': self.units,
'd_model': self.d_model,
'num_heads': self.num_heads,
'rate': self.rate,
})
return config

def call(self, inp, tar, training, enc_padding_mask,
look_ahead_mask, dec_padding_mask):

enc_output = self.encoder(inp, training, enc_padding_mask) # (batch_size, inp_seq_len, d_model)

# dec_output.shape == (batch_size, tar_seq_len, d_model)
dec_output, attention_weights = self.decoder(
tar, enc_output, training, look_ahead_mask, dec_padding_mask)

final_output = self.final_layer(dec_output) # (batch_size, tar_seq_len, target_vocab_size)
# return final_output, attention_weights


return tf.keras.Model(inputs=[inputs, dec_inputs], outputs=outputs, name=name)

并创建模型,编译它,并按如下方式拟合:
transformer = Transformer(num_layers, d_model, num_heads, dff,
input_vocab_size, target_vocab_size,
pe_input=input_vocab_size,
pe_target=target_vocab_size,
rate=dropout_rate)

transformer.compile(optimizer=optimizer, loss=loss_function, metrics=[accuracy])

transformer.fit(dataset, epochs=EPOCHS)

编辑:基于@Geeocode 将转换器类中的 def 函数更新为:
def call(self, inp, tar, enc_padding_mask,look_ahead_mask, dec_padding_mask, training=False,):

enc_output = self.encoder(inp, training, enc_padding_mask) # (batch_size, inp_seq_len, d_model)

# dec_output.shape == (batch_size, tar_seq_len, d_model)
dec_output, attention_weights = self.decoder(
tar, enc_output, training, look_ahead_mask, dec_padding_mask)

final_output = self.final_layer(dec_output) # (batch_size, tar_seq_len, target_vocab_size)
return final_output, attention_weights

但是,我仍然遇到相同的错误

最佳答案

你得到错误的原因是因为 self.call只需要两个变量 input和一个 training旗帜。如果您有多个输入变量,它们将作为元组传递。因此,您可以拥有类似于以下内容的函数定义:

def call(self, input, training):
inp, tar, enc_padding_mask,look_ahead_mask, dec_padding_mask = input
...

关于python - 传递给 `fit` 的模型只能将 `training` 和 `call` 中的第一个参数作为位置参数,发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59910494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com