Get startedGet started for free

It's a flow of tensors

If you have already built a model, you can use the model.layers and the tensorflow.keras.backend to build functions that, provided with a valid input tensor, return the corresponding output tensor.

This is a useful tool when we want to obtain the output of a network at an intermediate layer.

For instance, if you get the input and output from the first layer of a network, you can build an inp_to_out function that returns the result of carrying out forward propagation through only the first layer for a given input tensor.

So that's what you're going to do right now!

X_test from the Banknote Authentication dataset and its model are preloaded. Type model.summary() in the console to check it.

This exercise is part of the course

Introduction to Deep Learning with Keras

View Course

Exercise instructions

  • Import tensorflow.keras.backend as K.
  • Use the model.layers list to get a reference to the input and output of the first layer.
  • Use K.function() to define a function that maps inp to out.
  • Print the results of passing X_test through the 1st layer.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import tensorflow.keras backend
import ____

# Input tensor from the 1st layer of the model
inp = ____.____[____].input

# Output tensor from the 1st layer of the model
out = ____.____

# Define a function from inputs to outputs
inp_to_out = K.function([____], [____])

# Print the results of passing X_test through the 1st layer
print(____([____]))
Edit and Run Code