machine-learning - CNN - 将输出从Conv层 reshape 为dense层

标签 machine-learning tensorflow computer-vision deep-learning

我的卷积层的输出形状为 (64,3,3,80),其中 64 是批量大小。下一层是形状密集的层(3920,4096)。如何 reshape 我的卷积层的输出以适应我的密集层的形状?我正在 tensorflow 中实现:) 这是致密层之前的层。

    stride_conv = [1,1,1,1] 
    padding='SAME'
    filter_3 = tf.Variable(initial_value=tf.random_normal([3,3,112,80]))
    conv_3 = tf.nn.conv2d(conv_2,filter_3,stride_conv,padding)

谢谢!

最佳答案

conv3 => reshape => FC1 (720->4096)

[64,3,3,80] => [64,720] => [64,4096]

以下代码将转换为 FC,如上所示:

 shape = int(np.prod(conv_3.get_shape()[1:]))
 conv_3_flat = tf.reshape(conv_3, [-1, shape])

 fc1w = tf.Variable(tf.truncated_normal([shape, 4096],dtype=tf.float32,stddev=1e-1), name='weights')
 fc1b = tf.Variable(tf.constant(1.0, shape=[4096], dtype=tf.float32),
                                 trainable=True, name='biases')

 fc1 = tf.nn.bias_add(tf.matmul(conv_3_flat, fc1w), fc1b)
 fc1 = tf.nn.relu(fc1)

希望这有帮助。

此外,简单的 MNIST 模型(取自此处:https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/convolutional_network.py)

def conv_net(x, weights, biases, dropout):
    # Reshape input picture
    x = tf.reshape(x, shape=[-1, 28, 28, 1])

    # Convolution Layer
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    # Max Pooling (down-sampling)
    conv1 = maxpool2d(conv1, k=2)

    # Convolution Layer
    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    # Max Pooling (down-sampling)
    conv2 = maxpool2d(conv2, k=2)

    # Fully connected layer
    # Reshape conv2 output to fit fully connected layer input
    fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
    fc1 = tf.nn.relu(fc1)
    # Apply Dropout
    fc1 = tf.nn.dropout(fc1, dropout)

    # Output, class prediction
    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
    return out

关于machine-learning - CNN - 将输出从Conv层 reshape 为dense层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44705752/

相关文章:

machine-learning - 在多 channel 图像数据集上训练卷积网络

computer-vision - 计算机视觉 - 视频中的人体检测

machine-learning - Ngram IDF 平滑

tensorflow - cond 可以支持有副作用的 TF 操作吗?

python - 如何在张量 session 中在一行中同时运行多个操作?

python - 如何使用 mlflow.tensorflow.log_model 记录 tensorflow 模型(错误模块 'tensorflow._api.v2.saved_model' 没有属性 'tag_constants' )

machine-learning - 结合训练数据和验证数据,如何选择超参数?

python - 如何计算对象关键点相似度

python - keras中的全梯度下降

python - 改进网络摄像头代码的建议