How can actions customized through py_func in tensorflow be used in keras?

when customizing the layer through keras, if you use the py_func function of tensorflow to customize the action, it will cause AttributeError: "NoneType" object has no attribute" _ inbound_nodes" to be prompted when creating the model.

the test code is:

import tensorflow as tf
from keras import layers
from keras.layers import Layer
from keras.models import Model


class T(Layer):
    def __init__(self, **kwargs):
        super(T, self).__init__(**kwargs)

    def call(self, inputs, **kwargs):
        return tf.zeros(shape=(1, 1))

    def compute_output_shape(self, input_shape):
        return 1, 1


def direct_return(tensor1, tensor2):
    return tensor1, tensor2


def main():
    input1 = layers.Input(shape=(2, 2))
    input2 = layers.Input(shape=(2, 2))

    ret1, ret2 = tf.py_func(direct_return, [input1, input2], [tf.float32, tf.float32])
    ret1.set_shape((2, 2))
    ret2.set_shape((2, 2))

    t1 = T()(ret1)
    t2 = T()(ret2)

    model = Model(inputs=[input1, input2], outputs=[t1, t2])


main()

error:

Using TensorFlow backend.
Traceback (most recent call last):
  File "XXX/test.py", line 36, in <module>
    main()
  File "XXX/test.py", line 33, in main
    model = Model(inputs=[input1, input2], outputs=[t1, t2])
  File "\conda\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "\conda\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "\conda\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network
    self.inputs, self.outputs)
  File "\conda\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 1353, in _map_graph_network
    tensor_index=tensor_index)
  File "\conda\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)
  File "\conda\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 1312, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: "NoneType" object has no attribute "_inbound_nodes"

if you change the two lines of T1 and T2 to:

    t1 = T()(input1)
    t2 = T()(input2)

then everything is fine.

so how should I make the keras custom layer contain custom actions?


also encounter the same problem. If you want to call tf's py_func in keras, can you add me q775301251 to communicate? Thank you very much!

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea353e-47a2b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea353e-47a2b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?