python - 使用 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)
解决方案
推荐阅读
- javascript - NuxtJS 添加外部脚本组件不起作用
- arrays - 快速,如何创建一个数组,其中所有数字对应于第一个数组中存在的一个月中的所有日期?
- python - 网络摄像头上的客户对象检测
- typescript - 无法将 Typescript 数组重构为 JSON 对象?
- python - 使用递归函数检查矩阵重复元素
- c# - 为什么我的函数将 10 作为参数传递?
- javascript - 我正在制作计算器,当我在输入中添加数字 1+1 时,我得到答案 1。为什么
- elasticsearch - 尝试运行 kibana (elasticsearch) 版本 7.9.3
- c++ - c ++程序将数字提高到幂给出错误('for'之前的预期不合格ID)
- java - 尽管捕获了所有与 Toast 相关的错误,但在 Android 7 上的 ViewRootImpl.java 行 XXX 中崩溃