无效参数:indices[0,0] = -4 不在 [0, 40405) 中

2024-05-07

我有一个模型可以处理一些数据。我在数据集中添加了一些标记化的单词数据(为简洁起见有些被截断):

vocab_size = len(tokenizer.word_index) + 1
comment_texts = df.comment_text.values

tokenizer = Tokenizer(num_words=num_words)

tokenizer.fit_on_texts(comment_texts)
comment_seq = tokenizer.texts_to_sequences(comment_texts)
maxtrainlen = max_length(comment_seq)
comment_train = pad_sequences(comment_seq, maxlen=maxtrainlen, padding='post')
vocab_size = len(tokenizer.word_index) + 1

df.comment_text = comment_train

x = df.drop('label', 1) # the thing I'm training

labels = df['label'].values  # Also known as Y

x_train, x_test, y_train, y_test = train_test_split(
    x, labels, test_size=0.2, random_state=1337)        

n_cols = x_train.shape[1]

embedding_dim = 100  # TODO: why?

model = Sequential([
            Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_shape=(n_cols,)),
            LSTM(32),
            Dense(32, activation='relu'),
            Dense(512, activation='relu'),
            Dense(12, activation='softmax'),  # for an unknown type, we don't account for that while training
        ])
model.summary()

model.compile(optimizer='rmsprop',
                      loss='categorical_crossentropy',
                      metrics=['acc'])

# convert the y_train to a one hot encoded variable
encoder = LabelEncoder()
encoder.fit(labels)  # fit on all the labels
encoded_Y = encoder.transform(y_train)  # encode on y_train
one_hot_y = np_utils.to_categorical(encoded_Y)

model.fit(x_train, one_hot_y, epochs=10, batch_size=16)

现在,我收到此错误:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, 12, 100)           4040500   
_________________________________________________________________
lstm (LSTM)                  (None, 32)                17024     
_________________________________________________________________
dense (Dense)                (None, 32)                1056      
_________________________________________________________________
dense_1 (Dense)              (None, 512)               16896     
_________________________________________________________________
dense_2 (Dense)              (None, 12)                6156      
=================================================================
Total params: 4,081,632
Trainable params: 4,081,632
Non-trainable params: 0
_________________________________________________________________
Train on 4702 samples
Epoch 1/10
2020-03-04 22:37:59.499238: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: indices[0,0] = -4 is not in [0, 40405)

我认为这一定来自我的 comment_text 列,因为这是我添加的唯一内容。

Here is what comment_text looks like before I make the substitution: before

And here is after: after

我的完整代码(在进行更改之前)在这里:https://colab.research.google.com/drive/1y8Lhxa_DROZg0at3VR98fi5WCcunUhyc#scrollTo=hpEoqR4ne9TO https://colab.research.google.com/drive/1y8Lhxa_DROZg0at3VR98fi5WCcunUhyc#scrollTo=hpEoqR4ne9TO


你应该训练comment_train,不与x它正在采取未知的一切df.

The embedding_dim=100可以自由选择。这就像隐藏层中的单元数。您可以调整此参数来找到最适合您的模型的参数,也可以调整隐藏层中的单元数量。


在您的情况下,您将需要一个具有两个或多个输入的模型:

  • 评论的一次输入,通过嵌入和处理文本
  • 其余数据的另一个输入,可能通过标准网络传递。

在某些时候,您将连接这两个分支并继续前进。

这个链接有一个很好的教程函数式API模型并显示具有两个文本输入和一个额外输入的模型:https://www.tensorflow.org/guide/keras/functional https://www.tensorflow.org/guide/keras/functional

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无效参数:indices[0,0] = -4 不在 [0, 40405) 中 的相关文章

随机推荐

  • 如何替换cloudinary中的图像并保持相同的URL?

    我在 cloudinary 中有一个图像 我想替换它但保留原始 URL 可能吗 是的 可以更新图像并保持相同的 URL 当您上传新图像时 您希望使其 public id 与您尝试替换的现有图像相同 然后您的 Cloudinary 帐户将被更
  • 如何在 Azure 中调试测试发送?

    I am trying to setup a successfull push notifications between my Net backend and ios client in azure I followed this htt
  • MonoDevelop - 代码窗口颜色方案

    MonoDevelop 中有没有办法自定义代码窗口配色方案 我想要类似的东西黑曜石之子 http studiostyl es schemes son of obsidian如果可能的话 是的 转到 MonoDevelop 语法突出显示选项面
  • 继承模板的排序顺序

    使用 Sitecore 6 6 我们正在构建一些继承许多基本模板的模板 这对于重用和标准化我们的代码非常有用 因为所有字段都存储在常量文件中以便于引用 然而 我们似乎无法定义这些继承模板的显示顺序 无论如何 模板似乎都遵循它们添加的原始顺序
  • 如何在cxf-maven-plugin中定义wsdl的密码

    我想使用 cxf maven plugin 根据此文档从 WSDL 生成 Java 代码 http cxf apache org docs maven cxf codegen plugin wsdl to java html http cx
  • Dingo API 删除“数据”信封

    有没有一种简单的方法可以从 Dingo API 响应中删除 数据 信封 当我使用这个 Transformer 来转换用户模型时 class UserTransformer extends EloquentModelTransformer L
  • 尝试 SSH 时设备的 ioctl 不合适

    我正在尝试通过 SSH 连接几台服务器并尝试获取sudo l每个服务器的输出 下面是我正在执行的脚本 bin bash serverlist tmp servers while IFS read r server netgroup user
  • unity 3d 根据加速度计旋转游戏对象

    我想做一款类似 神庙逃亡 的游戏 我需要根据设备倾斜的程度来旋转播放器的平台 我正在尝试加速计 但无法使游戏对象倾斜 请指导我 谢谢 这是我的代码 我之前在注释中使用了代码 现在我尝试使用注释中的代码 public class tilt M
  • 如何将 Enzyme Shallow 与 Jest 快照结合使用

    我正在尝试使用shallow https github com airbnb enzyme blob master docs api shallow md from enzyme https github com airbnb enzyme
  • 如何在 iText 中获取新页面

    去新页面有点问题pdfContentByte 我使用下面的代码将数据放在第一页之后到下一页 但不幸的是iText不生成新页面 step1 itextDocument new com itextpdf text Document PageSi
  • 使用 Metro 风格应用程序启动桌面应用程序

    有没有办法从 Windows 8 上的 Metro 风格应用程序启动桌面应用程序 我正在尝试创建一些简单的桌面应用程序快捷方式 以替换开始屏幕上看起来不合适的桌面图标 我只需要一些超级简单的东西 最好是用 C 编写 以便在应用程序加载后立即
  • 如何比较 ASCII 值

    我想将字母的 ASCII 值存储到变量中 我该怎么做 例如 r ASCII variable 82 main character character read from a file variable r ascii in this cas
  • Python-按多列分组并获取最大值或总和

    我有兴趣获得产品价格的最大值 这是输入数据 我想按州 国家 地区分组 我如何对这两列进行分组以获得价格的最大值 import csv import locale from itertools import groupby locale se
  • paypal数字商品集成如何启用访客支付?

    您好 我正在使用贝宝集成向导进行数字商品结帐 PHP https www paypal labs com integrationwizard https www paypal labs com integrationwizard 它应该支持
  • 如何在 Lucene 5 中获取 Span Term 查询的匹配范围?

    在 Lucene 中 要获取术语周围的单词 建议使用跨度查询 有很好的演练http lucidworks com blog accessing words around a positional match in lucene http l
  • scala 使用 GMPUtil 处理 pidigits

    Rex Kerr 发布了有关在 scala 中使用 GMP 的信息 特别是运行 pidigits 程序 libjpargmp so 使用 GmpUtil c 生成 我的问题是 在哪里可以找到 GMPUtil c 我的谷歌搜索没有发现任何东西
  • CSS:为表格中选定的行设置颜色

    我需要将以下功能添加到我的表中 当用户单击一行 选择它 时 该行会用颜色标记 FFCF8B 与hover 我试过 newspaper b tbody tr selected td 但它不起作用 newspaper b border coll
  • 当所有通道都关闭时中断 select 语句

    我有两个独立生成数据的 goroutine 每个将其发送到一个通道 在我的主 goroutine 中 我想在每个输出进入时使用它们 但不关心它们进入的顺序 每个通道在耗尽其输出时都会自行关闭 虽然 select 语句是像这样独立使用输入的最
  • PHP:数组可以有一个数组作为键值对中的键吗?

    我得到这个数组的 非法偏移类型 public static CATS AND TYPES array Statement Administration array self CAT STATEMENT ADMIN gt Document A
  • 无效参数:indices[0,0] = -4 不在 [0, 40405) 中

    我有一个模型可以处理一些数据 我在数据集中添加了一些标记化的单词数据 为简洁起见有些被截断 vocab size len tokenizer word index 1 comment texts df comment text values