简体   繁体   English

相同的NN架构在张量流和keras中提供不同的精度

[英]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.

相关问题 相同数量的值,进出张量流神经网络? - The same number of values, into & out of Tensor Flow NN? 相同的模型在Keras和Tensorflow中产生一致的不同精度 - Same model produces consistently different accuracies in Keras and Tensorflow 培训和评估的准确性在keras LSTM模型中不同:为什么评估产生的结果与培训期间不同? - Training and evaluating accuracies different in keras LSTM model: why does evaluation not produce same results as during training? 为什么 Keras 的评估生成器和评估报告对相同数据的准确度不同? - Why do Keras's evaluate_generator and evaluate report different accuracies on the same data? tensorflowjs 和 keras 在相同模型和张量上的不同结果 - Different results for tensorflowjs and keras on same model and tensor 即使NN和数据集相同,Keras上的Predict()也会始终给出不同的结果 - Predict() on Keras gives alway different results even if the NN and the dataset is the same 用keras,tensorflow和python编写这个充满异国情调的NN架构 - Writing this exotic NN architecture with keras, tensorflow and python 使用Keras创建具有多个输入的NN体系结构 - Creating NN architecture with multiple inputs using Keras 使用相同种子、代码和数据集的不同机器上的不同精度 - Different accuracies on different machines using same seeds, code and dataset Keras ImageDataGenerator with flow, Got ValueError: `x` (images tensor) and `y` (labels) should have the same length - Keras ImageDataGenerator with flow, Got ValueError: `x` (images tensor) and `y` (labels) should have the same length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM