A layer that takes a user-defined function using Lambda. If the function has trainable weights, the weights should be provided. Remember to make sure the weights provided when the layer is constructed are SAME as the weights used when the layer is forwarded. For multiple inputs see :
| 17 | |
| 18 | |
| 19 | class Lambda(Layer): |
| 20 | """A layer that takes a user-defined function using Lambda. |
| 21 | If the function has trainable weights, the weights should be provided. |
| 22 | Remember to make sure the weights provided when the layer is constructed are SAME as |
| 23 | the weights used when the layer is forwarded. |
| 24 | For multiple inputs see :class:`ElementwiseLambda`. |
| 25 | |
| 26 | Parameters |
| 27 | ---------- |
| 28 | fn : function |
| 29 | The function that applies to the inputs (e.g. tensor from the previous layer). |
| 30 | fn_weights : list |
| 31 | The trainable weights for the function if any. Optional. |
| 32 | fn_args : dict |
| 33 | The arguments for the function if any. Optional. |
| 34 | name : str or None |
| 35 | A unique layer name. |
| 36 | |
| 37 | Examples |
| 38 | --------- |
| 39 | Non-parametric and non-args case: |
| 40 | This case is supported in the Model.save() / Model.load() to save / load the whole model architecture and weights(optional). |
| 41 | |
| 42 | >>> x = tl.layers.Input([8, 3], name='input') |
| 43 | >>> y = tl.layers.Lambda(lambda x: 2*x, name='lambda')(x) |
| 44 | |
| 45 | |
| 46 | Non-parametric and with args case: |
| 47 | This case is supported in the Model.save() / Model.load() to save / load the whole model architecture and weights(optional). |
| 48 | |
| 49 | >>> def customize_func(x, foo=42): # x is the inputs, foo is an argument |
| 50 | >>> return foo * x |
| 51 | >>> x = tl.layers.Input([8, 3], name='input') |
| 52 | >>> lambdalayer = tl.layers.Lambda(customize_func, fn_args={'foo': 2}, name='lambda')(x) |
| 53 | |
| 54 | |
| 55 | Any function with outside variables: |
| 56 | This case has not been supported in Model.save() / Model.load() yet. |
| 57 | Please avoid using Model.save() / Model.load() to save / load models that contain such Lambda layer. Instead, you may use Model.save_weights() / Model.load_weights() to save / load model weights. |
| 58 | Note: In this case, fn_weights should be a list, and then the trainable weights in this Lambda layer can be added into the weights of the whole model. |
| 59 | |
| 60 | >>> a = tf.Variable(1.0) |
| 61 | >>> def func(x): |
| 62 | >>> return x + a |
| 63 | >>> x = tl.layers.Input([8, 3], name='input') |
| 64 | >>> y = tl.layers.Lambda(func, fn_weights=[a], name='lambda')(x) |
| 65 | |
| 66 | |
| 67 | Parametric case, merge other wrappers into TensorLayer: |
| 68 | This case is supported in the Model.save() / Model.load() to save / load the whole model architecture and weights(optional). |
| 69 | |
| 70 | >>> layers = [ |
| 71 | >>> tf.keras.layers.Dense(10, activation=tf.nn.relu), |
| 72 | >>> tf.keras.layers.Dense(5, activation=tf.nn.sigmoid), |
| 73 | >>> tf.keras.layers.Dense(1, activation=tf.identity) |
| 74 | >>> ] |
| 75 | >>> perceptron = tf.keras.Sequential(layers) |
| 76 | >>> # in order to compile keras model and get trainable_variables of the keras model |
no outgoing calls
no test coverage detected
searching dependent graphs…