R to_categorical用于使用Keras进行序列标记

时间:2018-05-31 12:37:07

标签: r keras sequence labeling

我有一个如此定义的数据框 我正在尝试为深度学习问题创建序列标签输入。 所以我为每个句子元素都有标签,我为句子元素创建WordIndex的向量,为10个维度填充它们,对句子元素的标签执行相同的操作(为标签创建TagIndex,将它们填充到10尺寸)。 然后我需要将TagIndices转换为分类变量。那是错误出现的时候。任何帮助都会很棒。这是正确的做法吗?

SentenceID = c(1,1,1,1,2,2,2,3,3,3,3,3,3,3,3)
Tokens = c("I","went","to","school","nobody","can","find","some","people","know","what","they","are","doing","now")
WordIndex = c(3,4,7,8,9,10,12,54,34,66,33,89,87,23,22)
TagIndex = c(1,3,2,4,1,3,4,1,2,4,3,4,2,3,4)

df = data.frame(SentenceID, Tokens, WordIndex, TagIndex)

lst <- split(df$WordIndex, f = df$SentenceID)

lstWord2 <- lapply(lst, function(x){
  if (length(x) < 10){
    x2 <- c(x, rep(0, 10 - length(x)))
  }
  return(x2)
})

lstTag <- split(df$TagIndex, f = df$SentenceID)

lstTag2 <- lapply(lstTag, function(x){
  if (length(x) < 10){
    x2 <- c(x, rep(0, 10 - length(x)))
  }
  return(x2)
})

is.vector(lstTag2)

y <- to_categorical(lstTag2, num_classes = NULL)

我得到的错误就是这个。

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

Detailed traceback: 
  File "C:\Users\balak\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\keras\utils\np_utils.py", line 22, in to_categorical
    y = np.array(y, dtype='int')

1 个答案:

答案 0 :(得分:0)

我想to_categorical函数要求输入是一个矩阵,这样做可以使它起作用:

SentenceID = c(1,1,1,1,2,2,2,3,3,3,3,3,3,3,3)
Tokens = c("I","went","to","school","nobody","can","find","some","people","know","what","they","are","doing","now")
WordIndex = c(3,4,7,8,9,10,12,54,34,66,33,89,87,23,22)
TagIndex = c(1,3,2,4,1,3,4,1,2,4,3,4,2,3,4)

df = data.frame(SentenceID, Tokens, WordIndex, TagIndex)

lst <- split(df$WordIndex, f = df$SentenceID)

lstWord2 <- lapply(lst, function(x){
  if (length(x) < 10){
    x2 <- c(x, rep(0, 10 - length(x)))
  }
  return(x2)
})

lstTag <- split(df$TagIndex, f = df$SentenceID)

lstTag2 <- lapply(lstTag, function(x){
  if (length(x) < 10){
    x2 <- c(x, rep(0, 10 - length(x)))
  }
  return(x2)
})


y <- to_categorical(as.matrix(lstTag2), num_classes = NULL)

我明白了:

> y
, , 1

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    0    1    1    1    1    1     1
[2,]    0    0    0    1    1    1    1    1    1     1
[3,]    0    0    0    0    0    0    0    0    1     1

, , 2

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    1    0    0    0    0    0    0    0    0     0
[3,]    1    0    0    0    0    0    0    0    0     0

, , 3

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    1    0    0    0    0    0    0     0
[2,]    0    0    0    0    0    0    0    0    0     0
[3,]    0    1    0    0    0    1    0    0    0     0

, , 4

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    1    0    0    0    0    0    0    0     0
[2,]    0    1    0    0    0    0    0    0    0     0
[3,]    0    0    0    1    0    0    1    0    0     0

, , 5

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    1    0    0    0    0    0     0
[2,]    0    0    1    0    0    0    0    0    0     0
[3,]    0    0    1    0    1    0    0    1    0     0