在神经网络中使用卷积层

时间:2019-04-17 12:35:10

标签: tensorflow machine-learning keras

我试图在我的Keras神经网络中将Conv1D层用作输入层,我理解这是一个时间卷积层。我了解的是,它接受输入,并使用内核大小(具有一组功能)来创建新的卷积层。我的训练数据是一个长为231的长的热编码张量。我正在努力了解Conv1D层的输入如何/是什么?

我的x_train.shape([1])是231。

当我使用时:

n_cols = x_train.shape([1])

model.add(Conv1D(128, 11, activation = 'relu', input_shape = (n_cols,)))

(我使用11是因为据我所知,我相信它一次从一个热向量中获取11个值)

我收到错误消息:

ValueError: Input 0 of layer conv1d is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 231]

完整追溯:

Traceback (most recent call last):
  File "/Volumes/Hajar's     HDD/MSc_data/large_proteins/ml_network.py", line 18, in <module>
        model.add(Conv1D(128, 11, activation = 'relu', input_shape     = (n_cols,)))
  File "/anaconda3/lib/python3.7/site-    packages/tensorflow/python/training/tracking/base.py", line 456, in     _method_wrapper
    result = method(self, *args, **kwargs)
  File "/anaconda3/lib/python3.7/site-    packages/tensorflow/python/keras/engine/sequential.py", line 169, in     add
    layer(x)
  File "/anaconda3/lib/python3.7/site-    packages/tensorflow/python/keras/engine/base_layer.py", line 589, in     __call__
    self.name)
  File "/anaconda3/lib/python3.7/site-    packages/tensorflow/python/keras/engine/input_spec.py", line 124, in     assert_input_compatibility
    str(x.shape.as_list()))

我尝试更改内核大小,但仍然遇到相同的错误。我想也许我之前需要另一个输入层。对于内核大小,这是特征数(即231)吗?

1 个答案:

答案 0 :(得分:1)

您将需要输入shape=(231, x ),其中x是第二维。您可以尝试像这样使用x = 1

import numpy as np

n_cols = x_train.shape([1])
x_train = np.reshape( x_train , ( -1 , n_cols , 1 )  )

model.add(Conv1D(128, 11, activation = 'relu', input_shape = (n_cols,1)))

Conv1D要求格式为(batch_size, a, b )的二维输入,其中ab是非零尺寸。