形状必须为4级,但为5级

2024-04-26 01:17:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我是麦克斯在紧张状态下拼搏。但是我不需要使用内置的tf.nn.max_pool(),而是必须使用tf.reduce_max()。但它给出了一个错误:

ValueError: Shape must be rank 4 but is rank 5 for 'conv2_1/Conv2D' (op: 'Conv2D') with input shapes: [1,?,1,224,64], [3,3,64,128].

下面是代码:

 with tf.name_scope('conv1_2') as scope:
        kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64], dtype=tf.float32,
                                                 stddev=1e-1), name='weights')
        conv = tf.nn.conv2d(self.conv1_1, kernel, [1, 1, 1, 1], padding='SAME')
        biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32),
                             trainable=True, name='biases')
        out = tf.nn.bias_add(conv, biases)
        self.conv1_2 = tf.nn.relu(out, name=scope)
        self.parameters += [kernel, biases]

 self.pool1=tf.reduce_max(self.conv1_2,reduction_indices=[1], keep_dims=True)


        # conv2_1
        with tf.name_scope('conv2_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32,
                                                     stddev=1e-1), name='weights')
        sess = tf.InteractiveSession()
        tf.Print(self.pool1,[self.pool1],message="hellow fatima")
            conv = tf.nn.conv2d([self.pool1], kernel, [1, 1, 1, 1], padding='SAME')
            biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32),
                                 trainable=True, name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv2_1 = tf.nn.relu(out, name=scope)
            self.parameters += [kernel, biases]

Tags: nameselftfnnoutvariablekernelscope
1条回答
网友
1楼 · 发布于 2024-04-26 01:17:18

您可以尝试删除传入tf.nn.conv2d的第一个参数中的[]。所以,与其conv = tf.nn.conv2d([self.pool1], kernel, [1, 1, 1, 1], padding='SAME'),不如试试这个:conv = tf.nn.conv2d(self.pool1, kernel, [1, 1, 1, 1], padding='SAME')

相关问题 更多 >