【问题标题】:Same NN architecture giving different accuracies in tensor flow and keras相同的 NN 架构在 tensorflow 和 keras 中提供不同的精度
【发布时间】:2018-11-29 04:24:01
【问题描述】:

使用 [4, 4] 隐藏层在 iris 数据集上训练并在 tensorflow 和 keras 中分别创建的神经网络给出不同的结果。

虽然 tensorflow 模型在测试中给出了 96.6% 的准确率,但 keras 模型的准确率只有 50% 左右。在这两种情况下,学习率、优化器、小批量大小等各种超参数都是相同的。

Keras 模型

model = Sequential()

model.add(Dense(units = 4, activation = 'relu', input_dim = 4))
model.add(Dropout(0.25))
model.add(Dense(units = 4, activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(units = 3, activation = 'softmax'))

adam = Adam(epsilon = 10**(-6), lr = 0.01)
model.compile(optimizer = 'adagrad', loss = 'categorical_crossentropy', metrics = ['accuracy'])

one_hot_labels = keras.utils.to_categorical(y_train, num_classes = 3)

model.fit(X_train, one_hot_labels, epochs = 50, batch_size = 40)

张量流模型

feature_columns = [tf.feature_column.numeric_column(key = name,
                                                   shape = (1),
                                                   dtype = tf.float32) for name in list(X_train.columns)]

classifier = tf.estimator.DNNClassifier(hidden_units = [4, 4],
                                       feature_columns = feature_columns,
                                       n_classes = 3,
                                       dropout = 0.25,
                                       model_dir = './DNN_model')

train_input_fn = tf.estimator.inputs.pandas_input_fn(x = X_train,
                                              y = y_train,
                                              batch_size = 40,
                                              num_epochs = 50,
                                              shuffle = False)

classifier.train(input_fn = train_input_fn, steps = None)

对于 keras 模型,我确实尝试过改变学习率、增加 epoch 的数量、使用不同的优化器等。因此,准确性仍然很差。显然,这两个模型都在做不同的事情,但从表面上看,它们在所有关键方面似乎都与我相同。

感谢任何帮助。

【问题讨论】:

  • 使用模型类API时是否也会出现这种情况?

标签: python tensorflow keras deep-learning


【解决方案1】:

它们具有相同的架构,仅此而已。

性能差异来自以下一个或多个因素:

  • 你有辍学。因此,您的网络在每次开始时表现不同(检查 Dropout 的工作原理);

  • 权重初始化,您在 Keras 和 TensorFlow 中使用哪种方法?

  • 检查优化器的所有参数。

【讨论】:

  • - Dropout:我都跑了好几次。我每次都得到相似的结果。 - 权重初始化:我没有更改默认值。但我认为假设它使用 xavier 初始化程序是公平的。 - 优化器:我尝试更改优化器的学习率和 epsilon 值。事实上,对于 keras 模型,我尝试了几个优化器和学习率。它的性能仍然很差。
  • 你确定keras和TF的默认值是一样的吗?
猜你喜欢
  • 1970-01-01
  • 2018-10-18
  • 2019-06-09
  • 1970-01-01
  • 2018-09-28
  • 2019-11-10
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
相关资源
最近更新 更多