Tensorflow无法确定张量的大小为何在变化,并且无法直接调试

时间:2018-07-21 23:21:38

标签: python tensorflow neural-network conv-neural-network

我正在使用张量流创建卷积神经网络类,并遇到一个错误,在优化过程中,一个张量应具有40000个元素,而原本应该只有10000个。问题出现在行中:

correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(output, 1))

给出错误:

InvalidArgumentError (see above for traceback): Incompatible shapes: [10000] vs. [40000]
 [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ArgMax, ArgMax_1)]]

所以基本上说我的网络输出比我提供的测试标签大4倍。我的问题是,随着输出张量的变化,如何查看它的大小?似乎所有事情都通过单行代码session.run('model-name')在幕后发生,这使我无法对其进行调试。我如何查看内部情况并弄清楚发生了什么?

这是完整的代码:

class ConvNet:
    def __init__(self, epochs=1, learning_rate=0.01, batch_size=50):
        self.learning_rate = learning_rate
        self.epochs = epochs
        self.batch_size = batch_size

    self.x = tf.placeholder(tf.float32, [None, 784])
    self.x_shaped = tf.reshape(self.x, [-1, 28, 28, 1])

    self.y = tf.placeholder(tf.float32, [None, 10])

    self.accuracy = None

def predict(self, x_test, y_test):
    with tf.Session() as sess:
        new_saver = tf.train.import_meta_graph('convnet-model.meta')
        new_saver.restore(sess, tf.train.latest_checkpoint('./'))
        test_acc = sess.run(self.accuracy, feed_dict={self.x: x_test, self.y: y_test})
        print('test accuracy: {:.3f}'.format(test_acc))

def train(self, x_train, y_train):
    output, raw_output = self.create_structure()
    self.optimize(raw_output, output, x_train, y_train)

def create_structure(self):

    output = self.conv_layer(self.x_shaped, 1, 32, [5, 5], 'conv1')
    output = self.relu_layer(output)
    output = self.conv_layer(output, 32, 64, [5, 5], 'conv2')
    output = self.relu_layer(output)
    output = self.max_pool_layer(output, [2, 2])
    raw_output, output = self.full_connect_layers(output)

    return output, raw_output

def optimize(self, raw_output, output, x_train, y_train):

    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=raw_output, labels=output))
    optimiser = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(cross_entropy)

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

    init_op = tf.global_variables_initializer()

    with tf.Session() as sess:

        sess.run(init_op)
        total_batch = int(len(y_train) / self.batch_size)
        for epoch in range(self.epochs):

            for i in range(total_batch):

                batch_x, batch_y = self.get_batch(x_train, y_train) 
                _, c = sess.run([optimiser, cross_entropy], feed_dict={self.x: batch_x, self.y: batch_y})

        saver = tf.train.Saver()
        saver.save(sess, './convnet-model')

def conv_layer(self, input_data, num_input_channels, num_filters, filter_shape, name):

    # setup the filter shape for tf.nn.conv2d
    conv_filter_shape = [filter_shape[0], filter_shape[1], num_input_channels, num_filters]

    # init weights and bias for the filter
    weights = tf.Variable(tf.truncated_normal(conv_filter_shape, stddev=0.03), name=name+'_w')
    bias = tf.Variable(tf.truncated_normal([num_filters]), name=name+'_b')

    # set up the convolutional layer operation
    out_layer = tf.nn.conv2d(input_data, weights, [1, 1, 1, 1], padding='SAME')

    # add the bias
    out_layer += bias

    return out_layer

def relu_layer(self, out_layer):

    return tf.nn.relu(out_layer)

def max_pool_layer(self, out_layer, pool_shape):

    ksize = [1, pool_shape[0], pool_shape[1], 1]
    strides = [1, 2, 2, 1]
    out_layer = tf.nn.max_pool(out_layer, ksize=ksize, strides=strides, padding='SAME')

    return out_layer

def full_connect_layers(self, output):

    flattened = tf.reshape(output, [-1, 7 * 7 * 64])

    w1 = tf.Variable(tf.truncated_normal([7*7*64, 1000], stddev=0.03), name='w1')
    b1 = tf.Variable(tf.truncated_normal([1000], stddev=0.01), name='b1')

    dense_layer1 = tf.matmul(flattened, w1) + b1
    dense_layer1 = tf.nn.relu(dense_layer1)

    w2 = tf.Variable(tf.truncated_normal([1000, 10], stddev=0.03), name='w2')
    b2 = tf.Variable(tf.truncated_normal([10], stddev=0.01), name='b2')

    dense_layer2 = tf.matmul(dense_layer1, w2) + b2
    y = tf.nn.softmax(dense_layer2)
    return dense_layer2, y

def get_batch(self, features, labels):
    index_list = np.arange(0, len(labels))
    np.random.shuffle(index_list)
    new_features = np.asarray([features[i] for i in index_list])
    new_labels = np.asarray([labels[i] for i in index_list])

    return new_features[0:self.batch_size], new_labels[0:self.batch_size]

主文件:

from convnet import ConvNet
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

convnet = ConvNet()
convnet.train(mnist.test.images, mnist.test.labels)
convnet.predict(mnist.test.images, mnist.test.labels)

0 个答案:

没有答案