Softmax逻辑回归:scikit-learn和TensorFlow的不同表现

我正在尝试在一些数据上学习一个简单的线性Softmax模型。scikit-learn中的LogisticRegression似乎工作得很好,现在我试图将代码移植到TensorFlow中,但我没有得到同样的表现,而是差了很多。我理解结果不会完全相同(scikit-learn有正则化参数等),但差距太大了。

total = pd.read_feather('testfile.feather')labels = total['labels']features = total[['f1', 'f2']]print(labels.shape)print(features.shape)classifier = linear_model.LogisticRegression(C=1e5, solver='newton-cg', multi_class='multinomial')classifier.fit(features, labels)pred_labels = classifier.predict(features)print("SCI-KITLEARN RESULTS: ")print('\tAccuracy:', classifier.score(features, labels)) print('\tPrecision:', precision_score(labels, pred_labels, average='macro'))print('\tRecall:', recall_score(labels, pred_labels, average='macro'))print('\tF1:', f1_score(labels, pred_labels, average='macro'))# now try softmax regression with tensorflow print("\n\nTENSORFLOW RESULTS: ")## By default, the OneHotEncoder class will return a more efficient sparse encoding. ## This may not be suitable for some applications, such as use with the Keras deep learning library. ## In this case, we disabled the sparse return type by setting the sparse=False argument.enc = OneHotEncoder(sparse=False)enc.fit(labels.values.reshape(len(labels), 1)) # Reshape is required as Encoder expect 2D data as inputlabels_one_hot = enc.transform(labels.values.reshape(len(labels), 1))# tf Graph Inputx = tf.placeholder(tf.float32, [None, 2]) # 2 input featuresy = tf.placeholder(tf.float32, [None, 5]) # 5 output classes# Set model weightsW = tf.Variable(tf.zeros([2, 5]))b = tf.Variable(tf.zeros([5]))# Construct modelpred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmaxclas = tf.argmax(pred, axis=1)# Minimize error using cross entropycost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))# Gradient Descentoptimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost)# Initialize the variables (i.e. assign their default value)init = tf.global_variables_initializer()# Start trainingwith tf.Session() as sess:    # Run the initializer    sess.run(init)    # Training cycle    for epoch in range(1000):          # Run optimization op (backprop) and cost op (to get loss value)        _, c = sess.run([optimizer, cost], feed_dict={x: features, y: labels_one_hot})    # Test model    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))    class_out = clas.eval({x: features})    # Calculate accuracy    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))    print("\tAccuracy:", accuracy.eval({x: features, y: labels_one_hot}))    print('\tPrecision:', precision_score(labels, class_out, average='macro'))    print('\tRecall:', recall_score(labels, class_out, average='macro'))    print('\tF1:', f1_score(labels, class_out, average='macro'))

这段代码的输出是

(1681,)(1681, 2)SCI-KITLEARN RESULTS:    Accuracy: 0.822129684711    Precision: 0.837883361162    Recall: 0.784522522208    F1: 0.806251963817TENSORFLOW RESULTS:    Accuracy: 0.694825    Precision: 0.735883666192    Recall: 0.649145125846    F1: 0.678045562185

我检查了一热编码的结果和数据,但我不知道为什么TF中的结果差很多。

任何建议都会非常感激..


回答:

问题最终证明是愚蠢的,我只需要更多的训练周期,一个更小的学习率(为了效率,我转而使用AdamOptimizer,现在结果相同,尽管TF实现要慢得多。

(1681,)(1681, 2)SCI-KITLEARN RESULTS:    Accuracy: 0.822129684711    Precision: 0.837883361162    Recall: 0.784522522208    F1: 0.806251963817TENSORFLOW RESULTS:    Accuracy: 0.82213    Precision: 0.837883361162    Recall: 0.784522522208    F1: 0.806251963817

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注