python - 调用 "dense_features_5"层时遇到异常

我有多类分类(3 个类),因此输出层有 3 个神经元,所有列都是数字。并犯了一个我无法理解的错误。这是我的代码:

def df_to_dataset(df, shuffle=True, batch_size=32): 
  df = df.copy()
  labels = df.pop('class')
  dicts = {'STAR': 1, 'GALAXY': 2, 'QSO': 3}
  converted_labels = np.array([dicts[l] for l in labels])
  ds = tf.data.Dataset.from_tensor_slices((dict(df), converted_labels))
  if shuffle:
    ds = ds.shuffle(buffer_size=len(df))
  return ds

batch_size = 32
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

feature_columns = []
for numeric_col in ['objid', 'ra', 'dec', 'u', 'g', 'r', 'i', 'z', 'run', 'rerun', 'camcol', 'field', 'specobjid', 'redshift', 'plate', 'mjd', 'fiberid']:
  feature_columns.append(feature_column.numeric_column(numeric_col))

feature_layer = DenseFeatures(feature_columns) # A layer that produces a dense Tensor
model = Sequential([
  feature_layer,
  Dense(32, activation='relu'),
  Dense(3, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_ds,
          validation_data=val_ds,
          epochs=10)

这里有一个错误:

ValueError: 在用户代码中:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, in train_step
    y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

ValueError: Exception encountered when calling layer "dense_features_5" (type DenseFeatures).

Feature (key: camcol) cannot have rank 0. Given: Tensor("IteratorGetNext:0", shape=(), dtype=int64)

请告诉我它可能是什么?

最佳答案

所以我不小心从 df_to_dataset 函数中删除了这一行:

  def df_to_dataset(df, shuffle=True, batch_size=32): 
    df = df.copy()
    labels = df.pop('class')
    dicts = {'STAR': 1, 'GALAXY': 2, 'QSO': 3}
    converted_labels = np.array([dicts[l] for l in labels])
    ds = tf.data.Dataset.from_tensor_slices((dict(df), converted_labels))
    if shuffle:
      ds = ds.shuffle(buffer_size=len(df))
      ds = ds.batch(batch_size)     # this one
    return ds

现在一切正常:)

更新:您可以传递您拥有的所有数据,而不是批处理,但在 tensorflow 中,您应该传递批处理

https://stackoverflow.com/questions/69910971/

相关文章:

javascript - 是否所有 WebAPI 都被推送到任务队列中?

python - 如何将路径参数传递给 Pydantic 模型?

networking - float IP 在 Digital Ocean 上的使用

jooq - jooq record 取数据时是否使用列索引?

r - 具有不同颜色负值的条形图

angular - 检测关闭浏览器 Angular 12 的事件

swift - 我们如何在 SwiftUI 中制作自定义 GeometryReader?

kubernetes - Kubernetes 如何处理同一资源的多个 API 版本?

json - 通过 Postman 使用 PUT 请求更新 Contentful 帖子

node.js - 在不使用 NVM for Windows 的情况下在 Windows 上安装多个