我正在学习TensorFlow的一个教程,但我不理解以下代码的用途:
numeric_inputs = {name:input for name,input in inputs.items()
if input.dtype==tf.float32}
x = layers.Concatenate()(list(numeric_inputs.values()))
norm = layers.Normalization()
norm.adapt(np.array(titanic[numeric_inputs.keys()]))
all_numeric_inputs = norm(x)
all_numeric_inputs
这是我目前的尝试:
numeric_inputs
是过滤结果的映射- 我不明白为什么在这里使用
layers.Concatenate()
。我已经看过文件了 - 我知道在使用
norm
作为Sequential
模型的(起始)层之前需要调用norm.adapt()
(来自上一节)。但为什么在这里用x
,即norm(x)
来称呼它呢
Btw,关于使用官方教程学习有什么建议吗?我发现其中一些对我来说仍然太模糊了
您提供的代码部分位于本教程的混合数据类型部分
要回答您的问题,以下是教程中的一些代码:
numeric_inputs = {name:input for name,input in inputs.items()
if input.dtype==tf.float32}
x = layers.Concatenate()(list(numeric_inputs.values()))
norm = layers.Normalization()
norm.adapt(np.array(titanic[numeric_inputs.keys()]))
all_numeric_inputs = norm(x)
all_numeric_inputs
其中titanic
是熊猫数据帧:
titanic = pd.read_csv("https://storage.googleapis.com/tf-datasets/titanic/train.csv")
titanic_features = titanic.copy()
inputs = {}
for name, column in titanic_features.items():
dtype = column.dtype
if dtype == object:
dtype = tf.string
else:
dtype = tf.float32
inputs[name] = tf.keras.Input(shape=(1,), name=name, dtype=dtype)
回到你的问题:
- CCD_ 9是一个过滤字典
- CCD_ 10层允许从过滤字典中获得单个张量(首先将其转换为列表)
- 由于功能性API将用于定义模型,因此使用
Normalization
层(此处实例化为norm
)对数据进行规范化,该层对一些数据进行调整/训练。规范化层将前一层CCD_ 13的输出作为输入