Tensorflow登录和标签必须是可广播的

时间:2019-01-07 16:44:24

标签: python-3.x tensorflow jupyter-notebook

我在Tensorflow上工作非常环保,似乎无法克服此错误。我在两天内一直无法解决此错误,而且无法正常工作。谁能看到代码问题?我正在通过Jupyter Notebook使用python3。感谢您的协助。

这是我的代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("official/MNIST_data/", one_hot=True)
Extracting official/MNIST_data/train-images-idx3-ubyte.gz
Extracting official/MNIST_data/train-labels-idx1-ubyte.gz
Extracting official/MNIST_data/t10k-images-idx3-ubyte.gz
Extracting official/MNIST_data/t10k-labels-idx1-ubyte.gz

type(mnist)
tensorflow.contrib.learn.python.learn.datasets.base.Datasets

mnist.train.num_examples
55000

mnist.test.num_examples
10000


Preparation for building CNN model: define supporting Functions
Initialize weights in Filter

def initialize_weights (filter_shape):
    init_random_dist = tf.truncated_normal(filter_shape, stddev=.1)
    return (tf.Variable(init_random_dist))

def initialize_bias(bias_shape):
    initial_bias_vals = tf.constant(.1, shape=bias_shape)
    return(tf.Variable(initial_bias_vals))

def create_convolution_layer_and_compute_dot_product(inputs, filter_shape):
    filter_initialized_with_weights = initialize_weights(filter_shape)
    conv_layer_outputs = tf.nn.conv2d(inputs, filter_initialized_with_weights, strides = [1,1,1,1], padding = 'SAME')
    return(conv_layer_outputs)

def create_relu_layer_and_compute_dotproduct_plus_b(inputs, filter_shape):
    b = initialize_bias([filter_shape[3]])
    relu_layer_outputs = tf.nn.relu(inputs + b)
    return (relu_layer_outputs)

def create_maxpool2by2_and_reduce_spatial_size(inputs):
    pooling_layer_outputs = tf.nn.max_pool(inputs, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
    return(pooling_layer_outputs)

def create_fully_conected_layer_and_compute_dotproduct_plus_bias(inputs, output_size):
    input_size = int(inputs.get_shape()[1])
    W = initialize_weights([input_size, output_size])
    b = initialize_bias([output_size])
    fc_xW_plus_bias_outputs = tf.matmul(inputs, W) + b
    return(fc_xW_plus_bias_outputs)


Build the Convolutional Neural Network

x = tf.placeholder(tf.float32, shape = [None, 784])
y_true = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1,28,28,1])

conv_layer_1_outputs \
    = create_convolution_layer_and_compute_dot_product(x_image, filter_shape=[5,5,1,32])
conv_relu_layer_1_outputs \
    = create_relu_layer_and_compute_dotproduct_plus_b(conv_layer_1_outputs, filter_shape=[5,5,1,32])

pooling_layer_1_ouptuts = create_maxpool2by2_and_reduce_spatial_size(conv_relu_layer_1_outputs)

conv_layer_2_outputs \
    = create_convolution_layer_and_compute_dot_product(conv_layer_1_outputs, filter_shape=[5,5,32,64])
conv_relu_layer_2_outputs \
    = create_relu_layer_and_compute_dotproduct_plus_b(conv_layer_2_outputs, filter_shape=[5,5,32,64])

pooling_layer_2_outputs = create_maxpool2by2_and_reduce_spatial_size(conv_relu_layer_2_outputs)

pooling_layer_2_outputs_flat=tf.reshape(pooling_layer_2_outputs, [-1,7*7*64])

fc_layer_1_outputs \
    = create_fully_conected_layer_and_compute_dotproduct_plus_bias(pooling_layer_2_outputs_flat, output_size=1024)
fc_relu_layer_1_outputs = tf.nn.relu(fc_layer_1_outputs)

hold_prob = tf.placeholder(tf.float32)
fc_dropout_outputs = tf.nn.dropout(fc_layer_1_outputs, keep_prob=hold_prob)

y_pred = create_fully_conected_layer_and_compute_dotproduct_plus_bias(fc_dropout_outputs, output_size=10)

    softmax_cross_entropy_loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true, logits=y_pred)
    cross_entropy_mean = tf.reduce_mean(softmax_cross_entropy_loss)

    optimizer = tf.train.AdamOptimizer(learning_rate=.001)

    cnn_trainer = optimizer.minimize(cross_entropy_mean)

    vars_initializer = tf.global_variables_initializer()

    steps = 5000


Run tf.session to train and test deep learning CNN model

with tf.Session() as sess:
    sess.run(vars_initializer)
    for i in range(steps):
        batch_x, batch_y = mnist.train.next_batch(50)
        sess.run(cnn_trainer, feed_dict={x: batch_x, y_true: batch_y, hold_prob: .5})

        if i % 100 == 0:
            print('ON STEP: {}', format(i))
            print('ACCURACY: ')
            matches = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))
            acc = tf.reduce_mean(tf.cast(matches, tf.float32))
            test_accuracy = sess.run(acc, feed_dict = {x: mnist.test.images, y_true: mnist.test.labels, hold_prob: 1.0})
            print(test_accuracy)
            print('\n')

这是确切的错误消息:

InvalidArgumentError: logits and labels must be broadcastable: logits_size=[200,10] labels_size=[50,10]
     [[node softmax_cross_entropy_with_logits_7 (defined at <ipython-input-162-3d06fe78186c>:1)  = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](add_31, softmax_cross_entropy_with_logits_7/Reshape_1)]]

1 个答案:

答案 0 :(得分:0)

将其发布,以防其他人遇到类似问题。

该错误应显示为“ 哑用户”。我将错误的变量传递给第二层。

pooling_layer_1_ouptuts = create_maxpool2by2_and_reduce_spatial_size(conv_relu_layer_1_outputs)

conv_layer_2_outputs \
    = create_convolution_layer_and_compute_dot_product(conv_layer_1_outputs, filter_shape=[5,5,32,64])

应为:

pooling_layer_1_ouptuts = create_maxpool2by2_and_reduce_spatial_size(conv_relu_layer_1_outputs)

conv_layer_2_outputs \
    = create_convolution_layer_and_compute_dot_product(pooling_layer_1_ouptuts , filter_shape=[5,5,32,64])