gpt4 book ai didi

python - Tensorflow - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type float)

转载 作者:行者123 更新时间:2023-12-05 00:10:04 24 4
gpt4 key购买 nike

继续上一个问题:Tensorflow - TypeError: 'int' object is not iterable
我的训练数据是一个列表列表,每个列表包含 1000 个浮点数。例如,x_train[0] =

[0.0, 0.0, 0.1, 0.25, 0.5, ...]
这是我的模型:
model = Sequential()

model.add(LSTM(128, activation='relu',
input_shape=(1000, 1), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))

opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)

model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])

model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\bencu\Desktop\ProjectFiles\Code\Program.py", line 88, in FitModel
model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 728, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 224, in fit
distribution_strategy=strategy)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 547, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 606, in _process_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 479, in __init__
batch_size=batch_size, shuffle=shuffle, **kwargs)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 321, in __init__
dataset_ops.DatasetV2.from_tensors(inputs).repeat()
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 414, in from_tensors
return TensorDataset(tensors)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 2335, in __init__
element = structure.normalize_element(element)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\data\util\structure.py", line 111, in normalize_element
ops.convert_to_tensor(t, name="component_%d" % i))
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1184, in convert_to_tensor
return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1242, in convert_to_tensor_v2
as_ref=False)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1296, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
return constant_op.constant(value, dtype, name=name)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 227, in constant
allow_broadcast=True)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 235, in _constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "C:\Users\bencu\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\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 float).
我试过自己在谷歌上搜索错误,我发现了一些关于使用 tf.convert_to_tensor 的信息。功能。我尝试通过此传递我的培训和测试列表,但该功能不会接受它们。

最佳答案

TL;博士 几个可能的错误,大部分用 x = np.asarray(x).astype('float32') 修复.
其他的可能是数据预处理有问题;确保所有内容的格式正确(分类、nans、字符串等)。下面显示了模型的期望:

[print(i.shape, i.dtype) for i in model.inputs]
[print(o.shape, o.dtype) for o in model.outputs]
[print(l.name, l.input_shape, l.dtype) for l in model.layers]

问题的根源在于使用列表作为输入,而不是 Numpy 数组; Keras/TF 不支持前者。一个简单的转换是: x_array = np.asarray(x_list) .
下一步是确保以预期格式输入数据;对于 LSTM,这将是一个尺寸为 (batch_size, timesteps, features) 的 3D 张量- 或等效地, (num_samples, timesteps, channels) .最后,作为调试专业提示,打印数据的所有形状。完成上述所有操作的代码如下:
Sequences = np.asarray(Sequences)
Targets = np.asarray(Targets)
show_shapes()

Sequences = np.expand_dims(Sequences, -1)
Targets = np.expand_dims(Targets, -1)
show_shapes()
# OUTPUTS
Expected: (num_samples, timesteps, channels)
Sequences: (200, 1000)
Targets: (200,)

Expected: (num_samples, timesteps, channels)
Sequences: (200, 1000, 1)
Targets: (200, 1)

作为奖励提示,我注意到您通过 main() 运行。 ,因此您的 IDE 可能缺少类似 Jupyter 的基于单元格的执行;我强烈推荐 Spyder IDE .就像添加 # In[] 一样简单,然后按 Ctrl + Enter下面:


使用的函数 :
def show_shapes(): # can make yours to take inputs; this'll use local variable values
print("Expected: (num_samples, timesteps, channels)")
print("Sequences: {}".format(Sequences.shape))
print("Targets: {}".format(Targets.shape))

关于python - Tensorflow - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type float),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58636087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com