我正在使用以下简单代码使用tensorflow加载csv并使用keras执行建模...
无法发现此错误!
import tensorflow as tf
train_dataset_fp = tf.keras.utils.get_file(fname=file_path, origin=URL)
columns = ["X","Y"]
features = columns[:-1]
labels = columns[-1]
batch_size = 32
train_dataset = tf.data.experimental.make_csv_dataset(
train_dataset_fp,
batch_size,
column_names = columns,
label_name= labels,
num_epochs=1
)
data_iterator = train_dataset.make_one_shot_iterator()
X_train, Y_train = data_iterator.get_next()
from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(10, input_shape=[len(X_train)]),
keras.layers.Dense(1)
])
model.compile(loss='mse',
optimizer='adam',
metrics=['mae', 'mse'])
model.summary()
model.fit(X_train, Y_train, epochs=1000, steps_per_epoch=batch_size)
虽然其余代码工作正常,但我无法弄清为什么我得到密集的输入错误。
如果使用pandas的话,相同的代码也可以正常工作,我试图使用tensorflow组件删除对其他库的依赖,但似乎失败了。
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 10) 30
_________________________________________________________________
dense_1 (Dense) (None, 1) 11
=================================================================
Total params: 41
Trainable params: 41
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
File "/Users/abhinavasrivastava/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 267, in standardize_input_data
for x in names
File "/Users/abhinavasrivastava/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 267, in <listcomp>
for x in names
KeyError: 'dense_input'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "simple_linear_keras.py", line 47, in <module>
model.fit(X_train, Y_train, epochs=1000, callbacks=[tb], steps_per_epoch=batch_size)
File "/Users/abhinavasrivastava/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
validation_split=validation_split)
File "/Users/abhinavasrivastava/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
class_weight, batch_size)
File "/Users/abhinavasrivastava/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1117, in _standardize_weights
exception_prefix='input')
File "/Users/abhinavasrivastava/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 271, in standardize_input_data
'for each key in: ' + str(names))
ValueError: No data provided for "dense_input". Need data for each key in: ['dense_input']
答案 0 :(得分:0)
错误No data provided for "dense_input"
表示Keras根本没有获得输入数据或没有获得预期格式的输入数据,即,在Python中表示为numpy数组的数组形式。
假设其他一切正常,应该只需添加一行即可转换X_train和Y_train:
import numpy as np
X_train = np.array(X_train)
Y_train = np.array(Y_train)
答案 1 :(得分:0)
好像您正在使用 *
* *
* *
* *
*********
* *
* *
* *
*
来适合Keras模型。 Keras模型无法正确获取数据,因为您没有在数据集中指定要素cols的要素名称。要解决此问题,您需要在tf.Dataset
函数中明确提供功能名称name=features
:
model.fit