如何将 PostgreSQL 数据库的输出从 TensorFlow IO 提供给 tf.keras 顺序模型

How to feed the output of PostgreSQL database from TensorFlow IO to tf.keras Sequential Model

我正在尝试学习如何使用 this tutorial 将 SQL 数据集读入 Tensorflow。直到最后一步一切正常!

dataset = tfio.experimental.IODataset.from_sql(
    query="SELECT pt08s1, nmhc, c6h6, co FROM AirQualityUCI;",
    endpoint=endpoint)

print(dataset.element_spec)
#{'pt08s1': TensorSpec(shape=(), dtype=tf.int32, name=None), 'nmhc': TensorSpec(shape=(), dtype=tf.float32, name=None), 'c6h6': TensorSpec(shape=(), dtype=tf.float32, name=None), 'co': TensorSpec(shape=(), dtype=tf.float32, name=None)}

我只取前10条记录。

dataset = dataset.take(10)
for i in dataset.as_numpy_iterator():
    print(i)

# {'pt08s1': 1360, 'nmhc': 150.0, 'c6h6': 11.9, 'co': 2.6}
# {'pt08s1': 1292, 'nmhc': 112.0, 'c6h6': 9.4, 'co': 2.0}
# {'pt08s1': 1402, 'nmhc': 88.0, 'c6h6': 9.0, 'co': 2.2}
# {'pt08s1': 1376, 'nmhc': 80.0, 'c6h6': 9.2, 'co': 2.2}
# {'pt08s1': 1272, 'nmhc': 51.0, 'c6h6': 6.5, 'co': 1.6}
# {'pt08s1': 1197, 'nmhc': 38.0, 'c6h6': 4.7, 'co': 1.2}
# {'pt08s1': 1185, 'nmhc': 31.0, 'c6h6': 3.6, 'co': 1.2}
# {'pt08s1': 1136, 'nmhc': 31.0, 'c6h6': 3.3, 'co': 1.0}
# {'pt08s1': 1094, 'nmhc': 24.0, 'c6h6': 2.3, 'co': 0.9}
# {'pt08s1': 1010, 'nmhc': 19.0, 'c6h6': 1.7, 'co': 0.6}

现在,我的yco变量,其余都是自变量。我想创建一个非常基本的 DNN 回归模型用于演示目的。

model = tf.keras.Sequential([
      tf.keras.layers.Dense(64, activation='relu'),
      tf.keras.layers.Dense(64, activation='relu'),
      tf.keras.layers.Dense(1)
  ])

model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(0.001))

我知道我需要简单地将一个元组 (x,y) 提供给模型,但这是一个元组字典。我尝试使用以下方法将其映射以分离出特征和标签:

dataset = dataset.map(lambda item: ((item['pt08s1'], item['nmhc'], item['c6h6']), item['co'])) 
#<MapDataset shapes: (((), (), ()), ()), types: ((tf.int32, tf.float32, tf.float32), tf.float32)>

和运行

model.fit(dataset, epochs=2)

但是我得到一个错误:

ValueError: Layer my_model expects 1 input(s), but it received 3 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=() dtype=int32>, <tf.Tensor 'IteratorGetNext:1' shape=() dtype=float32>, <tf.Tensor 'IteratorGetNext:2' shape=() dtype=float32>]

其实我也不知道怎么算。感谢所有帮助!

它与发布的问题完全相似 here。我昨天刚回答。

诀窍是使用命名输入层并将输入作为 x 和 y 的组合字典而不是 x 和 y 的元组提供。有关详细信息,请查看上面的 link。