Keras模型-模型规格并将经过训练的模型应用于测试数据

时间:2019-09-09 16:27:31

标签: r keras

我有一些text数据和一个多类class变量,分别标记为123text数据称为x_train,而class数据称为y_train。我有一个非常艰巨的分类任务(想像要尝试艰难地预测股市),因此我不希望从模型中获得很高的准确性,但我希望获得的结果略高于33%,但并不比翻转更好因为我有3节课我运行的最佳模型(使用TF-IDF)获得0.45的精度。我希望使用Keras可以获得更好的准确性。

我的文档很长,我设置了maxlen = 25000num_words = 20000并填充了序列。

问题1)

您知道我可以尝试的任何“开箱即用”参数通常在您的keras模型上都能正常工作吗?

我有以下模型:

batch_size <- 32
embedding_dims <- 50
filters <- 250
kernel_size <- 3
hidden_dims <- 250
epochs <- 2


model <- keras_model_sequential() %>%  

  layer_embedding(max_features, embedding_dims, input_length = maxlen) %>%
  layer_dropout(0.2) %>%
  layer_conv_1d(
    filters, kernel_size, 
    padding = "valid", activation = "relu", strides = 1
  ) %>%
  layer_global_max_pooling_1d() %>%
  layer_dense(hidden_dims) %>%
  layer_dropout(0.2) %>%
  layer_activation("relu") %>%
  layer_dense(3) %>%
  layer_activation("softmax")


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

history <- model %>%
  keras::fit(
    x_train,
    to_categorical(y_train -1), # I set -1 here since keras expects 0, 1, 2 class and not 1, 2, 3 as I currently have.
    batch_size = batch_size, 
    epochs = epochs, 
    validation_split = 0.3
  )

哪个给我这个:

Train on 18895 samples, validate on 8098 samples
Epoch 1/2
18895/18895 [==============================] - 710s 38ms/sample - loss: 1.0991 - acc: 0.3344 - val_loss: 1.0985 - val_acc: 0.3405
Epoch 2/2
18895/18895 [==============================] - 708s 37ms/sample - loss: 1.0988 - acc: 0.3359 - val_loss: 1.0992 - val_acc: 0.3269

我使用以下方法进行预测

preds <- model %>%
  predict_classes(x_test, batch_size = 32)

所有预测都是0

训练和损失图:

enter image description here

0 个答案:

没有答案