CNN没有正确训练和测试

时间:2018-06-09 14:54:52

标签: python tensorflow machine-learning deep-learning convolutional-neural-network

我试图解决二进制分类任务。我有一个20,000 X 50的0和1的矩阵,每张图片20,000张图片是4392的维度向量,在此过程中我将重塑为61 * 72并通过网。在我通过网络传递图像后,我想为每个图像获得50个零和1的维度向量,然后在预测结果与我加载的真实值之间进行比较。  我有一个卷积神经网络,我使用这个例子建立: https://pythonprogramming.net/cnn-tensorflow-convolutional-nerual-network-machine-learning-tutorial/?completed=/convolutional-neural-network-cnn-machine-learning-tutorial//
我有用于训练和测试的图像数据集(通常80%用于训练,20%用于测试)。但是当我运行代码时我的训练准确性和失败不会改为在整个过程中匹配并保持相同的值。
我尝试在我的代码中更改几个参数,包括数据大小,学习率(0.001,0.1,0.01,0.0001),层数(2, 3,4,5),并尝试了几个成本函数和精度计算,但它根本没有帮助。 这是我的代码:
 

 
    from future import absolute_import
    from future import division
    from future import print_function
    import csv 
    import time
    import os
    import urllib

import numpy as np import tensorflow as tf start_time = time.time() data_in = np.transpose(np.loadtxt(open("20k_IMAGES.csv"), delimiter=",")) ones_zeros= np.loadtxt(open("activations20k.csv"), delimiter=",") print("--- %s seconds ---" % (time.time() - start_time)) epochs=10 learning_rate=0.001 batch_size=200 n_classes=50 height=61 width=72 channels=1 n_inputs=height*width train_X=data_in[0:16000,:] #training set (images) (8000,4392) test_X=data_in[16000:20000,:] # test se (images) (2000,4392) train_Y=ones_zeros[0:16000,:] #training set (labels) (8000,50) test_Y=ones_zeros[16000:20000,:] #test set (labels) (2000,50) n = train_X.shape[1] #4392 m = train_X.shape[0] d = train_Y.shape[1] #50 k= train_Y.shape[0] l = test_X.shape[0] #2000 x = tf.placeholder(tf.float32, shape=[None, n_inputs], name="X") trainY = tf.placeholder(tf.float32, shape=[None,n_classes], name="trainY") testY= tf.placeholder(tf.int32, shape=[l,d], name="testY") testX= tf.placeholder(tf.int32, shape=[l,n], name="testX") keep_rate = 0.8 keep_prob = tf.placeholder(tf.float32) #try to remove it def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def maxpool2d(x): # size of window movement of window return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') def convolutional_neural_network(x): weights = {'W_conv1': tf.Variable(tf.random_normal([5, 5, 1, 32])), 'W_conv2': tf.Variable(tf.random_normal([5, 5, 32, 64])), 'W_conv3': tf.Variable(tf.random_normal([5, 5, 64, 128])), 'W_conv4': tf.Variable(tf.random_normal([5, 5, 128, 256])), 'W_conv5': tf.Variable(tf.random_normal([5, 5, 256, 512])), 'W_fc': tf.Variable(tf.random_normal([2 * 3 * 512, 1024])), #check if 15 or 16 'out': tf.Variable(tf.random_normal([1024, n_classes]))} biases = {'b_conv1': tf.Variable(tf.random_normal([32])), 'b_conv2': tf.Variable(tf.random_normal([64])), 'b_conv3':tf.Variable(tf.random_normal([128])), 'b_conv4': tf.Variable(tf.random_normal([256])), 'b_conv5': tf.Variable(tf.random_normal([512])), 'b_fc': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_classes]))} x = tf.reshape(x, shape=[-1, 61, 72, 1]) conv1 = tf.nn.relu(conv2d(x, weights['W_conv1']) + biases['b_conv1']) conv1 = maxpool2d(conv1) conv2 = tf.nn.relu(conv2d(conv1, weights['W_conv2']) + biases['b_conv2']) conv2 = maxpool2d(conv2) conv3 = tf.nn.relu(conv2d(conv2, weights['W_conv3']) + biases['b_conv3']) conv3 = maxpool2d(conv3) conv4 = tf.nn.relu(conv2d(conv3, weights['W_conv4']) + biases['b_conv4']) conv4 = maxpool2d(conv4) conv5 = tf.nn.relu(conv2d(conv4, weights['W_conv5']) + biases['b_conv5']) conv5 = maxpool2d(conv5) fc = tf.reshape(conv5, [-1, 2 * 3 * 512]) fc = tf.nn.relu(tf.matmul(fc, weights['W_fc']) + biases['b_fc']) fc = tf.nn.dropout(fc, keep_rate) output = tf.matmul(fc, weights['out']) + biases['out'] sig_output = tf.nn.sigmoid(output) return sig_output def train_neural_network(x): prediction = convolutional_neural_network(x) #cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=prediction, labels=trainY)) #cost= tf.reduce_mean(tf.losses.sigmoid_cross_entropy(multi_class_labels=trainY, logits=prediction)) cost=tf.losses.absolute_difference(labels=trainY, predictions=prediction) #cost=tf.losses.log_loss(labels=trainY, predictions=prediction) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Here you check whether the index of the maximum value of the predicted image is equal to the actual labelled image. and both will be a column vector. correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(trainY, 1)) # calculate accuracy across all the given images and average them out. accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(epochs): for iteration in range(len(train_X) // batch_size): X_batch = train_X[iteration * batch_size:iteration * batch_size + batch_size,:] y_batch = train_Y[iteration * batch_size:iteration * batch_size + batch_size,:] sess.run(optimizer, feed_dict={x: X_batch , trainY: y_batch}) train_loss=sess.run(cost, feed_dict={x: X_batch , trainY: y_batch}) train_acc=sess.run(accuracy, feed_dict={x: X_batch , trainY: y_batch}) preds_train = sess.run(prediction, feed_dict={x: X_batch, trainY: y_batch}) print("Batch:", '%04d' % (batch_size * iteration), "iteration:", '%04d' % iteration, "Epoch", '%04d' % (epoch + 1), "loss=","{:.6f}".format(train_loss), ",training accuracy=", "{:.6f}".format(train_acc)) # ,"probabilities:",predictions) #print("train predictions:", preds_train) print("Optimization Finished!") valid_loss = sess.run([cost], feed_dict={x: test_X, trainY: test_Y}) #change trainY to y in test and training print("validation loss:", valid_loss) test_acc = sess.run([accuracy], feed_dict={x: test_X, trainY: test_Y})#change trainY to y in test and training print("Testing Accuracy:",test_acc) preds_test=sess.run(prediction, feed_dict={x: test_X, trainY: test_Y}) print("test predictions:", preds_test) #pred = open('predictions.csv', 'w') np.savetxt("prediction4k_of20k.csv", preds_test, delimiter=",") np.savetxt("test_Y4k_of_20k.csv", test_Y, delimiter=",") train_neural_network(x) print("--- %s seconds ---" % (time.time() - start_time))

我还可以做些什么来改进我的代码,以便它可以正常工作?解决它? 谢谢你的帮助!

0 个答案:

没有答案