了解张量流模型类如何将层/张量转换为模型



outputs是张量对象。当我们制作model对象时,我们将inputxoutputs对象封装为一个tf.Model对象。我的困惑来自于模型是如何做到这一点的。难道没有足够的信息来封装完整的东西吗?当我检查model.layers属性时,它会返回所有三层,而不是两层。很明显,我们只给tf.keras.Model类的构造函数提供了两个输入(inputsoutputs(,那么在给定这些参数的情况下,它如何访问中间层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。它特别需要这种能力来进行反向传播和更新所有权重。

最新更新