我一直在尝试使用Keras在已经生成的某些.tfrecord文件上运行神经网络模型。为此,我将它们作为命令行参数传递,并存储在tensorflow数据集中,然后用于拟合模型。但是,当我运行代码时,出现以下错误:ValueError: Please provide either inputs and targets or inputs, targets, and sample_weights
。 Keras似乎很生气,我没有传递单独的输入张量和标签张量,但是我被认为可以将数据集用作单个参数呢?代码如下所示:
import tensorflow as tf
import sys
import tensorflow.data
from tensorflow import keras
from tensorflow.keras import layers
tf.enable_eager_execution()
inputList = []
for file in sys.argv[0:]:
inputList.append(file)
filenames = tf.Variable(inputList, tf.string)
dataset = tf.data.TFRecordDataset(filenames)
dataset.shuffle(1600000)
model = tf.keras.Sequential()
model.add(layers.Dense(13, input_shape=(13,), activation='relu'))
model.add(layers.Dense(20, activation='relu'))
model.add(layers.Dense(20, activation='relu'))
model.add(layers.Dense(10, activation='relu'))
model.add(layers.Dense(2, activation='relu'))
model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(dataset, epochs=10, steps_per_epoch=30)