调用model.fit时出现“列表索引超出范围”错误

2024-04-25 22:28:34 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

import tensorflow as tf
import pandas as pd

from tensorflow.keras import layers
from tensorflow.keras.preprocessing.text import Tokenizer


train_df = pd.DataFrame({'Text':['This is a test'],'Answer':['Test Answer']})
vocab_size = 4

tokenizer = Tokenizer(num_words=vocab_size, char_level=False, split=' ')
tokenizer.fit_on_texts(train_df.Text)
vec_text = tokenizer.texts_to_sequences(train_df.Text)

longest_sentence = max([len(v) for v in vec_text])
input = tf.keras.Input(shape=(longest_sentence,))

embedding = layers.Embedding(vocab_size + 1, 100)(input)

dense = layers.Dense(vocab_size, activation='softmax')(embedding)

model = tf.keras.Model(input,dense)
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])

labels = tokenizer.texts_to_sequences([str(a) for a in train_df.Answer])
padded_X = tf.keras.preprocessing.sequence.pad_sequences(vec_text,maxlen=longest_sentence)
model.fit(padded_X, labels, batch_size=32, epochs=10, verbose=2)

您应该能够复制此文件并运行它以获取我得到的错误

我在最后一行得到一个索引错误,我不知道为什么。特别是在/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py的第623行

跟踪:

IndexError                                Traceback (most recent call last)
<ipython-input-20-d89868306962> in <module>()
     20 labels = tokenizer.texts_to_sequences([str(a) for a in train_df.Answer])
     21 padded_X = tf.keras.preprocessing.sequence.pad_sequences(vec_text,maxlen=longest_sentence)
---> 22 model.fit(padded_X, labels, batch_size=32, epochs=10, verbose=2)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1062           use_multiprocessing=use_multiprocessing,
   1063           model=self,
-> 1064           steps_per_execution=self._steps_per_execution)
   1065 
   1066       # Container that configures and calls `tf.keras.Callback`s.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
   1097       self._steps_per_execution_value = steps_per_execution.numpy().item()
   1098 
-> 1099     adapter_cls = select_data_adapter(x, y)
   1100     self._adapter = adapter_cls(
   1101         x,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in select_data_adapter(x, y)
    956 def select_data_adapter(x, y):
    957   """Selects a data adapter than can handle a given x and y."""
--> 958   adapter_cls = [cls for cls in ALL_ADAPTER_CLS if cls.can_handle(x, y)]
    959   if not adapter_cls:
    960     # TODO(scottzhu): This should be a less implementation-specific error.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in <listcomp>(.0)
    956 def select_data_adapter(x, y):
    957   """Selects a data adapter than can handle a given x and y."""
--> 958   adapter_cls = [cls for cls in ALL_ADAPTER_CLS if cls.can_handle(x, y)]
    959   if not adapter_cls:
    960     # TODO(scottzhu): This should be a less implementation-specific error.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in can_handle(x, y)
    613     handles_y = True
    614     if y is not None:
--> 615       handles_y = ListsOfScalarsDataAdapter._is_list_of_scalars(y)
    616     return handles_x and handles_y
    617 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _is_list_of_scalars(inp)
    621       return True
    622     if isinstance(inp, (list, tuple)):
--> 623       return ListsOfScalarsDataAdapter._is_list_of_scalars(inp[0])
    624     return False
    625 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _is_list_of_scalars(inp)
    621       return True
    622     if isinstance(inp, (list, tuple)):
--> 623       return ListsOfScalarsDataAdapter._is_list_of_scalars(inp[0])
    624     return False
    625 

IndexError: list index out of range

Tags: inpydatasizeadapterlibpackagesusr