我是tensorflow的新手,正在练习构建一个简单的前馈神经网络,并且正在发生一些奇怪的事情。
我试图预测二进制标签(即它是0或1)。所以我使用以下代码。
# I return the same number of training points that have label of 1 as
# label of 0 because before NN was returning 1 always (there are much more 1s
# than 0s)
def next_batch(num, data_mat, labels, helper):
'''
Return a total of `num` random samples and labels.
'''
num = num/2
num = int(num)
idx = np.arange(0 , len(data_mat))
pos_indices = np.where(helper == [1])
pos_indices = pos_indices[0]
np.random.shuffle(idx)
np.random.shuffle(pos_indices)
idx = idx[:num]
pos_indices = pos_indices[:num]
idx = np.concatenate((idx, pos_indices))
np.random.shuffle(idx)
data_shuffle = [data_mat[i] for i in idx]
labels_shuffle = [labels[i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
def neural_net(x, weights, biases):
# Hidden fully connected layer with 256 neurons
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
# Hidden fully connected layer with 256 neurons
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
# Output fully connected layer with a neuron for each class
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
learning_rate = 0.1
num_steps = 500
batch_size = 200
display_step = 100
n_hidden_1 = 10 # 1st layer number of neurons
n_hidden_2 = 10 # 2nd layer number of neurons
num_input = len(train.columns.values) #
num_classes = 2 #
# tf Graph input
X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_classes])
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([num_classes]))
}
logits = neural_net(X, weights, biases)
prediction = tf.nn.softmax(logits)
# Define loss and optimizer
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
# Evaluate model
correct_pred = tf.equal(tf.argmax(prediction), tf.argmax(Y))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
with tf.Session() as sess:
# Run the initializer
sess.run(init)
for step in range(1, num_steps+1):
batch_x, batch_y = next_batch(batch_size, train_matrix, labels)
# Run optimization op (backprop)
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
if step % display_step == 0 or step == 1:
# Calculate batch loss and accuracy
loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
Y: batch_y})
print("Step " + str(step) + ", Minibatch Loss= " + \
"{:.4f}".format(loss) + ", Training Accuracy= " + \
"{:.3f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for train
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={X: train,
Y: labels}))
所以我运行它并暂停最后一个打印语句。我运行predictions = sess.run(prediction, feed_dict={X: batch_x})
并获得一些预测。我运行predictions = [1 if x[1] > x[0] else 0 for x in predictions ]
和tru_labels = [1 if x[1] > x[0] else 0 for x in batch_y]
并计算这两者不同的次数。我得到6.我做14.0 / 20我的准确率为0.7。然后我运行sess.run(accuracy, feed_dict={X: batch_x, Y: batch_y})
,我得到0.0。为什么?这是怎么回事?
这也是打印语句的输出:
Step 1, Minibatch Loss= 21.6776, Training Accuracy= 0.500
Step 100, Minibatch Loss= 0.4614, Training Accuracy= 0.000
Step 200, Minibatch Loss= 0.5002, Training Accuracy= 0.500
Step 300, Minibatch Loss= 0.5157, Training Accuracy= 0.000
Step 400, Minibatch Loss= 0.5495, Training Accuracy= 0.000
Step 500, Minibatch Loss= 0.5910, Training Accuracy= 0.000
Step 600, Minibatch Loss= 0.5321, Training Accuracy= 0.000
Step 700, Minibatch Loss= 0.5180, Training Accuracy= 0.500
Step 800, Minibatch Loss= 0.5418, Training Accuracy= 0.000
Step 900, Minibatch Loss= 0.5050, Training Accuracy= 0.000
Step 1000, Minibatch Loss= 0.5108, Training Accuracy= 0.000
Step 1100, Minibatch Loss= 0.4737, Training Accuracy= 0.000
Step 1200, Minibatch Loss= 0.5985, Training Accuracy= 0.000
Step 1300, Minibatch Loss= 0.2716, Training Accuracy= 0.000
Step 1400, Minibatch Loss= 0.5839, Training Accuracy= 0.000
Step 1500, Minibatch Loss= 0.6726, Training Accuracy= 0.000
Step 1600, Minibatch Loss= 17.2756, Training Accuracy= 1.000
Step 1700, Minibatch Loss= 0.8098, Training Accuracy= 0.000
Step 1800, Minibatch Loss= 0.5322, Training Accuracy= 0.000
Step 1900, Minibatch Loss= 0.5866, Training Accuracy= 0.000
Step 2000, Minibatch Loss= 0.5407, Training Accuracy= 0.000
Step 2100, Minibatch Loss= 0.6749, Training Accuracy= 0.000
Step 2200, Minibatch Loss= 0.5363, Training Accuracy= 0.000
Step 2300, Minibatch Loss= 0.5968, Training Accuracy= 0.000
Step 2400, Minibatch Loss= 0.4667, Training Accuracy= 0.000
Step 2500, Minibatch Loss= 0.5713, Training Accuracy= 0.000
Step 2600, Minibatch Loss= 0.6382, Training Accuracy= 0.000
Step 2700, Minibatch Loss= 0.6168, Training Accuracy= 0.000
Step 2800, Minibatch Loss= 0.6685, Training Accuracy= 0.000
Step 2900, Minibatch Loss= 0.4987, Training Accuracy= 0.000
Step 3000, Minibatch Loss= 0.3820, Training Accuracy= 0.000
Step 3100, Minibatch Loss= 0.4556, Training Accuracy= 0.000
Step 3200, Minibatch Loss= 0.4292, Training Accuracy= 0.000
Step 3300, Minibatch Loss= 0.6192, Training Accuracy= 0.000
Step 3400, Minibatch Loss= 0.6137, Training Accuracy= 0.000
Step 3500, Minibatch Loss= 0.5665, Training Accuracy= 0.000
Step 3600, Minibatch Loss= 0.2847, Training Accuracy= 0.000
Step 3700, Minibatch Loss= 0.3382, Training Accuracy= 0.000
Step 3800, Minibatch Loss= 0.5396, Training Accuracy= 0.000
Step 3900, Minibatch Loss= 0.4069, Training Accuracy= 0.000
Step 4000, Minibatch Loss= 0.6689, Training Accuracy= 0.000
Step 4100, Minibatch Loss= 0.4920, Training Accuracy= 0.000
Step 4200, Minibatch Loss= 0.5750, Training Accuracy= 0.000
Step 4300, Minibatch Loss= 0.4918, Training Accuracy= 0.000
Step 4400, Minibatch Loss= 0.4784, Training Accuracy= 0.000
Step 4500, Minibatch Loss= 0.6457, Training Accuracy= 0.000
Step 4600, Minibatch Loss= 0.4326, Training Accuracy= 0.000
Step 4700, Minibatch Loss= 0.4557, Training Accuracy= 0.000
Step 4800, Minibatch Loss= 0.3729, Training Accuracy= 0.000
Step 4900, Minibatch Loss= 0.5595, Training Accuracy= 0.000
Step 5000, Minibatch Loss= 0.4460, Training Accuracy= 0.000
Step 5100, Minibatch Loss= 0.5430, Training Accuracy= 0.500
Step 5200, Minibatch Loss= 0.3638, Training Accuracy= 0.000
Step 5300, Minibatch Loss= 0.4524, Training Accuracy= 0.000
Step 5400, Minibatch Loss= 0.7159, Training Accuracy= 0.000
Step 5500, Minibatch Loss= 4.7344, Training Accuracy= 0.000
Step 5600, Minibatch Loss= 0.5006, Training Accuracy= 0.000
Step 5700, Minibatch Loss= 0.5062, Training Accuracy= 0.000
Step 5800, Minibatch Loss= 0.4394, Training Accuracy= 0.000
Step 5900, Minibatch Loss= 0.5160, Training Accuracy= 0.000
Step 6000, Minibatch Loss= 0.3884, Training Accuracy= 0.000
Step 6100, Minibatch Loss= 0.5501, Training Accuracy= 0.000
Step 6200, Minibatch Loss= 0.4486, Training Accuracy= 0.000
Step 6300, Minibatch Loss= 0.4165, Training Accuracy= 0.000
Step 6400, Minibatch Loss= 0.4924, Training Accuracy= 0.000
Step 6500, Minibatch Loss= 0.4942, Training Accuracy= 0.000
Step 6600, Minibatch Loss= 0.4783, Training Accuracy= 0.000
Step 6700, Minibatch Loss= 0.3772, Training Accuracy= 0.000
Step 6800, Minibatch Loss= 0.7205, Training Accuracy= 0.000
Step 6900, Minibatch Loss= 0.5531, Training Accuracy= 0.000
Step 7000, Minibatch Loss= 0.5829, Training Accuracy= 0.000
Step 7100, Minibatch Loss= 0.6349, Training Accuracy= 0.000
Step 7200, Minibatch Loss= 0.5420, Training Accuracy= 0.000
Step 7300, Minibatch Loss= 0.3575, Training Accuracy= 0.500
Step 7400, Minibatch Loss= 0.4242, Training Accuracy= 0.000
Step 7500, Minibatch Loss= 0.5211, Training Accuracy= 0.500
Step 7600, Minibatch Loss= 0.3020, Training Accuracy= 0.000
Step 7700, Minibatch Loss= 0.4305, Training Accuracy= 0.500
Step 7800, Minibatch Loss= 0.5304, Training Accuracy= 0.000
Step 7900, Minibatch Loss= 0.5394, Training Accuracy= 0.000
Step 8000, Minibatch Loss= 0.5554, Training Accuracy= 0.000
Step 8100, Minibatch Loss= 0.4356, Training Accuracy= 0.000
Step 8200, Minibatch Loss= 0.3782, Training Accuracy= 0.000
Step 8300, Minibatch Loss= 0.3854, Training Accuracy= 0.000
Step 8400, Minibatch Loss= 0.6727, Training Accuracy= 0.000
Step 8500, Minibatch Loss= 0.5484, Training Accuracy= 0.000
Step 8600, Minibatch Loss= 0.6856, Training Accuracy= 0.000
Step 8700, Minibatch Loss= 4.6333, Training Accuracy= 0.500
Step 8800, Minibatch Loss= 1.7541, Training Accuracy= 0.500
Step 8900, Minibatch Loss= 0.3309, Training Accuracy= 0.000
Step 9000, Minibatch Loss= 0.4506, Training Accuracy= 0.000
Step 9100, Minibatch Loss= 0.7060, Training Accuracy= 0.000
Step 9200, Minibatch Loss= 0.7779, Training Accuracy= 0.500
Step 9300, Minibatch Loss= 0.5186, Training Accuracy= 0.000
Step 9400, Minibatch Loss= 0.5144, Training Accuracy= 0.000
Step 9500, Minibatch Loss= 0.6899, Training Accuracy= 0.000
Step 9600, Minibatch Loss= 0.4099, Training Accuracy= 0.000
Step 9700, Minibatch Loss= 0.5568, Training Accuracy= 0.000
Step 9800, Minibatch Loss= 0.4362, Training Accuracy= 0.000
Step 9900, Minibatch Loss= 0.4632, Training Accuracy= 0.500
Step 10000, Minibatch Loss= 0.5170, Training Accuracy= 0.000
Optimization Finished!
任何人都知道为什么我的损失不会下降得多吗?我知道随机森林很容易获得更好的损失。
答案 0 :(得分:1)
尝试检查您需要找到最大值的轴。可能它应该是:
correct_pred = tf.equal(tf.argmax(预测,轴= 1),tf.argmax(Y,轴= 1))