简体   繁体   English

为什么在尝试运行 Conv1D 层时会出现 Conv2D 错误?

[英]Why do I get a Conv2D error trying to run Conv1D layer?

I am trying to write a simple 1 dimensional convolution with a regression (1 dimensional float) output.我正在尝试编写一个简单的一维卷积与回归(一维浮点数)output。

model = Sequential()
model.add(Conv1D(filters=1, kernel_size=8, activation='relu'))
model.add(Dense(1, 'softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x = x_train, y = y_train, epochs=3)

This gives me the error:这给了我错误:

TypeError: Exception encountered when calling layer "conv1d" (type Conv1D).
Input 'filter' of 'Conv2D' Op has type float32 that does not match type int32 of argument 'input'.
Call arguments received:
• inputs=tf.Tensor(shape=(None, 30931, 4), dtype=int32)

Even if my code is wrong, how is it possible I am getting a Conv2D error without even having a Conv2D layer?即使我的代码是错误的,我怎么可能在没有 Conv2D 层的情况下收到 Conv2D 错误?

x_train is a numpy array of 3361 training examples, each 1d array of length 30931, with 4 channels of np.int32 data. x_train 是一个包含 3361 个训练示例的 numpy 数组,每个一维数组的长度为 30931,具有 4 个通道的 np.int32 数据。 shape = (3361,30931, 4)形状 = (3361,30931, 4)

y_train is a numpy array of 3361 np.float64 values I am training my.network to recognize. y_train 是一个 numpy 数组,包含 3361 个 np.float64 值,我正在训练 my.network 进行识别。

Should this format of input data work?这种输入数据格式应该有效吗? Or do I need to transform it or use another data type?还是我需要对其进行转换或使用其他数据类型?

Do I need an input_shape parameter in my Conv1D layer?我的 Conv1D 层中是否需要 input_shape 参数? If so, what should it be?如果是这样,它应该是什么?

I realize this is oversimplified, and plan a much more complex.network to train against many more examples, but just want this running first.我意识到这过于简单化了,并计划了一个更复杂的网络来训练更多的例子,但只想先运行它。

Your x_train data should be of the data type float .您的x_train数据应该是数据类型float Also, you usually flatten your 2D data into 1D or apply some global pooling operation before feeding it into a softmax output layer:此外,您通常会将 2D 数据展平为 1D 或应用一些全局池化操作,然后再将其送入softmax output 层:

import tensorflow as tf

x_train = tf.random.normal((10,30931, 4), dtype=tf.float32)
y_train = tf.random.uniform((10,), maxval=2, dtype=tf.int32)
y_train = tf.keras.utils.to_categorical(y_train, 2)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(filters=1, kernel_size=8, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(2, 'softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x = x_train, y = y_train, epochs=3)

Regarding your error message, both the Conv1D and Conv2D layer use the tf.nn.convolution operation internally .关于您的错误信息, Conv1DConv2D层都在内部使用了tf.nn.convolution操作。 Interestingly, the problem is caused by the parameter filter , which has a float data type and cannot handle integer inputs.有趣的是,问题是由参数filter引起的,它具有 float 数据类型,无法处理 integer 输入。

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

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