问题
i'm currently working for a binary text classification using imdb keras dataset. I have been try to fix this problem for a few hours, looking for answer in stackoverflow and github but that doesn't help. Here's my code
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)
The error is :
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)
Please help me. I really appreciate. Thanks
回答1:
You need to vectorize the sequences. To answer quickly I reduced the dimension to 10.000, you can set the value whatever you like.
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)
We will start with vector_seq
function.
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
function make x_train
, x_test
as a tuple dimension. The reason you got the error is because of the dimension. You are feeding the dimension (25.000,) but keras
needs (25.000, 10.000). Of course, you can change the 10.000 to whatever you like.
We will continue with the formatting our data
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')
and now we are ready to compile our model.
Below is the full code:
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)
来源:https://stackoverflow.com/questions/63169445/valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupported-object-type