tensorflow - 使用 AWS Sagemaker 时,Keras Conv2D 层具有不同的输出

标签 tensorflow machine-learning keras artificial-intelligence amazon-sagemaker

我一直在尝试在 AWS Sagemaker 上训练模型,因为我发现我的计算机不再强大到足以在合理的时间内训练我的模型。但是,当我尝试加载模型时(从计算机复制粘贴代码后),我遇到了意外错误。

经过一番修改后,我发现第一个 Conv2D 层的输出形状与我的计算机上的不同。

Sagemaker 输出尺寸:

(无、128、498、3)

预期输出尺寸:

(无、498、498、3)

我的代码如下:

import tensorflow as tf
from tensorflow import keras


model = keras.models.Sequential()

model.add(keras.Input(shape = (500,500,3)))
model.add(keras.layers.Conv2D(filters=128, kernel_size = (3,3), activation='relu'))

model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.0001),
                loss=keras.losses.CategoricalCrossentropy(from_logits=True),
                metrics=['accuracy'])

model.summary()

我该如何解决这个问题?

最佳答案

我来这里是因为我遇到了同样的问题。我找到了解决方案,但我仍然对此感到困惑。我只想提一下,我在本地和 sagemaker (2.10) 上使用相同的 tensorflow 版本。并且两者的代码完全相同。

如果你去https://keras.io/api/layers/convolution_layers/convolution2d/ 它指出:

“输出形状

形状为batch_shape + (filters, new_rows, new_cols)的4+D张量if data_format='channels_first'或形状为batch_shape + (new_rows, new_cols,filters)的4+D张量<强>如果 data_format='channels_last'。行和列值可能由于填充而发生变化。”

所以我强制 Sagemaker 的版本为`data_format='channels_last' 现在本地版本和AWS版本是一致的。

`

关于tensorflow - 使用 AWS Sagemaker 时,Keras Conv2D 层具有不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74595454/

相关文章:

tensorflow - Keras 类型错误 : can't pickle _thread. RLock 对象

machine-learning - LSTM 对子序列的预测失败

python - 尝试在Keras中加载顺序模型时出现“KeyError:0”

c++ - Tensorflow 张量的子张量 (C++)

python - 将队列拆分为训练/测试集

machine-learning - 如何加载检查点文件并使用略有不同的图形结构继续训练

r - 错误: could not find function "includePackage"

python - scikit-learn 中处理 nan/null 的分类器

python - Keras 极高的损失

python - LSTM网络的图解说明