outputs
是张量对象。当我们制作model
对象时,我们将input
、x
和outputs
对象封装为一个tf.Model
对象。我的困惑来自于模型是如何做到这一点的。难道没有足够的信息来封装完整的东西吗?当我检查model.layers
属性时,它会返回所有三层,而不是两层。很明显,我们只给tf.keras.Model
类的构造函数提供了两个输入(inputs
和outputs
(,那么在给定这些参数的情况下,它如何访问中间层x
?
import tensorflow as tf
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
TensorFlow能够推断出要获得输出,必须通过层x。它特别需要这种能力来进行反向传播和更新所有权重。