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?
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With