- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经为多输入nn编写了一个生成器,但是在使用tf.data.Dataset.from_generator()函数时出现错误,所有数据都在numpy中,其中:输入1的形状为(16,100,223,3),输入2 的形状为(100,223),输入 3 的形状为(16,),标签的形状为(2,)。数据是所有这些组合的数组
我的代码
def data_generator(train_list, batch_size):
i = 0
j = 0
flag = True
while True:
# inputs = []
# outputs = []
if i < len(train_list):
if flag == True:
train_path = os.path.join(training_dir, train_list[i])
data = np.load(train_path, allow_pickle=True)
flag = False
if j >= len(data):
j = 0
i += 1
flag = True
del data
else:
if len(data[j:]) >= batch_size:
input_1 = data[j:(j+batch_size), 0]
input_2 = data[j:(j+batch_size), 1]
input_3 = data[j:(j + batch_size), 2]
outputs= data[j:(j+batch_size), -1]
j += (batch_size)
yield {'Input_Branch-1' : input_1,'Input_Branch-2': input_2, 'Input_Branch-3': input_3}, outputs
elif len(data[j:])< batch_size:
input_1 = data[j:, 0]
input_2 = data[j:, 1]
input_3 = data[j:, 2]
outputs= data[j:, -1]
j = 0
i+= 1
flag = True
del data
yield {'Input_Branch-1': input_1, 'Input_Branch-2': input_2, 'Input_Branch-3': input_3}, outputs
else:
i = 0
del data
flag = True
np.random.shuffle(train_list)
batch_size = 5
dataset = tf.data.Dataset.from_generator(data_generator, args= [train_list, batch_size],
output_types = ({'Input_Branch-1': tf.uint8, 'Input_Branch-2': tf.uint8, 'Input_Branch-3': tf.float32}, tf.float32),)
# for seeing the output of data generator
num = 0
for data, labels in dataset:
print(data.shape, labels.shape)
print(labels)
print()
num = num + 1
if num > 1: break
我收到以下错误
2020-08-04 17:43:30.653430: W tensorflow/core/framework/op_kernel.cc:1741] Invalid argument: TypeError: `generator` yielded an element that could not be converted to the expected type. The expected type was uint8, but the yielded element was [array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=uint8)].
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\panwa\.conda\envs\Vision\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py", line 801, in generator_py_func
ret, dtype=dtype.as_numpy_dtype))
File "C:\Users\panwa\.conda\envs\Vision\lib\site-packages\tensorflow\python\ops\script_ops.py", line 203, in _convert
result = np.asarray(value, dtype=dtype, order="C")
File "C:\Users\panwa\.conda\envs\Vision\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
同样的错误重复多次请帮忙!!!
最佳答案
我有一个类似的问题,我相信我通过指定生成器生成的形状解决了这个问题。对我来说,输出是 2 个图像 32x32x3,以及两个标签,所以我使用它作为 from_generator 方法的参数。
dataset = tf.data.Dataset.from_generator
generator,
# this is the part that you care about #
output_signature=(
tf.TensorSpec(shape=(2,32,32,3), dtype=tf.int32),
tf.TensorSpec(shape=(2,1), dtype=tf.int32,))
#
)
我尝试运行您的代码,但 train_list 未定义。我仍然相信这可能会起作用,也许需要进行一两次更改
output_signature = (
# the one is there to clarify that there is one of these objects
tf.TensorSpec(shape=(1,16,100,223,3), dtype=tf.int32)
tf.TensorSpec(shape=(1,100,223), dtype=tf.int32)
tf.TensorSpec(shape=(1,16,) ,dtype=tf.int32)
tf.TensorSpec(shape=(2,1),dtype=tf.int32)
)
这里是一个玩具数据集的链接,我制作的目的是为了弄清楚这个困惑的情况
https://colab.research.google.com/drive/17pu6jJYLGP-I1nJnzigOx2YOh3Ih4cvG?usp=sharing
关于python - tensorflow from_generator() 给出错误 - 'generator` 产生无法转换为预期类型的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63247003/
我正在尝试使用 TensorFlow Dataset API 读取 HDF5 文件,使用 from_generator 方法。一切正常,除非批量大小不能平均分配给事件数。我不太明白如何使用 API 进
我正在尝试使用生成器训练估算器,但我想为该估算器提供每次迭代的样本包。我显示代码: def _generator(): for i in range(100): feats = np.ran
我已经为多输入nn编写了一个生成器,但是在使用tf.data.Dataset.from_generator()函数时出现错误,所有数据都在numpy中,其中:输入1的形状为(16,100,223,3)
我有一个不平凡的输入管道, from_generator 非常适合... dataset = tf.data.Dataset.from_generator(complex_img_label_gene
这是重复我在尝试使用生成器动态生成/提供训练数据时遇到的问题的玩具代码。 def makeRand(): yield np.random.rand(1) dataset = tf.data.D
我想使用通过 from_generator 构建的 TensorFlow 数据集来访问格式化文件。大多数一切正常,除了我不知道如何在生成器用完数据时停止数据集迭代器(当您超出范围时,生成器只会永远返回
我有一个包含各种尺寸图像的文件夹。我需要将它们全部调整为 (160,120) 并创建一个 tf.data.Dataset。为此,我使用了 tf.data.Dataset.from_generator。
我想创建一个 tf.data.Dataset.from_generator(...) 数据集。我需要传入一个 Python generator。 我想将先前数据集的属性传递给生成器,如下所示: dat
我正在根据需要尝试使用 tensorflow 的 nightly 1.4 Dataset.from_generator将一些可变长度的数据集拼在一起。这个简单的代码(来自 here 的想法): imp
我想使用 from_generator() 函数创建一些 tf.data.Dataset。我想向生成器函数 (raw_data_gen) 发送一个参数。这个想法是生成器函数将根据发送的参数产生不同的数
我正在尝试生成我自己的图像数据集以在单个 GPU 上使用 Tensorflow Dataset API 测量推理性能 resolutions = [ (2048, 1080) ] def ge
在生产环境中,我有来自 N 个生产者的数据,这些数据必须通过网络。我在 parallelising tf.data.Dataset.from_generator 上找到了这条评论这真的描述了我想要的东
我有一堆 JSON 数组文件(准确地说是 AVRO),每个文件都会产生多个样本来训练 Keras 模型。使用来自 @GPhilo 的想法来自@jsimsa ,我能够想出这个来并行化我的输入管道。无法弄
我需要遍历大量图像文件并将数据提供给 tensorflow。我创建了一个 Dataset由生成器函数返回,该函数将文件路径名生成为字符串,然后使用 map 将字符串路径转换为图像数据。 .但它失败了,
tf.data 有一个 from_generator初始值设定项,它似乎不可扩展。来自官方指南 Caution: While this is a convienient approach it has
我正在尝试在 keras 中实现一个具有多个输入的模型: 图片 (200x200) 一些数字 (1x50) 三个一维信号(1x50000、2x100000) 为了提供该模型,我想编写一个与 tf.da
当我从生成器创建 tf 数据集并尝试运行 tf2.0 代码时,它会向我发出贬值消息警告。 代码: import tensorflow as tf from tensorflow.keras.layer
该脚本试图使用一个函数(在本例中为 np.sin())作为生成器,旨在最终通过管道传输到模型中进行训练(因此 tf.Session( ))。不幸的是,我不断收到错误消息: “ValueError:ge
我正在为我的训练数据使用 tensorflow 数据集 api,为 tf.data.Dataset.from_generator api 使用 input_fn 和生成器 def generator(
我正在处理一个时间序列问题,其中每个时间序列都相当长(10^3-10^4 个时间步长,并且每个时间序列的长度不同)。 对于每个序列,我可以定义一个 Python 生成器,它一次生成一个时间步长的值。我
我是一名优秀的程序员,十分优秀!