简体   繁体   English

为 R 中的一维数据运行 CNN 时出错

[英]Error while running CNN for 1 dimensional data in R

I am trying to run 1 dimensional CNN in R using keras package.我正在尝试使用keras包在 R 中运行一维 CNN。 I am trying to create one-dimensional Convolutional Neural Network (CNN) architecture with the following specification我正在尝试使用以下规范创建一维卷积神经网络 (CNN) 架构

在此处输入图片说明

library(keras)
library(deepviz)

#create a neural network with a convolutional layer and train the model
model <- keras_model_sequential() %>%
  layer_conv_1d(filters=32, kernel_size=4, activation="relu", input_shape=c(100, 10)) %>%
  layer_max_pooling_1d(pool_size=2) %>%
  layer_conv_1d(filters=64, kernel_size=4, activation="relu") %>%
  layer_max_pooling_1d(pool_size=5) %>%
  layer_conv_1d(filters=128, kernel_size=4, activation="relu") %>%
  layer_max_pooling_1d(pool_size=5) %>%
  layer_conv_1d(filters=256, kernel_size=4, activation="relu") %>%
  layer_max_pooling_1d(pool_size=5) %>%
  layer_dropout(rate=0.4) %>%
  layer_flatten() %>%
  layer_dense(units=100, activation="relu") %>%
  layer_dropout(rate=0.2) %>%
  layer_dense(units=1, activation="linear")

But it is giving me following error但它给了我以下错误

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Negative dimension size caused by subtracting 4 from 1 for 'conv1d_20/conv1d' (op: 'Conv2D') with input shapes: [?,1,1,128], [1,4,128,256]. py_call_impl(callable, dots$args, dots$keywords) 中的错误:ValueError: 负尺寸大小是由 'conv1d_20/conv1d' (op: 'Conv2D') 的 1 减去 4 引起的,输入形状为:[?,1,1,128] , [1,4,128,256]。

How to solve the error?如何解决错误?

Another question, how to optimise the filters , kernel_size , pool_size , rate , units ?另一个问题,如何优化filterskernel_sizepool_sizerateunits In my question input_shape=c(100, 10) is an arbitrary value.在我的问题中input_shape=c(100, 10)是一个任意值。 How to decide about the input size?如何决定输入大小?

You have too many Max-Pooling layers, the max pooling layer reduces the dimension of the inputted vector by factor of its parameter.你有太多的 Max-Pooling 层,max pooling 层将输入向量的维度减少了它的参数。

Try to reduce the pool_size parameters , or alternatively remove the last 2 max-pooling layers.尝试减少 pool_size 参数,或者删除最后 2 个最大池化层。 A value you can try is pool_size=2 for all layers.对于所有层,您可以尝试的值是 pool_size=2。

As for the parameters you should learn of the meaning of them: Here you can find an explanation of the convolution layer and max pooling layer parameters like filters , kernel size and pool size: Convolutional layer至于参数,你应该了解它们的含义:在这里你可以找到卷积层和最大池化层参数的解释,如过滤器、内核大小和池大小:卷积层

The dropout layer is a regularization which maximize the effectiveness of the layer weights , every epoch it zeroes different percent (size of "rate" parameter) of the weights . dropout 层是一种正则化,它最大化层权重的有效性,每个时期它都将权重的不同百分比(“速率”参数的大小)归零。 the larger the rate - you have less overfitting but training time is longer.比率越大 - 过拟合越少,但训练时间越长。 learn about it here: Dropout layer在此处了解它: Dropout 层

The units is the size of the Fully Connected layer.单位是全连接层的大小。 Fully Connected layer 全连接层

The input shape is a dimensions of your data, when the number of records does not count.当记录数不算数时,输入形状是数据的维度。 In 1d vectors it is (N,C) when N is the vector length and C is number of channels you have, if you have 1 channel it is (N,1).在 1d 向量中它是 (N,C),当 N 是向量长度并且 C 是您拥有的通道数时,如果您有 1 个通道,则它是 (N,1)。 In 2d vectors it is (Height,Width,Channels).在 2d 向量中它是 (Height,Width,Channels)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM