【问题标题】:Failed to convert a NumPy array to a Tensor (Unsupported object type dict)无法将 NumPy 数组转换为张量(不支持的对象类型字典)
【发布时间】:2021-09-09 11:13:22
【问题描述】:

我的方法我认为问题出在

 history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], validation_data=val_generator, validation_steps=val_steps)

def data_generator(descriptions, photos, tokenizer, max_length, imgsIds, vocab_size):
    while 1:
        for ind in range(len(imgsIds)):
            photo = photos[ind]
            key = imgsIds[ind]
            desc_list = descriptions[str(key)]
            in_img, in_seq, out_word = create_sequences(
                tokenizer, max_length, desc_list, photo, vocab_size)
            yield [in_img, in_seq], out_word

我明白了

Failed to convert a NumPy array to a Tensor (Unsupported object type dict).

如果有什么我应该添加的,请评论..谢谢

Traceback (most recent call last):
  File "fit.py", line 271, in <module>
    main(sys.argv)
  File "fit.py", line 268, in main
    fit_model(train, train_descriptions, train_rnn_input, val, val_descriptions, val_rnn_input)
  File "fit.py", line 255, in fit_model
    history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], validation_data=val_generator, validation_steps=val_steps)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1479, in fit_generator
    initial_epoch=initial_epoch)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 872, in fit
    return_dict=True)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1057, in evaluate
    model=self)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__
    model=model)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 775, in __init__
    peek = _process_tensorlike(peek)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1013, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 617, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 617, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1008, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 262, in constant
    allow_broadcast=True)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type dict).
2021-06-27 04:46:22.936001: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.
     [[{{node PyFunc}}]]

编辑

def create_sequences(tokenizer, max_length, desc_list, photo, vocab_size):
    X1, X2, y = list(), list(), list()
    for desc in desc_list:
        seq = tokenizer.texts_to_sequences([desc])[0]
        for i in range(1, len(seq)):
            in_seq, out_seq = seq[:i], seq[i]
            in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
            out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
            X1.append(np.array(photo).astype(np.float32))
            X2.append(np.array(in_seq).astype(np.float32))
            y.append(np.array(out_seq).astype(np.float32))
    return array(X1), array(X2), array(y)

【问题讨论】:

  • 尝试打印照片的形状,in_seq,out_seq:print (photo.shape, photo.dtype)
  • 我得到了 X1.append(np.array(photo).astype(np.float32)) TypeError: float() argument must be a string or a number, not 'dict'
  • 这个输出是什么? print(type(photo)) 告诉我形状
  • 我得到了这个 (1000,)
  • photo 变量是一个字典。在您获得此变量的位置添加您的代码以及该值的一些示例数据。

标签: python tensorflow keras lstm tensor


【解决方案1】:

此错误表明您的数据中的某些值或所有值没有要转换的有效数据类型。


原因

此错误的常见原因是数组的值在图形模式下不是给定的 dtype。这可能是因为某些值是 NaNNone 或所有值的格式不支持转换为张量,例如 python 字典。


解决方案

可以通过将数据转换为预期的 dtype 来解决此问题,例如,在输入模型之前将 x=np.asarray(x).astype(np.float32) 等方法应用于输入数据。它还支持 NaN 值问题。请注意,最好对 None 数据值进行一些预处理,并将其替换为 DataFrame.fillna() 之类的方法。

但是在您的数据类型不受支持的情况下(例如 python 字典),您无法使用上述方法解决问题。您应该更改数据结构,然后将其提供给模型。请注意,即使np.asarray 也无法更改结构。您可能会得到数据类型为 numpy 数组,但结构保持不变,无法通过网络处理。所以不要把类型作为不听话的证据。举个例子:

x = {1:1,2:2,3:3,4:4,5:5}
print(type(x))      #<class 'dict'>
x = np.asarray(x)
print(type(x))      #<class 'numpy.ndarray'> #the type is changed
print(x)            #{1: 1, 2: 2, 3: 3, 4: 4, 5: 5} #the structure has not been changed
    

您的场景:

正如您的代码所反映的,您将数据转换为 numpy 数组和浮点数。所以,即使你有 None 变量,你也不会出错。因此,正如错误所反映的 (Unsupported object type dict),您的输入变量之一 ([in_img, in_seq], out_word) 是字典。根据您的代码,in_seqout_seq 是列表。所以,它应该是从photo 变量启动的in_img。所以,检查这个变量数据。它很可能拥有像数据一样的字典。不要关注类型(print(type(photo))),因为在我上面的代码中,它可能是一个numpy.ndarray,但是持有一个字典数据。

【讨论】:

  • 你在cmets里说过X1.append(np.array(photo).astype(np.float32)) TypeError: float() argument must be a string or a number, not 'dict'.你是怎么解决的?
  • 我没有解决它..我只是打印这些参数持有的数据..但基本错误仍然
猜你喜欢
  • 1970-01-01
  • 2020-07-05
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 2021-04-25
  • 2022-01-10
  • 2021-12-01
相关资源
最近更新 更多