【问题标题】:Get Specific Indices from a Tensorflow Tensor从 Tensorflow 张量获取特定索引
【发布时间】:2019-12-18 22:12:54
【问题描述】:

我正在尝试使用tensorflow.keras 实现 BReLU 激活函数,如下所述。

以下是我为自定义层编写的代码:

class BReLU(Layer):

    def __init__(self):
        super(BReLU, self).__init__()

    def call(self, inputs):
        for i, element in enumerate(inputs):
            if i % 2 == 0:
                inputs[i] = tf.nn.relu(inputs[i])
            else:
                inputs[i] = -tf.nn.relu(-inputs[i])

我正在尝试使用以下代码 sn-p 测试实现:

>>> import warnings
>>> warnings.filterwarnings('ignore')
>>> from custom_activation import BReLU
>>> from tensorflow.keras.layers import Input
>>> from tensorflow.keras.models import Model
>>> inp = Input(shape = (128,))
>>> x = BReLU()(inp)

在执行测试 sn-p 时,我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\KIIT_Intern\.conda\envs\style_transfer\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 554, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:\Workspace\Echo\Echo\Activation\Tensorflow\custom_activation.py", line 308, in call
    for i, element in enumerate(inputs):
  File "C:\Users\KIIT_Intern\.conda\envs\style_transfer\lib\site-packages\tensorflow\python\framework\ops.py", line 442, in __iter__
    "Tensor objects are only iterable when eager execution is "
TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.

如何修改层的实现以使其在不启用 Eager Execution 的情况下工作?

【问题讨论】:

    标签: python tensorflow keras tensor activation-function


    【解决方案1】:

    假设i 指的是最后一个轴。

    def brelu(x):
    
        #get shape of X, we are interested in the last axis, which is constant
        shape = K.int_shape(x)
    
        #last axis
        dim = shape[-1]
    
        #half of the last axis (+1 if necessary)
        dim2 = dim // 2
        if dim % 2 != 0:
            dim2 += 1
    
        #multiplier will be a tensor of alternated +1 and -1
        multiplier = K.ones((dim2,))
        multiplier = K.stack([multiplier,-multiplier], axis=-1)
        if dim % 2 != 0:
            multiplier = multiplier[:-1]
    
        #adjust multiplier shape to the shape of x
        multiplier = K.reshape(multiplier, tuple(1 for _ in shape[:-1]) + (-1, ))
    
        return multiplier * tf.nn.relu(multiplier * x)
    

    在 lambda 层中使用它:

    x = Lambda(brelu)(inp)
    

    【讨论】:

    • 在 brelu 函数的倒数第二行,执行您的实现后我收到错误 TypeError: unsupported operand type(s) for +: 'generator' and 'tuple'
    • @SoumikRakshit,使用multiplier = K.reshape(multiplier, tuple(1 for _ in shape[:-1]) + (-1,))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多