[英]Same NN architecture giving different accuracies in tensor flow and keras
A neural network trained on iris dataset using [4, 4] hidden layers and created separately in tensorflow and keras gives different results. 在虹膜数据集上使用[4,4]隐藏层训练并在tensorflow和keras中分别创建的神经网络给出了不同的结果。
While the tensorflow model gives 96.6 % accuracy on test, keras model gives only around 50%. 尽管tensorflow模型在测试中提供了96.6%的准确性,但keras模型仅提供了约50%的准确性。 The various hyper parameters like learning rate, optimiser, mini batch size, etc were the same in both cases. 两种情况下的各种超参数(如学习率,优化器,最小批次大小等)都相同。
Keras model 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)
Tensorflow model Tensorflow模型
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)
For the keras model, I did try changing the learning rate, increasing the number of epochs, using different optimisers, etc. As such, the accuracy remained poor. 对于keras模型,我确实尝试过更改学习率,增加时期数,使用不同的优化器等。因此,准确性仍然很差。 Clearly, both the models are doing different things, but on the surface, they seem identical to me for all the key aspects. 显然,两个模型都在做不同的事情,但是从表面上看,对于所有关键方面,它们对我来说似乎都是相同的。
Any help is appreciated. 任何帮助表示赞赏。
they have the same architecture , and that's all. 它们具有相同的架构 ,仅此而已。
The difference in performance is coming from one or more of these factors: 性能上的差异来自以下一个或多个因素:
You have Dropout. 你有辍学。 Therefore your networks in every start behaving differently (check how the Dropout works); 因此,您的网络在每次启动时的行为都不同(请检查Dropout的工作方式);
Weight initializations, which method you're using in Keras and TensorFlow? 权重初始化,您在Keras和TensorFlow中使用哪种方法?
Check all parameters of the optimizer. 检查优化器的所有参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.