【问题标题】:If then inside custom non-trainable keras layer如果然后在自定义不可训练的 keras 层内
【发布时间】:2023-03-19 13:40:02
【问题描述】:

我有一个自定义 Keras 层,我想从特定输入返回特定输出。我不希望它是可训练的。

图层应该做以下事情

if input = [1,0] then output = 1
if input = [0,1] then output = 0

相反,它总是输出-1,如果有问题我设置的值。

我认为与我预期不符的行是:

if(test_mask_1_result_count == 2)

这是自定义层:

class my_custom_layer(layers.Layer):

    def __init__(self, **kwargs):
        super(my_custom_layer, self).__init__(**kwargs)

    def call(self, inputs,training=None):

        def encode():

            # set up the test mask:
            test_mask_1 = np.array([0,1],dtype=np.int32)
            k_test_mask_1 = backend.variable(value=test_mask_1)

            # test if the input is equal to the test mask
            test_mask_1_result = backend.equal(inputs,k_test_mask_1)

            # add up all the trues
            test_mask_1_result_count = tf.reduce_sum(tf.cast(test_mask_1_result, tf.int32))

            # return if we've found the right mask
            if(test_mask_1_result_count == 2):
                res = np.array([0]).reshape((1,)) #top left
                k_res = backend.variable(value=res)
                return k_res

            # set up to test the second mask
            test_mask_2 = np.array([1,0],dtype=np.int32)
            k_test_mask_2 = backend.variable(value=test_mask_2)

            # test if the input is equal to the test mask
            test_mask_2_result = backend.equal(inputs,k_test_mask_2)

            # add up all the trues
            test_mask_2_result_count = tf.reduce_sum(tf.cast(test_mask_2_result, tf.int32))

            # return if we've found the right mask
            if(test_mask_2_result_count == 2):
                res = np.array([1]).reshape((1,)) #top left
                k_res = backend.variable(value=res)
                return k_res


            # if we've got here we're in trouble:
            res = np.array([-1]).reshape((1,)) #top left
            k_res = backend.variable(value=res)
            return k_res


        return encode()

    def compute_output_shape(self, input_shape):
        return (input_shape[0],1) 

为什么if 从未触发?

我还在网络之外使用 keras 制作了一个 MWE。这似乎按预期工作:

mask_1 = np.array([1,0],dtype=np.int32)
k_mask_1 = backend.variable(value=mask_1)

input_1 = np.array([1,0],dtype=np.int32)
k_input_1 = backend.variable(value=input_1)


mask_eq = backend.equal(k_input_1,k_mask_1)

mask_eq_sum = tf.reduce_sum(tf.cast(mask_eq, tf.int32))

# keras backend
sess = backend.get_session()

print(sess.run(mask_eq_sum))

输出2

我怀疑有些基本的东西我不明白。

【问题讨论】:

    标签: keras deep-learning keras-layer


    【解决方案1】:

    我不确定您的代码有什么问题,但您的层似乎比必要的复杂得多。例如,

    my_custom_layer = layers.Lambda(lambda x: x[0])
    

    应该符合您的规格。如果你想让它更健壮,你可以使用

    my_custom_layer = layers.Lambda(lambda x: 1 if x == [1,0] else 0 if x == [0,1] else -1)
    

    def mask_func(in_t):
        if in_t == [1,0]:
            return 1
        elif in_t == [0,1]:
            return 0
        else:
            return -1
    my_custom_layer = layers.Lambda(mask_func)
    

    相反。假设您使用的是TF2.0,自定义图层非常宽松。显然,如果您使用它来处理批处理,则需要对其进行一些修改,但希望您明白这一点。

    【讨论】:

    • 感谢您的回答!你介意扩展你关于批次的观点吗?我会修改哪些位?
    • @RNs_Ghost 如果数据是分批处理而不是单独处理,它会为数据添加另一个维度。因此,在这种情况下,例如,形状将从(2,) 变为(batch_size,2),您必须执行编辑mask_func 之类的操作来单独处理批次中的每个项目或类似的操作。
    • 我不断收到:AttributeError: 'NoneType' object has no attribute 'get_shape'
    • @RNs_Ghost 我假设这是在您要批量处理的修改中?我无权访问该代码 - 您可能想发布一个新问题。
    • 是的,我明白没有整个事情很难 - 我在这里发布了我所有的代码:stackoverflow.com/questions/60566048/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多