Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply (call) a single layer on data in Keras?

Tags:

Is there an easy way to give data to a layer in Keras (over TF) and see the return values, for test purposes, without actually building a full model and fitting data to it?

If not, how can one test a customized layer that they develop?

like image 934
user25004 Avatar asked Sep 14 '18 19:09

user25004


2 Answers

You can define and use a backend function for this purpose:

from keras import backend as K

# my_layer could be a layer from a previously built model, like:
# my_layer = model.layers[3]
func = K.function(model.inputs, [my_layer.output])

# or it is a layer with customized weights, like:
# my_layer = Dense(...)
# my_layer.set_weights(...)
# out = my_layer(input_data)
input_data = Input(shape=...)
func = K.function([input_data], [my_layer.output])

# to use the function:
layer_output = func(layer_input)   # layer_input is a list of numpy array(s)
like image 97
today Avatar answered Sep 28 '22 17:09

today


Here's a complete example of the suggestion in this answer.

import numpy as np
import tensorflow as tf
# import tensorflow_probability as tfp 


def main():
    input_data = tf.keras.layers.Input(shape=(1,))

    layer1 = tf.keras.layers.Dense(1)
    out1 = layer1(input_data)

    # Get weights only returns a non-empty list after we need the input_data
    print("layer1.get_weights() =", layer1.get_weights())

    # This is actually the required object for weights.
    new_weights = [np.array([[1]]), np.array([0])] 
    layer1.set_weights(new_weights)

    out1 = layer1(input_data)
    print("layer1.get_weights() =", layer1.get_weights())

    func1 =  tf.keras.backend.function([input_data], [layer1.output])

    #layer2 = tfp.layers.DenseReparameterization(1)
    #out2 = layer2(input_data)
    #func2 = tf.keras.backend.function([input_data], [layer2.output])

    # The input to the layer.
    data = np.array([[1], [3], [4]])
    print(data)

    # The output of layer1
    layer1_output = func1(data)
    print("layer1_output =", layer1_output)

    # The output of layer2
    # layer2_output = func2(data)
    # print("layer2_output =", layer2_output)


if __name__ == "__main__":
    main()
like image 22
nbro Avatar answered Sep 28 '22 19:09

nbro