Python ConvNN精度分数和精度图

Python ConvNN精度分数和精度图,python,tensorflow,deep-learning,conv-neural-network,Python,Tensorflow,Deep Learning,Conv Neural Network,嗨(关于Begging,我想说我是神经网络的新手)! 我一直在用python编写简单的猫和狗分类器。我正在使用python和tensorflow。神经网络的类型是Conv。我训练了几次网络,我得到了低精度分数(50%),精度图看起来很奇怪 这是神经网络: def create_net(): weights, biases = init_weights_biases() l1 = conv2d(x, weights['wc1'], biases['bc1']) l1 =

嗨(关于Begging,我想说我是神经网络的新手)! 我一直在用python编写简单的猫和狗分类器。我正在使用python和tensorflow。神经网络的类型是Conv。我训练了几次网络,我得到了低精度分数(50%),精度图看起来很奇怪

这是神经网络:

def create_net():
    weights, biases = init_weights_biases()

    l1 = conv2d(x, weights['wc1'], biases['bc1'])
    l1 = maxpool2d(l1)

    l2 = conv2d(l1, weights['wc2'], biases['bc2'])
    l2 = maxpool2d(l2)

    l3 = conv2d(l2, weights['wc3'], biases['bc3'])
    l3 = maxpool2d(l3)

    l4 = tf.reshape(l3, shape=[-1, weights['wfc'].get_shape().as_list()[0]])
    l4 = tf.add(tf.matmul(l4, weights['wfc']), biases['bfc'])
    l4 = tf.nn.softmax(l4)
    l4 = tf.nn.dropout(l4, .5)

    out = tf.add(tf.matmul(l4, weights['bout']), biases['bout'])
    return out

pred = create_net()
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=.001).minimize(cost)
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
for i in range(epochs):
    if previous_batch >= len(X_train):
        previous_batch = 0

    current_batch = previous_batch + batch

    X_train_i = X_train[previous_batch:current_batch]
    X_train_i = np.array(X_train_i).reshape(batch, 64, 64, 1)

    y_train_i = y_train[previous_batch:current_batch]
    y_train_i = np.array(y_train_i)

    sess.run(optimizer, feed_dict={
        x: X_train_i,
        y: y_train_i
    })

    previous_batch += batch
下面是我如何训练神经网络的:

def create_net():
    weights, biases = init_weights_biases()

    l1 = conv2d(x, weights['wc1'], biases['bc1'])
    l1 = maxpool2d(l1)

    l2 = conv2d(l1, weights['wc2'], biases['bc2'])
    l2 = maxpool2d(l2)

    l3 = conv2d(l2, weights['wc3'], biases['bc3'])
    l3 = maxpool2d(l3)

    l4 = tf.reshape(l3, shape=[-1, weights['wfc'].get_shape().as_list()[0]])
    l4 = tf.add(tf.matmul(l4, weights['wfc']), biases['bfc'])
    l4 = tf.nn.softmax(l4)
    l4 = tf.nn.dropout(l4, .5)

    out = tf.add(tf.matmul(l4, weights['bout']), biases['bout'])
    return out

pred = create_net()
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=.001).minimize(cost)
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
for i in range(epochs):
    if previous_batch >= len(X_train):
        previous_batch = 0

    current_batch = previous_batch + batch

    X_train_i = X_train[previous_batch:current_batch]
    X_train_i = np.array(X_train_i).reshape(batch, 64, 64, 1)

    y_train_i = y_train[previous_batch:current_batch]
    y_train_i = np.array(y_train_i)

    sess.run(optimizer, feed_dict={
        x: X_train_i,
        y: y_train_i
    })

    previous_batch += batch

我认为您并不是在每个时代都遍历完整的数据集。您只是在每次迭代中使用大小
batch
的数据,我认为这些数据相当低,如100、128、256等。这可能是您获得低精度分数的原因

例如,考虑以下训练循环的输出(与您的相同),具有一些随机数据:

import numpy as np

epochs = 5
previous_batch = 0
X_train = np.random.rand(1000, 5)
batch = 128
y_train = np.random.rand(1000, 2)
for i in range(epochs):
    if previous_batch >= len(X_train):
        previous_batch = 0

    current_batch = previous_batch + batch
    print(previous_batch, current_batch)

    X_train_i = X_train[previous_batch:current_batch]
    y_train_i = y_train[previous_batch:current_batch]   
    print(i, X_train_i.shape, y_train_i.shape)

    previous_batch += batch
输出:

0 128
0 (128, 5) (128, 2) # epoch 0
128 256
1 (128, 5) (128, 2) # epoch 1
256 384
2 (128, 5) (128, 2) # epoch 2
384 512
3 (128, 5) (128, 2) # epoch 3
512 640
4 (128, 5) (128, 2) # epoch 4
在这里,每个迭代只使用整个数据集中的128个数据样本。换句话说,我们在每次迭代训练时没有使用整个数据集(X_序列,y_序列),这可能会导致较差的训练结果

为了在每次迭代中运行整个数据集,请执行以下操作:

for i in range(epochs):
    previous_batch = 0
    for j in range(len(X_train)//batch):
        current_batch = previous_batch + batch

        X_train_i = X_train[previous_batch:current_batch]
        X_train_i = np.array(X_train_i).reshape(batch, 64, 64, 1)

        y_train_i = y_train[previous_batch:current_batch]
        y_train_i = np.array(y_train_i)

        sess.run(optimizer, feed_dict={
            x: X_train_i,
            y: y_train_i
        })

        previous_batch += batch

我认为您并不是在每个时代都遍历完整的数据集。您只是在每次迭代中使用大小
batch
的数据,我认为这些数据相当低,如100、128、256等。这可能是您获得低精度分数的原因

例如,考虑以下训练循环的输出(与您的相同),具有一些随机数据:

import numpy as np

epochs = 5
previous_batch = 0
X_train = np.random.rand(1000, 5)
batch = 128
y_train = np.random.rand(1000, 2)
for i in range(epochs):
    if previous_batch >= len(X_train):
        previous_batch = 0

    current_batch = previous_batch + batch
    print(previous_batch, current_batch)

    X_train_i = X_train[previous_batch:current_batch]
    y_train_i = y_train[previous_batch:current_batch]   
    print(i, X_train_i.shape, y_train_i.shape)

    previous_batch += batch
输出:

0 128
0 (128, 5) (128, 2) # epoch 0
128 256
1 (128, 5) (128, 2) # epoch 1
256 384
2 (128, 5) (128, 2) # epoch 2
384 512
3 (128, 5) (128, 2) # epoch 3
512 640
4 (128, 5) (128, 2) # epoch 4
在这里,每个迭代只使用整个数据集中的128个数据样本。换句话说,我们在每次迭代训练时没有使用整个数据集(X_序列,y_序列),这可能会导致较差的训练结果

为了在每次迭代中运行整个数据集,请执行以下操作:

for i in range(epochs):
    previous_batch = 0
    for j in range(len(X_train)//batch):
        current_batch = previous_batch + batch

        X_train_i = X_train[previous_batch:current_batch]
        X_train_i = np.array(X_train_i).reshape(batch, 64, 64, 1)

        y_train_i = y_train[previous_batch:current_batch]
        y_train_i = np.array(y_train_i)

        sess.run(optimizer, feed_dict={
            x: X_train_i,
            y: y_train_i
        })

        previous_batch += batch