簡體   English   中英

CIFAR-10 TensorFlow:InvalidArgumentError(請參閱上面的回溯):logits和標簽必須是可廣播的

[英]CIFAR-10 TensorFlow: InvalidArgumentError (see above for traceback): logits and labels must be broadcastable

我正在實現CNN,如下所示,但出現此錯誤:

InvalidArgumentError(請參閱上面的回溯):登錄信息和標簽必須可廣播

我在下面附加了我的部分代碼。 我懷疑錯誤是由於我的體重和偏見的形狀和尺寸引起的。

我要實現的目標-我想將CNN層從兩個完全連接的層減少到僅一個完全連接的層,這意味着out=tf.add(tf.add(fc1....)並將其停在那里。

nInput = 32
nChannels = 3
nClasses = 10

# Placeholder and drop-out
X = tf.placeholder(tf.float32, [None, nInput, nInput, nChannels])
Y = tf.placeholder(tf.float32, [None, nClasses])
keep_prob = tf.placeholder(tf.float32)

def conv2d(x, W, b, strides=1):
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)


def maxpool2d(x, k=2):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')


def normalize_layer(pooling):
    #norm = tf.nn.lrn(pooling, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1')
    norm = tf.contrib.layers.batch_norm(pooling, center=True, scale=True)
    return norm


def drop_out(fc, keep_prob=0.4):
    drop_out = tf.layers.dropout(fc, rate=keep_prob)
    return drop_out


weights = {
    'WC1': tf.Variable(tf.random_normal([5, 5, 3, 32]), name='W0'),
    'WC2': tf.Variable(tf.random_normal([5*5*32, 64]), name='W1'),
    #'WD1': tf.Variable(tf.random_normal([8 * 8 * 64, 64]), name='W2'),
    #'WD2': tf.Variable(tf.random_normal([64, 128]), name='W3'),
    'out': tf.Variable(tf.random_normal([64, nClasses]), name='W5')
}

biases = {
    'BC1': tf.Variable(tf.random_normal([32]), name='B0'),
    'BC2': tf.Variable(tf.random_normal([64]), name='B1'),
    #'BD1': tf.Variable(tf.random_normal([64]), name='B2'),
    #'BD2': tf.Variable(tf.random_normal([128]), name='B3'),
    'out': tf.Variable(tf.random_normal([nClasses]), name='B5')
}

def conv_net(x, weights, biases):
    conv1 = conv2d(x, weights['WC1'], biases['BC1'])
    conv1 = maxpool2d(conv1)
    conv1 = normalize_layer(conv1)

    #conv2 = conv2d(conv1, weights['WC2'], biases['BC2'])
    #conv2 = maxpool2d(conv2)
    #conv2 = normalize_layer(conv2)

    fc1 = tf.reshape(conv1, [-1, weights['WC2'].get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, weights['WC2']), biases['BC2'])
    fc1 = tf.nn.relu(fc1)  # Using self-normalization activation
    fc1 = drop_out(fc1)

    #fc2 = tf.add(tf.matmul(fc1, weights['WD2']), biases['BD2'])
    #fc2 = tf.nn.selu(fc2)  # Using self-normalization activation
    #fc2 = drop_out(fc2)

    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
    out = tf.nn.softmax(out)

    return out

我認為權重字典的“ WC2”參數有問題。 它應該是'WC2': tf.Variable(tf.random_normal([16*16*32, 64]), name='W1')

應用1卷積和最大池合並操作后,您將輸入圖像從32 x 32 x 3下采樣到16 x 16 x 3 ,現在需要展平該下采樣的輸出,以將其作為輸入輸入到完全連接的層。 這就是為什么您需要傳遞16*16*32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM