Mee*_*Mee 8 python compilation transformer-model keras tensorflow
我正在尝试遵循此代码,但在另一个数据集上: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'].
Run Code Online (Sandbox Code Playgroud)
这是模型:
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)
Run Code Online (Sandbox Code Playgroud)
并创建模型,编译它,并按如下方式拟合:
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)
Run Code Online (Sandbox Code Playgroud)
编辑:基于@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
Run Code Online (Sandbox Code Playgroud)
但是,我仍然遇到相同的错误
你得到错误的原因是因为self.call只需要两个变量 aninput和一个training标志。如果您有多个输入变量,它们将作为元组传递。因此,您可以拥有类似于以下内容的函数定义:
def call(self, input, training):
inp, tar, enc_padding_mask,look_ahead_mask, dec_padding_mask = input
...
Run Code Online (Sandbox Code Playgroud)
这可能是文档中的一个错误,正如我们在 github 上看到的那样,如果我们想要继承,那么除了方法的定义之外,tf.keras.Model我们只能有一个参数,这就是错误消息抱怨的内容:traininginputscall()
如果您对 进行子类化
Model,则可以选择training在 中包含一个参数(布尔值)call,您可以使用它来指定训练和推理中的不同行为
"""
If you subclass `Model`, you can optionally have
a `training` argument (boolean) in `call`, which you can use to specify
a different behavior in training and inference:
```python
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
self.dropout = tf.keras.layers.Dropout(0.5)
def call(self, inputs, training=False):
x = self.dense1(inputs)
if training:
x = self.dropout(x, training=training)
return self.dense2(x)
model = MyModel()
```
"""
Run Code Online (Sandbox Code Playgroud)
我建议你在 github 上创建一个问题来引用它。
| 归档时间: |
|
| 查看次数: |
2408 次 |
| 最近记录: |