Google Colab 和 IBM Watson Studio 中的代码相同但结果不同

Same code but different outcomes in Google Colab and IBM Watson Studio

我正在使用 tf.keras 数据集 API 中直接可用的 Fashion MNIST 数据集训练一个简单的神经网络。我在 Colab 和 IBM Watson Studio 中训练了模型。但当我发现在 Colab 中,该模型接受了 1875 次梯度更新训练,而在 Watson Studio 中,它接受了 60000 次梯度更新训练时,我感到很惊讶。为什么在这两种环境中没有梯度更新不同?它们在 Colab 和 Watson Studio 中不应该相同吗?

此处提供代码:

import tensorflow as tf
print(tf.__version__)

mnist = tf.keras.datasets.fashion_mnist

(training_images, training_labels), (test_images, test_labels) = mnist.load_data()

import numpy as np
np.set_printoptions(linewidth=200)
import matplotlib.pyplot as plt
plt.imshow(training_images[0])
print(training_labels[0])
print(training_images[0])

training_images  = training_images / 255.0
test_images = test_images / 255.0

model = tf.keras.models.Sequential([tf.keras.layers.Flatten(), 
                                    tf.keras.layers.Dense(128, activation=tf.nn.relu), 
                                    tf.keras.layers.Dense(10, activation=tf.nn.softmax)])

model.compile(optimizer = tf.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(training_images, training_labels, epochs=5)

这里是Colab and Watson Studio的链接。

编辑:经过更多研究,我发现了 'batch-size'。由于 'batch-size' 未包含在代码中,因此使用了默认值 'batch-size' (32),因此在 Colab 中没有使用 1875 (60000/32)。但是我还是不明白为什么Watson Studio里面有60000个更新。我将感谢你对此的帮助。

是的,两者的区别在于平台不同,计算能力不同,耗时不同。 elements/samples 你在两者中训练的次数不同,这就是为什么基于云计算的效率和计算能力,他们给出不同的结果。

此外,watson 中的代码仍然是 运行ning,这就是为什么您在 watson 中只有两个准确度输出。让代码 运行 完整,你将得到总共 5 个训练输出。 我想你明白了。

从TensorFlow 2.2.0版本开始,如果不指定,batch_size默认为32。

现在在Colab中,使用的是TensorFlow 2.4.0版本,由于代码中没有定义batch size,所以使用默认值,因此迭代次数为60000/32 = 1875次。

另一方面,截至 2020 年 12 月 30 日,IBM Watson Studio 中的 TensorFlow 版本为 2.1.0,没有任何默认批量大小。所以,在那种情况下,数据集没有被分批考虑,因此有 60000 次迭代。