【问题标题】:Exception encountered when calling layer "dense_features_5"调用层“dense_features_5”时遇到异常
【发布时间】:2021-11-10 09:44:36
【问题描述】:

我有多类分类(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)

请告诉我它是什么?

【问题讨论】:

  • @AloneTogether 它来自 tf.keras.layers
  • 你在这行feature_columns.append(feature_column.numeric_column(numeric_col))做什么?
  • @AloneTogether 使用代表数字特征的feature_column.numeric_column 添加/附加值

标签: python tensorflow keras neural-network


【解决方案1】:

所以我不小心从 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 中,您应该传递批次

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-27
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2022-08-19
    • 2022-07-18
    • 1970-01-01
    相关资源
    最近更新 更多