r - InvalidArgumentError : indices[127, 7] = 43 不在 Keras R 中的 [0, 43) 中

标签 r indexing keras tokenize

问题是相关的
至: InvalidArgumentError (see above for traceback): indices[1] = 10 is not in [0, 10)
我需要它用于 R,因此是上面链接中给出的另一种解决方案。

maxlen <- 40
chars <- c("'",  "-",  " ",  "!",  "\"", "(",  ")",  ",",  ".",  ":",  ";",  "?",  "[",  "]",  "_",  "=",  "0", "a",  "b",  "c",  "d",  "e", "f",  "g",  "h",  "i",  "j",  "k",  "l",  "m",  "n",  "o",  "p",  "q",  "r",  "s",  "t",  "u",  "v",  "w",  "x",  "y",  "z")



tokenizer <- text_tokenizer(char_level = T, filters = NULL)

tokenizer %>% fit_text_tokenizer(chars)
unlist(tokenizer$word_index)

输出是:
 '  -     !  "  (  )  ,  .  :  ;  ?  [  ]  _  =  0  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z 
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 

如何更改索引,使其在 text_tokenizer 中从 0 开始而不是从 1 开始?

运行 fit() 后得到的错误如下:
InvalidArgumentError: indices[127,7] = 43 is not in [0, 43)
     [[Node: embedding_3/embedding_lookup = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:@training_1/RMSprop/Assign_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_3/embeddings/read, embedding_3/Cast, training_1/RMSprop/gradients/embedding_3/embedding_lookup_grad/concat/axis)]]

但我相信改变索引会解决我的问题。

最佳答案

索引 0 通常保留给 填充 所以从 0 开始你的实际字符索引也不是一个明智的主意。相反,您应该冒险进入 Embedding 层,并按照 documentation 的建议将输入大小加 1:

input_dim: int > 0. Size of the vocabulary, i.e. maximum integer index + 1.



在您的情况下,这将是 43 + 1 = 44。

关于r - InvalidArgumentError : indices[127, 7] = 43 不在 Keras R 中的 [0, 43) 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968865/

相关文章:

r - 如何在R中计算列中的行

regex - R grep匹配点

mysql - 不使用索引进行连接并且索引完全相同

excel - 在 Excel 中按字母顺序排序的顺序公式

python - 使 tensorflow 模型中的一个输入保持不变

tensorflow - 将 Pytorch 程序翻译成 Keras : different results

tensorflow - 值错误 : Expect x to be a non-empty array or dataset (Tensor Flow lite model maker on Collab)

r - 如何在 ggplot2 中绘制 "geom_smooth"时删除 SE 上的填充区域?

r - 在R中的ddply中使用colwise, is.numeric进行分位数计算

python - 替换两列中的值pandas一个条件