Lambda图层无法正确通过遮罩

时间:2019-01-16 19:57:53

标签: python tensorflow keras

我遇到了Lambda层的问题。 Lambda层的内部功能应该采用从上一层传递的蒙版张量。我做了一个简单的例子,说明它不能正常工作。

import numpy as np
import keras

def inner_fn(var, mask=None):
    print('print mask in inner_fn', mask)    
    return mask

arr_in = np.ones((2,3, 3))
arr_in[:, 0, :] = 0.
arr_out = np.ones((2,3,3))

x = keras.Input((None, 3))
mask_layer_out = keras.layers.Masking(mask_value = 0.)(x)
y = keras.layers.Lambda(inner_fn)(mask_layer_out)
model = keras.Model(inputs=x, outputs=y)
model.compile('sgd', 'mse')
model.fit(arr_in, arr_out, epochs=1)

使用此代码,两次调用inner_fn中的打印,如下所示:

print mask in inner_fn Tensor("masking_1/Any_1:0", shape=(?, ?), dtype=bool)
print mask in inner_fn None

首先,我认为是对的。但是我不知道为什么要进行第二次打印,并且第二次使用蒙版None。此外,由于遮罩为None,因此无法编译模型,因此 AttributeError: 'NoneType' object has no attribute 'get_shape'

这是Lambda层的问题吗? 我的keras版本是2.2.4,tensorflow 1.12.0和Windows 10。

0 个答案:

没有答案
相关问题