keras IndexError:索引2超出大小为1的轴1的界限

yvgpqqbh  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(204)

我正在使用Keras处理python脚本,但收到“IndexError:当我尝试将标签数据转换为分类格式时,索引2超出了最后一行中大小为1的轴1的范围”-我不明白为什么形状会有问题。欢迎提供任何帮助...:

import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense

# Generate a random list of 10 characters 'a' or 'b'
data = ['a' if x == 0 else 'b' for x in np.random.randint(2, size=10)]

# Initialize a tokenizer to encode the characters as integers
tokenizer = Tokenizer()
tokenizer.fit_on_texts(data)

# Encode the input data as integers
X = tokenizer.texts_to_sequences(data)
X = np.array(X).reshape(1, 10, 1)
y = np.roll(X, -1)
y = to_categorical(y, num_classes=2)
puruo6ea

puruo6ea1#

将num_classes=2更改为num_classes=None解决了此问题。

相关问题