簡體   English   中英

從Tensorflow張量獲取特定的索引

[英]Get Specific Indices from a Tensorflow Tensor

我正在嘗試使用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])

我正在嘗試使用以下代碼段測試實現:

>>> 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)

執行測試代碼段后,出現以下錯誤:

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.

如何在不啟用急切執行的情況下修改該層的實現以使其起作用?

假設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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM