首页 > 解决方案 > 使用 tensorflow 数据集导致 keras 值错误的原因是什么?

问题描述

我一直在尝试使用 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)

标签: pythonkerastensortensorflow-datasets

解决方案


推荐阅读