[英]Setting input shape for an NLP task in R(Rstudio) using keras 1D convolution layer, when it expects 3 dimensional input (a tensor)
I am using R programming language and using Keras API to build a functional 1D CNN. 我使用R编程语言并使用Keras API构建功能性1D CNN。
I have a matrix of my dataset of the following shape rows*features (6000*1024). 我有一个以下形状行*特征(6000 * 1024)的数据集矩阵。
The input layer is set using the following code: 使用以下代码设置输入层:
input_layer = layer_input(shape = 1024, batch_shape = c(nrow(train_matrix),1024), dtype = 'float64')
and then I am building a 1d conv layer as follows: 然后我建立一个1d转换层,如下所示:
conv1 = input_layer %>% layer_conv_1d(filters = 32, kernel_size = 50, strides = 10, input_shape = 1024, batch_input_shape = list(NULL, 1024) ,dtype = 'float64', activation = 'relu' )
But I get the following error: 但是我收到以下错误:
Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Input 0 is incompatible with layer conv1d: expected ndim=3, found ndim=2 py_call_impl出错(callable,dots $ args,dots $ keywords):ValueError:输入0与图层conv1d不兼容:expected ndim = 3,found ndim = 2
I believe it is due to the fact that 1D cnn layer expects the input in the following form 我相信这是因为1D cnn层需要以下列形式输入
Input shape: 3D tensor with shape: (batch_size, steps, input_dim) 输入形状:具有形状的3D张量:(batch_size,steps,input_dim)
I understand that I have to reshape my data as (NULL, nrow(train_matrix), 1
; as this has been suggested in various answer for the same issue arising for keras when used in Python. 我知道我必须将我的数据重新(NULL, nrow(train_matrix), 1
为(NULL, nrow(train_matrix), 1
;因为在Python中使用keras时出现的相同问题的各种答案中都提到了这一点。
If I am right, 如果我是对的,
also if my understanding is wrong what should be done otherwise ? 如果我的理解是错的,那么应该做什么呢?
The input layer was producing a 2d tensor, whereas 1d convolutional layer expects a 3d tensor as input. 输入层产生2d张量,而1d卷积层需要3d张量作为输入。 A good explanation of as to why keras expects it and how should you reshape your 2d input to 3d can be found in this answer . 关于为什么keras期望它以及如何将你的2d输入重塑为3d的一个很好的解释可以在这个答案中找到。
I used Keras R API function k_reshape( 2d_tensor, (list_of_new_dims))
--> k_reshape(input_layer, list(nrow(train_matrix), num_of_feature_vectors, 1)
. 我使用了k_reshape( 2d_tensor, (list_of_new_dims))
R API函数k_reshape( 2d_tensor, (list_of_new_dims))
- > k_reshape(input_layer, list(nrow(train_matrix), num_of_feature_vectors, 1)
。
nrow(train_matrix)
- the total number of rows in my matrix (no of samples) nrow(train_matrix)
- 矩阵中的总行数(没有样本)
num_of_feature_vectors
- total number of columns in matrix (total number of features) num_of_feature_vectors
- 矩阵中的总列数( num_of_feature_vectors
总数)
1
- i want a 3d tensor with only 1 element at z axis, therefore z axis is initialised to 1 1
- 我想要一个在z轴上只有1个元素的3d张量,因此z轴初始化为1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.