我试图在一些文本分类数据上复制Python Keras模型,但是这样做时遇到错误。

Python代码(有效):

# Build the model
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

history = model.fit(xx_train, yy_train,
                    batch_size = batch_size,
                    epochs = epochs,
                    verbose = 1,
                    validation_split = 0.1)


R复制(在history上失败):

num_classes = 3
batch_size = 32
epochs = 10
max_words = 10000

model <- keras_model_sequential() %>%
  layer_embedding(input_dim = max_words, output_dim = num_classes) %>%
  layer_dense(units = 512, activation = "relu") %>%
  layer_dropout(0.5) %>%
  layer_dense(units = num_classes, activation = "softmax")

model %>% compile(
  optimizer = "adam",
  loss = "categorical_crossentropy",
  metrics = c("accuracy")
)


history <- model %>% fit(
  xx_train, yy_train,
  epochs = epochs,
  batch_size = batch_size,
  validation_split = 0.1
)


在复制Python模型的尝试之间,我看到的唯一“差异”是我必须添加output_dim = num_classes-Python版本似乎并不需要。

我在R代码上运行history时收到此错误。

Error in py_call_impl(callable, dots$args, dots$keywords) :
  InvalidArgumentError: Incompatible shapes: [32] vs. [32,10000]
     [[{{node metrics_4/acc/Equal}}]]

Detailed traceback:
  File "/data/users/msmith/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 780, in fit
    steps_name='steps_per_epoch')
  File "/data/users/msmith/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 363, in model_iteration
    batch_outs = f(ins_batch)
  File "/data/users/msmith/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3292, in __call__
    run_metadata=self.run_metadata)
  File "/data/users/msmith/.virtualenvs/r-reticulate/lib64/python3.6/site-packages/tensorflow/python/client/session.py", line 1458, in __call__
    run_metadata_ptr)


我知道该错误与shape有关,但是Python代码对相同的数据起作用。

在此先感谢您的帮助。

编辑:

我在这里遵循了建议:https://github.com/keras-team/keras/issues/11749

我已经降级到keras 2.2.2
我运行了以下pip3 install --user git+https://github.com/keras-team/keras.git -U,但是我在服务器上安装了几个版本的Python,并且不确定R是否可以找到此keras更新...

当我设置bactch_size = 1时,该模型可以工作,但每隔一个batch_size都会中断。

编辑:

有关Python实现的另一个问题。我在Python中执行以下操作:

tokenize.fit_on_texts(X_train) # only fit on train
xx_train = tokenize.texts_to_matrix(X_train)


但是在R我这样做:

xx_train <- texts_to_matrix(tokenize, X_train, mode = c("tfidf"
                                                        #"binary",
                                                        #"count", ,
                                                        #"freq"
                                                        ))



默认的Python text_to_matrix模式是什么?

最佳答案

在python版本中,您没有使用嵌入层,而在R版本中,您使用了嵌入层。
我不知道您使用的是案例,所以我不确定嵌入层是否应该存在。

关于python - 尝试复制Python结果时出现Keras错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57340341/

10-11 02:08
查看更多