我有以下数据([train][1](518MB(,[test][2](129MB((。我以以下方式使用tensorflow
加载它们:-
import tensorflow as tf
train_data = tf.data.experimental.make_csv_dataset("flight_2018_train.csv",
batch_size = 10000,
label_name="Cancelled",
num_epochs = 20,
num_parallel_reads=2)
test_data = tf.data.experimental.make_csv_dataset("flight_2018_test.csv",
batch_size = 10000,
label_name="Cancelled",
num_epochs = 20,
num_parallel_reads=2)
这些对象的类型为tensorflow.python.data.ops.dataset_ops.PrefetchDataset
。
我创建了以下模型:-
sda_1 = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation="relu", input_shape=(16,)),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(2, activation = "sigmoid")
])
sda_1.compile(optimizer='adam',
loss=tf.keras.losses.MeanAbsoluteError(),
metrics = [tf.keras.metrics.MeanSquaredError()])
并按照@Finn Meyer的建议进行了编辑。
现在我有一个不同的错误消息。
当我在model.fit
中传递数据集时,我得到以下错误:-
sda_1.fit(train_data, validation_data = test_data)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-78-46f637b35970> in <module>
----> 1 sda_1.fit(train_data)
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1160, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1146, 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 1135, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 993, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 198, in assert_input_compatibility
f'Missing data for input "{name}". '
ValueError: Missing data for input "dense_43_input". You passed a data dictionary with keys ['Month', 'DayofMonth', 'DayOfWeek', 'OriginAirportID', 'DestAirportID', 'DepTime', 'DepDelay', 'DepDel15', 'ArrTime', 'ArrDelay', 'ArrDel15', 'CarrierDelay', 'WeatherDelay', 'NASDelay', 'SecurityDelay', 'LateAircraftDelay']. Expected the following keys: ['dense_43_input']
我不明白我哪里错了。
您需要为第一层中的传入数据定义输入形状。由于您有16个输入,请执行以下操作:
sda_1 = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation="relu", input_shape=(16,)),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(2, activation = "sigmoid")
])