我正在尝试使用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
表示最后一个轴在lambda层中使用:
相关问题 更多 >
编程相关推荐