Tensorflow卷积网络错误:无效参数:logits和labels必须大小相同:logits_size=[512,4] labels_size=[128,4]

我基于这里找到的5_convolutional_net.py示例制作了一个卷积网络:https://github.com/nlintz/TensorFlow-Tutorials。我尝试对国际象棋棋子进行分类。我加载了我的图片:每种棋子有1136张60×60的灰度图像。我将它们分为训练和测试图像,为每种棋子制作了热向量,并将它们合并。因此,我的testimages.shape=(40,60,60),testlabels.shape=(40,4),trainimages.shape=(4504,60,60),trainlabels.shape=(4504,4)。4504=4*(1136-10)

#!/usr/bin/env pythonfrom os import listdirfrom os.path import isfile, joinimport tensorflow as tfimport numpy as np# import input_dataimport cv2def init_weights(shape):    return tf.Variable(tf.random_normal(shape, stddev=0.01))def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):    l1a = tf.nn.relu(tf.nn.conv2d(X, w,                       # l1a shape=(?, 28, 28, 32)                        strides=[1, 1, 1, 1], padding='SAME'))    l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],              # l1 shape=(?, 14, 14, 32)                        strides=[1, 2, 2, 1], padding='SAME')    l1 = tf.nn.dropout(l1, p_keep_conv)    l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,                     # l2a shape=(?, 14, 14, 64)                        strides=[1, 1, 1, 1], padding='SAME'))    l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],              # l2 shape=(?, 7, 7, 64)                        strides=[1, 2, 2, 1], padding='SAME')    l2 = tf.nn.dropout(l2, p_keep_conv)    l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,                     # l3a shape=(?, 7, 7, 128)                        strides=[1, 1, 1, 1], padding='SAME'))    l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],              # l3 shape=(?, 4, 4, 128)                        strides=[1, 2, 2, 1], padding='SAME')    l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])    # reshape to (?, 2048)    l3 = tf.nn.dropout(l3, p_keep_conv)    l4 = tf.nn.relu(tf.matmul(l3, w4))    l4 = tf.nn.dropout(l4, p_keep_hidden)    pyx = tf.matmul(l4, w_o)    return pyxdef add_images(folder,lista):    onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]    for file in onlyfiles:        img = cv2.imread(mypath + file, 0)  # 60x60 numpy ndarray        lista.append(img)    return listatrainimages = []testimages = []folders=['TRAININGIMAGES/bw/rooks/','TRAININGIMAGES/bw/knights/','TRAININGIMAGES/bw/bishops/','TRAININGIMAGES/bw/pawns/']for folder in folders:    print ( folder)    onlyfiles = [f for f in listdir(folder) if isfile(join(folder, f))]    images = []    for file in onlyfiles:        img = cv2.imread(folder + file, 0)  # 60x60 numpy ndarray        images.append(img)    trainimages.extend(images[10:])    testimages.extend(images[:10])size=len(onlyfiles)trainlabels = []testlabels = []rook_label   = np.array([0, 0, 0, 1], dtype=bool)bishop_label = np.array([0, 0, 1, 0], dtype=bool)pawn_label   = np.array([0, 1, 0, 0], dtype=bool)knight_label = np.array([1, 0, 0, 0], dtype=bool)hotvectors = [rook_label,pawn_label,knight_label,bishop_label]for label in hotvectors:    labels=[]    for x in range(size):        labels.append(label)    trainlabels.extend(labels[10:])    testlabels.extend(labels[:10])trainimages = np.asarray(trainimages)  # shape : (4544,60,60)testimages = np.asarray(testimages)trainlabels = np.asarray(trainlabels)testlabels = np.asarray(testlabels)trainimages=trainimages.reshape(-1,60,60,1)testimages=testimages.reshape(-1,60,60,1)X = tf.placeholder("float", [None, 60, 60, 1])Y = tf.placeholder("float", [None, 4])w = init_weights([3, 3, 1, 32])       # 3x3x1 conv, 32 outputsw2 = init_weights([3, 3, 32, 64])     # 3x3x32 conv, 64 outputsw3 = init_weights([3, 3, 64, 128])    # 3x3x32 conv, 128 outputsw4 = init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputsw_o = init_weights([625, 4])         # FC 625 inputs, 10 outputs (labels)p_keep_conv = tf.placeholder("float")p_keep_hidden = tf.placeholder("float")py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)predict_op = tf.argmax(py_x, 1)with tf.Session() as sess:    # you need to initialize all variables    tf.initialize_all_variables().run()    for i in range(100):        for start, end in zip(range(0, len(trainimages), 128), range(128, len(trainimages), 128)):            sess.run(train_op, feed_dict={X: trainimages[start:end], Y: trainlabels[start:end],                                          p_keep_conv: 0.8, p_keep_hidden: 0.5})        test_indices = np.arange(len(testimages))  # Get A Test Batch        np.random.shuffle(test_indices)        test_indices = test_indices[0:256]        print(i, np.mean(np.argmax(testlabels[test_indices], axis=1) ==                         sess.run(predict_op, feed_dict={X: testimages[test_indices],                                                         Y: testlabels[test_indices],                                                         p_keep_conv: 1.0,                                                         p_keep_hidden: 1.0})))

当我运行脚本时,在第100行时得到了以下错误:

tensorflow.python.framework.errors.InvalidArgumentError: logits和labels必须大小相同:logits_size=[512,4] labels_size=[128,4]     [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MatMul_1, _recv_Placeholder_1_0)]]Caused by op 'SoftmaxCrossEntropyWithLogits', defined at:  File "/home/matyi/OneDrive/PYTHON/PYTHON3/chess_vision/5_convolutional_net_chess.py", line 100, in <module>    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))

我也不明白第108行的128的作用。你能帮我解答吗?

图像示例:enter image description hereenter image description here


回答:

由于你输入的是60x60x1的图像,你的张量形状将是这些:

Tensor("Relu:0", shape=(?, 60, 60, 32), dtype=float32)Tensor("MaxPool:0", shape=(?, 30, 30, 32), dtype=float32)Tensor("Relu_1:0", shape=(?, 30, 30, 64), dtype=float32)Tensor("MaxPool_1:0", shape=(?, 15, 15, 64), dtype=float32)Tensor("Relu_2:0", shape=(?, 15, 15, 128), dtype=float32)Tensor("MaxPool_2:0", shape=(?, 8, 8, 128), dtype=float32)

所以你的最后一个权重w4应该是:

w4 = init_weights([128 * 8 * 8, 625])

我们先尝试这个更改。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注