tfp.keras.layers:如果数据格式是 'channels_first' ,我可以将网络输入形状指定为 'channels_last' 吗?

问题描述

我的输入图像的形状为 (4,128,128) ,其中 data_format 为 channels_first。在我的网络中,我使用具有Convolution3DFlipoutdata_format='channels_last' 层。当我将 data_format 更改为:

layer = tfp.layers.Convolution3DFlipout(data_format='channels_first')(input_layer)

并提供输入形状为:

from tensorflow.keras.layers import Input

def model(input_shape=(4,128),optimizer=Adam,initial_learning_rate=5e-4,loss_function=bin_crossentropy,activation_name="sigmoid",metrics=dice_coefficient):

    inputs = Input(input_shape)
    ......

我收到以下错误

TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [None,16,16384,128]. Consider casting elements to a supported type.

  1. 当层输出实际上应该是[None,128]时,为什么它返回[None,128]。有谁知道为什么在 data_format='channels_first' 中设置 Convolution3DFlipout() 会引发此错误

  2. 如果我在数据具有形状 (128,4) 时将输入形状提供为 (4,128)(这样我就不必更改认的 data_format),我错了吗?

解决方法

tfp.layers.Convolution3DFlipout()

data_format:一个字符串,channels_last 之一(默认)或 渠道第一

.

如果您将输入重塑为 (129,128,4),则无需在 tfp.layers.Convolution3DFlipout 中提供 data_format 参数。

看看下面的例子,

import tensorflow as tf
import tensorflow_probability as tfp

model = tf.keras.Sequential([
    tf.keras.layers.Reshape([128,4]),tfp.layers.Convolution3DFlipout(
        64,kernel_size=5,padding='SAME',activation=tf.nn.relu))