Python 3.x TensorFlow 2.0中lambda函数的合成

Python 3.x TensorFlow 2.0中lambda函数的合成,python-3.x,tensorflow,tensorflow2.0,Python 3.x,Tensorflow,Tensorflow2.0,为了在TensorFlow 2.0中构建一个大模型,我使用了一种函数方法,并利用了Python 3.6的functools模块。我通过显示特定自定义层的代码来说明这个问题 import tensorflow as tf import functools from collections.abc import Iterable # TODO Check for correctness of the model implementation class Unit3D(tf.keras.layer

为了在TensorFlow 2.0中构建一个大模型,我使用了一种函数方法,并利用了Python 3.6的
functools
模块。我通过显示特定自定义层的代码来说明这个问题

import tensorflow as tf
import functools
from collections.abc import Iterable


# TODO Check for correctness of the model implementation
class Unit3D(tf.keras.layers.Layer):
    def __init__(self, output_channels,
                 kernel_shape=(1, 1, 1),
                 stride=(1, 1, 1),
                 activation_fn='relu',
                 use_batch_norm=True,
                 use_bias=False,
                 is_training=False,
                 name='unit_3d'):
        super(Unit3D, self).__init__(name=name)
        self._output_channels = output_channels
        self._kernel_shape = kernel_shape
        self._stride = stride
        self._activation = activation_fn
        self._use_batch_norm = use_batch_norm
        self._use_bias = use_bias
        self._is_training = is_training
        self._pipeline = []
        self._pipeline.append(tf.keras.layers.Conv3D(
            filters=self._output_channels,
            kernel_size=self._kernel_shape,
            strides=self._stride,
            padding='same',
            use_bias=self._use_bias,
            data_format='channels_first'
        )
        )
        if self._use_batch_norm:
            bn = tf.keras.layers.BatchNormalization(
                axis=1,
                fused=False,
            )
            bn = functools.partial(bn, training=self._is_training)
            self._pipeline.append(bn)

        if self._activation is not None:
            self._pipeline.append(tf.keras.layers.Activation(
                activation=self._activation
            )
            )

        print(isinstance(self._pipeline, Iterable))
        print(type(self._pipeline))
        self._pipeline = lambda x: functools.reduce(lambda f, g: g(f), self._pipeline, x)

    def call(self, input):
        return self._pipeline(input)
当使用以下代码进行测试时,它返回

TypeError:reduce()arg 2必须支持迭代

该错误与
\uuu init\uuu
方法中的
self.\u管道
中函数的组合有关

import tensorflow as tf
from nets.i3d import Unit3D

model = Unit3D(output_channels=64, kernel_shape=[7,7,7],
               is_training=True)

input = tf.keras.backend.random_uniform(shape=(1,3,64,224,224),
                                        dtype=tf.float32)
output = model(input)
在TensorFlow 2.0中,在急切执行期间,所有列表都被包装在称为

事实证明,这种数据结构是可移植的。我在
collections.abc
模块中使用Iterable类对其进行了测试

我无法理解这段代码的问题,也不确定这是TensorFlow 2.0中的内部问题,还是我这里缺少的基本问题


如果有帮助的话,我正在使用从
r2.0
分支的源代码编译的tensorflow版本
2.0.0-beta1
。相应的git散列是
8e423e3d56390671f0d954c90f4fd163ab02a9c1

我认为您的问题与Tensorflow无关

在这方面:

self._pipeline = lambda x: functools.reduce(lambda f, g: g(f), self._pipeline, x)
您正在使用函数覆盖先前在构造函数中创建的列表。因此,在lambda的实际执行过程中(特别是传递给reduce的lambda),它只是一个
函数

如果为了调试目的将外部lambda函数重构为常规函数,则会变得更加明显:

    def debug_func(x):
        print(type(self._pipeline))  # prints <class 'function'>
        return functools.reduce(lambda f, g: g(f), self._pipeline, x)

    self._pipeline = debug_func  # Clearly, no longer a list
    # ...
    self._pipeline_func = lambda x: functools.reduce(lambda f, g: g(f), self._pipeline, x)

def call(self, input):
    return self._pipeline_func(input)