Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 Tensorflow卷积神经网络精度_Python_Tensorflow_Deep Learning_Neural Network - Fatal编程技术网

Python Tensorflow卷积神经网络精度

Python Tensorflow卷积神经网络精度,python,tensorflow,deep-learning,neural-network,Python,Tensorflow,Deep Learning,Neural Network,我正在尝试使用Tensorflow(三个Conv层,然后是完全连接的)来训练一个用于多类分类问题的小型卷积神经网络。图表构建起来很轻松,没有任何问题,培训也开始了,但结果并没有多大意义 有关数据和代码的一些简要信息如下: 数据形状(格式=NHWC):(N,1,150,3) 目标数据形状:(N,3) 下面是我使用的全部代码: # Hyperparameters: training_iters = 50 learning_rate = 0.001 batch_size = 128 # Netw

我正在尝试使用Tensorflow(三个Conv层,然后是完全连接的)来训练一个用于多类分类问题的小型卷积神经网络。图表构建起来很轻松,没有任何问题,培训也开始了,但结果并没有多大意义

有关数据和代码的一些简要信息如下:

  • 数据形状(格式=NHWC):
    (N,1,150,3)
  • 目标数据形状:
    (N,3)
下面是我使用的全部代码:

# Hyperparameters:
training_iters = 50
learning_rate = 0.001
batch_size = 128

# Network Parameters:
n_classes = 3  # total classes

tf.reset_default_graph()

x = tf.placeholder("float", [None, 1, 150, 3])
y = tf.placeholder("float", [None, n_classes])

def conv2d(x, W, b, strides=1):
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='VALID')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)

def maxpool2d(x, k=2):
    return tf.nn.max_pool(x, ksize=[1,1,k,1], strides=[1,1,k,1], padding='VALID')

# defining weights and biases
weights = {
    'wc1': tf.get_variable('W0', shape=(1,5,3,4), initializer=tf.contrib.layers.xavier_initializer()),
    'wc2': tf.get_variable('W1', shape=(1,4,4,8), initializer=tf.contrib.layers.xavier_initializer()),
    'wc3': tf.get_variable('W2', shape=(1,4,8,16), initializer=tf.contrib.layers.xavier_initializer()),
    'wd1': tf.get_variable('W3', shape=(1*16*16,12), initializer=tf.contrib.layers.xavier_initializer()),
    'out': tf.get_variable('W6', shape=(12, n_classes), initializer=tf.contrib.layers.xavier_initializer())
}

biases = {
    'bc1': tf.get_variable('B0', shape=(4), initializer=tf.contrib.layers.xavier_initializer()),
    'bc2': tf.get_variable('B1', shape=(8), initializer=tf.contrib.layers.xavier_initializer()),
    'bc3': tf.get_variable('B2', shape=(16), initializer=tf.contrib.layers.xavier_initializer()),
    'bd1': tf.get_variable('B3', shape=(12), initializer=tf.contrib.layers.xavier_initializer()),
    'out': tf.get_variable('B4', shape=(n_classes), initializer=tf.contrib.layers.xavier_initializer()),
}

def conv_net(x, weights, biases):  
    # here we call the conv2d function we had defined above and pass the input image x, weights wc1 and bias bc1.
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    # Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 14*14 matrix.
    conv1 = maxpool2d(conv1, k=2)

    # Convolution Layer
    # here we call the conv2d function we had defined above and pass the input image x, weights wc2 and bias bc2.
    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    # Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 7*7 matrix.
    conv2 = maxpool2d(conv2, k=2)

    conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
    # Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 4*4.
    conv3 = maxpool2d(conv3, k=2)

    # Fully connected layer
    # Reshape conv3 output to fit fully connected layer input

    fc1 = tf.reshape(conv3, [-1, weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
    fc1 = tf.nn.relu(fc1)

    # Output, class prediction
    # finally we multiply the fully connected layer with the weights and add a bias term. 
    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
    return out

init = tf.global_variables_initializer()

pred = conv_net(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=pred))
optimizer = tf.train.AdamOptimizer().minimize(cost)

correct_prediction = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()) 
    train_loss = []
    test_loss = []
    train_accuracy = []
    test_accuracy = []
    summary_writer = tf.summary.FileWriter('./Logs/Output', sess.graph)
    for i in range(training_iters):
        for batch in range(len(train_X)//batch_size):
            batch_x = train_X[batch*batch_size:min((batch+1)*batch_size,len(train_X))]
            batch_y = train_y[batch*batch_size:min((batch+1)*batch_size,len(train_y))]    
            # Run optimization op (backprop).
                # Calculate batch loss and accuracy
            opt, loss, acc = sess.run([optimizer, cost, accuracy], feed_dict={x: batch_x, y: batch_y})
            #acc, loss = sess.run([accuracy, cost], feed_dict={x: batch_x, y: batch_y})

        print(f"Iter: {str(i+1)} \nTrain Loss: {loss:.8}, Train Accuracy: {acc:.8}")

        # Calculate accuracy for test data
        test_acc,valid_loss = sess.run([accuracy,cost], feed_dict={x: test_X,y : test_y})
        train_loss.append(loss)
        test_loss.append(valid_loss)
        train_accuracy.append(acc)
        test_accuracy.append(test_acc)
        saved_path = saver.save(sess, './Logs/Output/my-model', global_step=i)
        print(f"Test  Loss: {valid_loss:.8},    Test Accuracy: {test_acc:.8}")
    summary_writer.close()
培训的进度输出为:

Iter: 1 
Train Loss: 1.8626448e-08, Train Accuracy: 1.0
Test  Loss: 4.4217701,    Test Accuracy: 0.77379447
Iter: 2 
Train Loss: 9.3132252e-10, Train Accuracy: 1.0
Test  Loss: 4.3212051,    Test Accuracy: 0.77379447
Iter: 3 
Train Loss: 1.1455266e-07, Train Accuracy: 1.0
Test  Loss: 3.8599913,    Test Accuracy: 0.77379447
Iter: 4 
Train Loss: 1.5655376e-05, Train Accuracy: 1.0
Test  Loss: 4.6927552,    Test Accuracy: 0.77379447
Iter: 5 
Train Loss: 1.5967382e-05, Train Accuracy: 1.0
Test  Loss: 2.5689926,    Test Accuracy: 0.77379447
Iter: 6 
Train Loss: 0.00015663292, Train Accuracy: 1.0
Test  Loss: 2.0523434,    Test Accuracy: 0.77379447
Iter: 7 
Train Loss: 0.0017181182, Train Accuracy: 1.0
Test  Loss: 1.8788136,    Test Accuracy: 0.77379447
Iter: 8 
Train Loss: 4.656612e-09, Train Accuracy: 1.0
Test  Loss: 7.2591481,    Test Accuracy: 0.77379447
Iter: 9 
Train Loss: 7.4859377e-06, Train Accuracy: 1.0
Test  Loss: 3.1993084,    Test Accuracy: 0.77379447
Iter: 10 
Train Loss: 0.00010431254, Train Accuracy: 1.0
Test  Loss: 2.1976376,    Test Accuracy: 0.77379447
Iter: 11 
Train Loss: 0.0025378512, Train Accuracy: 1.0
Test  Loss: 1.5550417,    Test Accuracy: 0.77379447
Iter: 12 
Train Loss: 0.0010065944, Train Accuracy: 1.0
Test  Loss: 1.6730684,    Test Accuracy: 0.77379447
Iter: 13 
Train Loss: 0.00052482914, Train Accuracy: 1.0
Test  Loss: 1.8188649,    Test Accuracy: 0.77379447
Iter: 14 
Train Loss: 0.00013962867, Train Accuracy: 1.0
Test  Loss: 2.2431495,    Test Accuracy: 0.77379447
Iter: 15 
Train Loss: 0.00098675233, Train Accuracy: 1.0
Test  Loss: 2.0452898,    Test Accuracy: 0.77379447
在这里,经过一些迭代后,损耗开始正常运行,并开始稳步下降,并且可能通过试验参数变得更好。虽然问题在于准确度,但培训和测试都是如此。到底是什么导致了这一切?我想可能是因为我计算
正确预测
pred
的方式。。如何解决这个问题


我目前对深度学习和所有方面都是新手,并且仍在试图弄清楚如何使用低级API。所以,请原谅我的天真。非常感谢您的任何帮助

只是一个建议:在深入研究低级API之前,先用高级API(最重要的是Keras)构建网络,看看这是否符合您的预期,然后,如果您仍然感兴趣,再深入研究低级API。这样,您将知道问题是在您的实现中,还是仅仅在网络学习数据集的能力中。你也可能会意识到,也许,高级API实际上正是你所需要的。是的,我认为没有必要提及这一点,但我已经在Keras上工作了相当长的一段时间,我甚至在PyTorch和CoreML上工作了几个月,一切都很好。我刚刚转向Tensorflow,因为它提供了更多的控制,并且在部署方面更加成熟。因此,这可能是我使用Tensorflow的旅程的开始,我需要一个帮手您可以尝试在最大池层之前包含更多卷积层。将尝试此。。但是@ShubhamPanchal你的意思是,代码和图形体系结构没有任何问题导致我的训练和测试精度停滞?