gpt4 book ai didi

python - 值错误 : Failed to convert a NumPy array to a Tensor (Unsupported object type list) in Keras/Tensorflow Python

转载 作者:行者123 更新时间:2023-12-03 19:07:36 24 4
gpt4 key购买 nike

我目前正在使用 imdb keras 数据集进行二进制文本分类。我已经尝试解决这个问题几个小时了,在 stackoverflow 和 github 中寻找答案,但这没有帮助。这是我的代码

import tensorflow as tf
from tensorflow import keras
import numpy as np

data = keras.datasets.imdb
(x_train,y_train),(x_test,y_test) = data.load_data()

dictionary = data.get_word_index()
dictionary = {k:(v+3) for k,v in dictionary.items()}
dictionary['<PAD>'] = 0
dictionary['<START>'] = 1
dictionary['<UNKNOWN>'] = 2
dictionary['<UNUSED>'] = 3

dictionary = dict([(v,k) for (k,v) in dictionary.items()])

model = keras.Sequential([
keras.layers.Embedding(10000,16),
keras.layers.GlobalAveragePooling1D(),
keras.layers.Dense(16,activation='relu'),
keras.layers.Dense(1,activation='sigmoid')
])

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

print(model.summary())

history = model.fit(x_train,y_train,epochs=50,batch_size=32,verbose=1)

prediction = model.predict(x_test)
print(prediction)
错误是:
Traceback (most recent call last):
File "imdb_classification.py", line 65, in <module>
history = model.fit(x_train,y_train,epochs=50,batch_size=32,verbose=1)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 235, in fit
use_multiprocessing=use_multiprocessing)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 593, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 706, in _process_inputs
use_multiprocessing=use_multiprocessing)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 357, in __init__
dataset = self.slice_inputs(indices_dataset, inputs)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 383, in slice_inputs
dataset_ops.DatasetV2.from_tensors(inputs).repeat()
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 566, in from_tensors
return TensorDataset(tensors)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 2765, in __init__
element = structure.normalize_element(element)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\data\util\structure.py", line 113, in normalize_element
ops.convert_to_tensor(t, name="component_%d" % i))
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1314, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\PHILIP\Anaconda3\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\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 258, in constant
allow_broadcast=True)
File "C:\Users\PHILIP\Anaconda3\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 266, in _constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "C:\Users\PHILIP\Anaconda3\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 list)
请帮我。我真的很感激。谢谢

最佳答案

您需要对序列进行矢量化。为了快速回答,我将维度减少到 10.000,您可以随意设置该值。

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)
我们将从 vector_seq 开始功能。
def vector_seq(sequences, dimension=10000):
results = zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results
vector_seq函数制作 x_train , x_test作为元组维度。你得到错误的原因是因为维度。您正在提供维度 (25.000,) 但 keras需要 (25.000, 10.000)。当然,您可以将 10.000 更改为您喜欢的任何值。
我们将继续格式化我们的数据
x_train = vector_seq(x_train)
x_test = vector_seq(x_test)
y_train = asarray(y_train).astype('float32')
y_test = asarray(y_test).astype('float32')
现在我们准备编译我们的模型。
下面是完整的代码:
from keras.datasets import imdb
from keras import Sequential, layers
from numpy import asarray, zeros


def vector_seq(sequences, dimension=10000):
results = zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results


(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)

dictionary = imdb.get_word_index()
dictionary = {k: (v+3) for k, v in dictionary.items()}
dictionary['<PAD>'] = 0
dictionary['<START>'] = 1
dictionary['<UNKNOWN>'] = 2
dictionary['<UNUSED>'] = 3

dictionary = dict([(v, k) for (k, v) in dictionary.items()])

model = Sequential([
layers.Embedding(10000, 16),
layers.GlobalAveragePooling1D(),
layers.Dense(16, activation='relu'),
layers.Dense(1, activation='sigmoid')
])

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

print(model.summary())

x_train = vector_seq(x_train)
x_test = vector_seq(x_test)
y_train = asarray(y_train).astype('float32')
y_test = asarray(y_test).astype('float32')

history = model.fit(x_train, y_train, epochs=50, batch_size=32, verbose=1)

prediction = model.predict(x_test)
print(prediction)

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

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