fork download
  1. import tensorflow as tf
  2. from tensorflow import keras
  3. from tensorflow.keras import layers
  4.  
  5. model =keras.Sequential(
  6. [
  7. layers.Dense(2, activation="relu", name="layer1"),
  8. layers.Dense(3, activation="relu", name="layer2"),
  9. layers.Dense(4, name="layer3"),
  10. ]
  11. )
  12. # Call model on a test input
  13. x = tf.ones((1,2))
  14. y = model(x)
  15. #model.pop()
  16. print(len(model.layers))
  17. #x = tf.ones((1, 4))
  18. print(x)
  19. # No weights at this stage!
  20.  
  21. # At this point, you can't do this:
  22. # model.weights
  23.  
  24. # You also can't do this:
  25. # model.summary()
  26.  
  27. # Call the model on a test input
  28.  
  29. print("Number of weights after calling the model:", len(model.weights)) # 6
  30. model.summary()
  31.  
Success #stdin #stdout #stderr 1.88s 203140KB
stdin
Standard input is empty
stdout
3
Tensor("ones:0", shape=(1, 2), dtype=float32)
Number of weights after calling the model: 6
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
layer1 (Dense)               multiple                  6         
_________________________________________________________________
layer2 (Dense)               multiple                  9         
_________________________________________________________________
layer3 (Dense)               multiple                  16        
=================================================================
Total params: 31
Trainable params: 31
Non-trainable params: 0
_________________________________________________________________
stderr
WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.