Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这个神经网络';s损耗始终为0.0,精度始终为1.0?_Python_Numpy_Tensorflow - Fatal编程技术网

Python 为什么这个神经网络';s损耗始终为0.0,精度始终为1.0?

Python 为什么这个神经网络';s损耗始终为0.0,精度始终为1.0?,python,numpy,tensorflow,Python,Numpy,Tensorflow,我试图在大约80个条目的数据集上训练一个基本的前馈神经网络(主要是为了证明概念,我知道我的数据集太小了)。我的代码基于。我选择了10个批量大小,并运行了8个步骤: learning_rate = 0.01 num_steps = 8 batch_size = 10 display_step = 1 num_input = 16 n_hidden_1 = 8 n_hidden_2 = 8 num_classes = 1 X = tf.placeholder("float", [None, nu

我试图在大约80个条目的数据集上训练一个基本的前馈神经网络(主要是为了证明概念,我知道我的数据集太小了)。我的代码基于。我选择了10个批量大小,并运行了8个步骤:

learning_rate = 0.01
num_steps = 8
batch_size = 10
display_step = 1

num_input = 16
n_hidden_1 = 8
n_hidden_2 = 8
num_classes = 1

X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_classes])

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]))
}

layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1'])
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
logits = tf.matmul(layer_2, weights['out']) + biases['out']

prediction = tf.nn.softmax(logits)
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)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for step in range(0, num_steps):
        batch_x, batch_y = manager.import_data()

        batch_x = batch_x[step * batch_size:(step + 1) * batch_size]
        batch_y = batch_y[step * batch_size:(step + 1) * batch_size]

        batch_x = np.reshape(batch_x, (batch_size, num_input))
        batch_y = np.reshape(batch_y, (batch_size, num_classes))
        sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
        if step % display_step == 0 or step == 1:
            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))
manager.import\u data()
返回numpy数组的列表。我知道我应该随机化批次的选择,我最终会实现这一点——但是,输出是:

步骤0,小批量损失=0.0000,训练精度=1.000
步骤1,小批量损失=0.0000,训练精度=1.000
步骤2,小批量损失=0.0000,训练精度=1.000
步骤3,小批量损失=0.0000,训练精度=1.000
步骤4,小批量损失=0.0000,训练精度=1.000
步骤5,小批量损失=0.0000,训练精度=1.000
步骤6,小批量损失=0.0000,训练精度=1.000
步骤7,小批量损失=0.0000,训练精度=1.000

显然,情况不应该如此。我做错了什么

我猜在您的培训集中,所有项目都具有相同的标签(例如0)


处理神经网络时,最好的做法是准备3个不同的集合-训练、val和测试,在类之间的分布大致相同。Train用于训练时间,val用于每次迭代结束时保存或忽略模型。测试类似于对模型的现实检查,您不应该根据测试分数调整参数。

在神经网络中,您需要一个类的正面和负面示例。因此,该算法可以区分什么是汽车,什么不是。所以你需要更多的类来告诉他们什么是好的,什么是错的。谢谢