【问题标题】:Keras error when trying to replicate Python results尝试复制 Python 结果时出现 Keras 错误
【发布时间】:2019-08-03 16:48:37
【问题描述】:

我正在尝试在一些文本分类数据上复制 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 keras


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我已经移除了嵌入层,看起来还可以。我添加了一个编辑/附加问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多