一、建Embeddin层优化
1、报错信息:Unrecognized keyword arguments passed to Embedding:{'batch_input_shape': [64, None]}
2、模型配置可优化
在创建Embedding层时,使用了batch_size作为参数。这在某些情况下可以加速初始化过程,但不是必须的,且可能会导致在不同batch大小上重复使用模型时出现问题。通常,我们建议不指定batch_size,让Keras在运行时根据实际情况决定。
3、解决方案:移除batch_size参数
# 创建模型 model = tf.keras.Sequential([ tf.keras.layers.Embedding(vocab_size, embedding_dim), tf.keras.layers.GRU(rnn_units, return_sequences=True, stateful=False, recurrent_initializer='glorot_uniform'), tf.keras.layers.Dense(vocab_size, activation=None) # 可根据任务调整激活函数,默认为None ])
二、错误信息
When using save_weights_only=True in ModelCheckpoint, the filepath provided must end in .weights.h5 (Keras weights format). Received:
解决方案:
根据错误提示,当在 tf.keras.callbacks.ModelCheckpoint 中设置 save_weights_only=True 时,保存路径(filepath)应以 .weights.h5 结尾。为了修正这个问题,请修改检查点文件名格式。以下是修复后的代码:
# %% # 进行训练 # 检查点保存至的目录 checkpoint_dir = '/training_checkpoints' # 检查点的文件名格式,确保以 .weights.h5 结尾 checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch:02d}.weights.h5") # 训练的回调 checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_prefix, save_weights_only=True) # 进行训练 history = model.fit(dataset, epochs=20, callbacks=[checkpoint_callback])