TensorFlow ValueError:等级不匹配错误

时间:2019-05-21 17:32:52

标签: tensorflow

所有,由于使用卷积网络对猫与狗进行分类的代码中的错误,我完全陷入了困境。我可以使用这些天可用的高级库,但是为了学习,我想让这个较低级的库工作。输出是包含猫或狗的图像的二进制分类。我已经扫描了许多与Rank相关的线程,但无法明确使用sparse_softmax_cross_entropy_with_logits解决该错误。

如果我更改2行;使用softmax_cross_entropy_with_logits_v2()并取消注释标签= tf.argmax(y,1),它便会运行,但即使在火车上,其准确度也会迅速降低(净偏差)。

任何帮助将不胜感激。谢谢。

我不确定100%的2行如下。

这里的1应该是n_outputs(即2)吗? (因为是二进制文件,但似乎不正确)

y = tf.placeholder(dtype=tf.int64, shape=[100, 1], name="y")

这是引发错误的行:ValueError:等级不匹配:标签的等级(接收到2)应等于logits等级减去1(接收到2)。

xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(\
                                   labels=y, logits=logits)

完整代码(从拥有数据的角度出发)如下所示;它长久以来很简单。我已经注释掉了结尾,因为错误是在错误到达之前抛出的。错误在下面的末尾。

#---Split data into training & test sets---

# Work the data for cats and dogs numpy arrays 
# These numpy arrays were generated in previous data prep work
# Stack the numpy arrays for the inputs
X_cat_dog = np.concatenate((cats_1000_64_64_1, dogs_1000_64_64_1), 
                           axis = 0) 
X_cat_dog = X_cat_dog.reshape(-1, width*height) #Flatten

# Scikit Learn for min-max scaling of the data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(np.array([0., 255.]).reshape(-1,1))
X_cat_dog_scaled = scaler.transform(X_cat_dog)

# Define the labels to be used: cats = 0, dogs = 1
y_cat_dog = np.concatenate((np.zeros((1000), dtype = np.int32), 
                            np.ones((1000), dtype = np.int32)), 
                           axis = 0)

# Scikit Learn for random splitting of the data  
from sklearn.model_selection import train_test_split

# Random split of data into training (80%) and test (20%)  
X_train, X_test, y_train, y_test = \
    train_test_split(X_cat_dog_scaled, y_cat_dog, test_size=0.20, 
                     random_state = RANDOM_SEED)
print('Train orig. shape:', X_train.shape, y_train.shape)
print('Test orig. shape:', X_test.shape, y_test.shape)

#Reshape into 4D
X_train = np.reshape(X_train, newshape=[X_train.shape[0], height, width, channels])
y_train = np.reshape(y_train, newshape=[y_train.shape[0], 1])
X_test = np.reshape(X_test, newshape=[X_test.shape[0], height, width, channels])
y_test = np.reshape(y_test, newshape=[y_test.shape[0], 1])
print('Train 4D shape:', X_train.shape, y_train.shape, type(X_train), type(y_train))
print('Test 4D shape:', X_test.shape, y_test.shape, type(X_test), type(y_test))

#---Define and run convolution net---

#Init
results = []  #Summary results
reset_graph()  #Else upon rerun, error occurs
n_outputs = 2  #Binary; cat or dog
n_strides = [1,2,2]  #Symmetric XY + same across conv & pool
n_conv_blocks = 1  #Number of convolution blocks
n_filters = [5, 10, 20]  #Number of filters applied per layer

#Placeholders for batch training
X = tf.placeholder(dtype=tf.float64, 
                   shape=[100, height, width, channels], name="X")
y = tf.placeholder(dtype=tf.int64, shape=[100, 1], name="y")
print('X.shape =', X.shape, tf.rank(X))
print('y.shape =', y.shape, tf.rank(y))

#Define hidden layers
with tf.name_scope("cnn"):

    #Create number of convolution blocks required
    for block in range(n_conv_blocks):

        #Convolution layer
        inputLayer = X
        if (block>0):
            inputLayer = pool
        print('\nStride:', n_strides[block])
        conv = tf.layers.conv2d(inputLayer, 
                                filters = n_filters[block], 
                                kernel_size = 1, 
                                strides = n_strides[block], 
                                activation = tf.nn.leaky_relu, 
                                padding = "SAME")
        print('Conv '+str(block)+'.shape =', 
              conv.get_shape().as_list())

        #Pooling layer
        pool = tf.nn.avg_pool(conv, 
                 ksize = [1,2,2,1], 
                 strides = [1,n_strides[block],n_strides[block],1], 
                 padding = "SAME")
        print('Pool '+str(block)+'.shape =', pool.shape)
        pool_shape = pool.get_shape().as_list()
        next_width = pool_shape[1]
        next_height = pool_shape[2]
        next_depth = pool_shape[3]
    #Fully connected
    flattened = tf.reshape(pool, [-1, 
                           next_width * next_height * next_depth])
    print('\nFlattened.shape =', flattened.shape)
    hidden = tf.layers.dense(flattened, 
                           next_width * next_height * next_depth, 
                           name="hidden1",
                           activation=tf.nn.leaky_relu)
    print('\nHidden.shape =', hidden.shape, tf.rank(hidden))
    #Output
    logits = tf.layers.dense(hidden, n_outputs, name="outputs")
    print('\nLogits.shape =', logits.shape, tf.rank(logits))

#Define loss function
with tf.name_scope("loss"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(\
                                       labels=y, logits=logits)
    loss = tf.reduce_mean(xentropy, name="loss")

#Define optimizer used for reducing the loss; MomentumOptimizer
learning_rate = 0.01
momentum = 0.01
with tf.name_scope("train"):
    optimizer = tf.train.MomentumOptimizer(learning_rate, 
                                           momentum)
    training_op = optimizer.minimize(loss)

#Define performance measure; accuracy in this case
with tf.name_scope("eval"):
    #labels = tf.argmax(y, 1)
    labels = y
    correct = tf.nn.in_top_k(logits, labels, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

#Define instantiator for TensorFlow variables
init = tf.global_variables_initializer()

#Carry out training in mini-batches
n_epochs = 1
batch_size = 100
with tf.Session() as sess:
    #Instantiate variables
    init.run()

    #Loop over n_epochs
    for epoch in range(n_epochs):

        #Loop over batches
        for iteration in range(y_train.shape[0] // batch_size):
            X_batch = X_train[\
              iteration*batch_size:(iteration + 1)*batch_size,:]
            y_batch = y_train[\
              iteration*batch_size:(iteration + 1)*batch_size]
            print(y_batch.shape, type(y_batch))
#             sess.run(training_op, feed_dict={X: X_batch, 
#                                              y: y_batch})

#         #Measure performance
#         acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
#         acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})
#         if (epoch % 1 == 0):
#             print(epoch, 
#                   "Train Accuracy:", 
#                   '{:0.1%}'.format(acc_train),
#                   "\tTest Accuracy:", 
#                   '{:0.1%}'.format(acc_test))
#         results.append([epoch, acc_train, acc_test])

错误如下。

X.shape = (100, 64, 64, 1) Tensor("Rank:0", shape=(), dtype=int32)
y.shape = (100, 1) Tensor("Rank_1:0", shape=(), dtype=int32)

Stride: 1
Conv 0.shape = [100, 64, 64, 5]
Pool 0.shape = (100, 64, 64, 5)

Flattened.shape = (100, 20480)

Hidden.shape = (100, 20480) Tensor("cnn/Rank:0", shape=(), dtype=int32)

Logits.shape = (100, 2) Tensor("cnn/Rank_1:0", shape=(), dtype=int32)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-7961eb9a772c> in <module>()
     58 #Define loss function
     59 with tf.name_scope("loss"):
---> 60     xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(                                       labels=y, logits=logits)
     61     loss = tf.reduce_mean(xentropy, name="loss")
     62 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py in sparse_softmax_cross_entropy_with_logits(_sentinel, labels, logits, name)
   2645       raise ValueError("Rank mismatch: Rank of labels (received %s) should "
   2646                        "equal rank of logits minus 1 (received %s)." %
-> 2647                        (labels_static_shape.ndims, logits.get_shape().ndims))
   2648     if (static_shapes_fully_defined and
   2649         labels_static_shape != logits.get_shape()[:-1]):

ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).

1 个答案:

答案 0 :(得分:0)

好的,我知道了。我调整了以下两行。

  1. 我从y形状中减去了多余的尺寸,如下所示。

    y = tf.placeholder(dtype = tf.int64,shape = [None],name =“ y”)

  2. 定义之后,所有对y_batch的引用都被y_batch.reshape(-1)取代。需要这样做以降低y_batch的等级以消除错误。

其余部分保持不变。现在,我遇到了一个新问题,准确性仍然很低,但至少现在它表现出来了(并且不会为零)。游戏时间!

0 Train Accuracy: 61.0%     Test Accuracy: 48.5%
1 Train Accuracy: 60.0%     Test Accuracy: 48.8%
2 Train Accuracy: 61.0%     Test Accuracy: 49.5%
3 Train Accuracy: 65.0%     Test Accuracy: 50.2%
4 Train Accuracy: 65.0%     Test Accuracy: 51.0%
5 Train Accuracy: 64.0%     Test Accuracy: 51.0%
6 Train Accuracy: 65.0%     Test Accuracy: 51.5%
7 Train Accuracy: 66.0%     Test Accuracy: 51.0%
8 Train Accuracy: 64.0%     Test Accuracy: 51.2%
9 Train Accuracy: 63.0%     Test Accuracy: 52.5%
10 Train Accuracy: 62.0%    Test Accuracy: 52.0%
11 Train Accuracy: 62.0%    Test Accuracy: 52.0%
12 Train Accuracy: 63.0%    Test Accuracy: 53.5%
13 Train Accuracy: 63.0%    Test Accuracy: 53.5%
14 Train Accuracy: 63.0%    Test Accuracy: 54.0%
15 Train Accuracy: 63.0%    Test Accuracy: 53.5%
16 Train Accuracy: 64.0%    Test Accuracy: 53.5%
17 Train Accuracy: 64.0%    Test Accuracy: 53.8%
18 Train Accuracy: 65.0%    Test Accuracy: 53.8%
19 Train Accuracy: 65.0%    Test Accuracy: 53.8%