Machine learning Tensorflow:低损耗导致低精度
我正在尝试使用Tensorflow并构建一个简单的逻辑回归分类器。训练集由大约50万(48万)个数据点组成。我正在培训批量为100码的产品 当训练模型时,损失在20个训练周期内从0.002下降到0.000000034(在每个训练周期内逐批遍历整个训练集) 然而,这使得训练集的准确度为0.671725,这似乎非常低。据我所知,训练集的准确度应接近1.00(100%)。由于我是Tensorflow的新手,我不确定我的计算中是否遗漏了一个重要的细节Machine learning Tensorflow:低损耗导致低精度,machine-learning,tensorflow,logistic-regression,Machine Learning,Tensorflow,Logistic Regression,我正在尝试使用Tensorflow并构建一个简单的逻辑回归分类器。训练集由大约50万(48万)个数据点组成。我正在培训批量为100码的产品 当训练模型时,损失在20个训练周期内从0.002下降到0.000000034(在每个训练周期内逐批遍历整个训练集) 然而,这使得训练集的准确度为0.671725,这似乎非常低。据我所知,训练集的准确度应接近1.00(100%)。由于我是Tensorflow的新手,我不确定我的计算中是否遗漏了一个重要的细节 将批处理大小更改为1000并没有多大区别-从0.67
x = tf.placeholder(tf.float32, [None, window_size]) # data with the size of the window currently set to 6
y = tf.placeholder(tf.float32, [None, 2])
W = tf.Variable(tf.zeros([window_size, 2]))
b = tf.Variable(tf.zeros([2]))
# Construct model
model = tf.nn.sigmoid(tf.matmul(x, W) + b) #Sigmoid
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(model), reduction_indices=1))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
# Inirializing the variables
init = tf.initialize_all_variables()
print (len(feed_windows_np))
batch_size = 1000
#Splitting to train and test
train = feed_windows_np[:len(feed_windows_np)//2 - ((len(feed_windows_np)//2) % batch_size)]
train_labels = labels[:len(feed_windows_np)//2 - ((len(feed_windows_np)//2) % batch_size)]
test = feed_windows_np[(len(feed_windows_np) * 2) // 3:]
test_labels = labels[(len(feed_windows_np) * 2) // 3:]
total_batches = int(math.floor(len(train) / batch_size))
avg_costs = []
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(epochs):
avg_cost = 0.
for i in range(total_batches):
feed_dict = {
x: train[i * batch_size:(i + 1) * batch_size],
y: labels[i * batch_size:(i + 1) * batch_size]
}
_, c = sess.run([optimizer, cost], feed_dict=feed_dict)
#sess.run(optimizer, feed_dict=feed_dict)
# updating the loss
avg_cost += c / total_batches
if (epoch + 1) % display_step == 0:
print ('Epoch:', '%04d' % (epoch + 1), 'cost=', '{:.9f}'.format(avg_cost))
avg_costs.append(avg_cost)
print ('Optimisation Finished!')
plt.plot(avg_costs)
plt.show()
# Training accuracy
correct_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
accuracy_val= sess.run(accuracy, feed_dict={x: train, y: train_labels})
print ('Accuracy on training data:', accuracy_val)
知道是什么导致了这种行为吗?它的大小是否很大,因为当下降到80个训练数据点时,准确度达到了应有的水平?Hm,你对y的定义看起来很可疑。如果只有两个类,为什么输出维度是2?您应该设置为1(建议;第一个类的值为0,第二个类的值为1),或者使用softmax作为模型。感谢您的回复。我按2级分类经营。一个标签是[0,1],另一个是[1,0]。我也使用了softmax,它产生了相同的效果。两个类不需要两个输出。一个乙状结肠就足够了。当您使用2维y和1维模型时,我可以看到代码中存在问题。可能需要将y的形状更改为[None,1]