If then inside custom non-trainable keras layer

送分小仙女□ 提交于 2020-04-17 22:06:02

问题


I have a custom Keras layer that I want to return specific output from specific inputs. I don't want it to be trainable.

The layer should do the following

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

Instead, it always outputs -1, the value I set if there's problem.

I think the line that is not behaving as I expect it would is:

if(test_mask_1_result_count == 2)

Here's the custom layer:

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) 

Why doesn't the if ever trigger?

I also produced a MWE using keras outside of a network. This seems to work as intended:

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

Outputs 2

I suspect there is something fundamental that I don't understand.


回答1:


I'm not sure what the problem is with your code, but your layer seems to be much more complicated than necessary. For instance,

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

should meet your specs. If you want it to be more robust, you could use

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

or

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)

instead. Assuming you're using TF2.0, custom layers are pretty lenient. Obviously, if you're using this to process batches, you'll need to modify it a little bit, but hopefully you get the point.



来源:https://stackoverflow.com/questions/60555305/if-then-inside-custom-non-trainable-keras-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!