简体   繁体   English

使用 Conv1D 层时出现 ValueError

[英]ValueError when using Conv1D layer

I'm trying to understand how to use a Conv1D layer to extract features out of a vector.我试图了解如何使用 Conv1D 层从向量中提取特征。 Here's my code:这是我的代码:

import tensorflow as tf
from tensorflow.keras import models, layers
import numpy as np

# make 100 40000-dimensional vectors:
x = []
for i in range(100):
  array_of_random_floats = np.random.random_sample((40000))
  x.append(array_of_random_floats)
x = np.asarray(x)

# make 100 80000-dimensional vectors:
y = []
for i in range(100):
  array_of_random_floats = np.random.random_sample((80000))
  y.append(array_of_random_floats)
y = np.asarray(y)


model = models.Sequential([
  layers.Input((40000,)),
  layers.Conv1D(padding='same', kernel_initializer='Orthogonal', filters=16, kernel_size=16, activation=None, strides=2),
  # ...
  layers.Dense(80000)
])

model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])

history = model.fit(x=x,
                    y=y,
                    epochs=100)

This produces the following error:这会产生以下错误:

ValueError: Input 0 of layer conv1d_20 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 40000)

I'm a bit confused as the documentation for the Conv1D layer seems to suggest that it can process vectors...我有点困惑,因为 Conv1D 层的文档似乎表明它可以处理向量......

It seems it's just an error with your dimensions.看来这只是您的尺寸错误。 I found that it works if you specify the input shape in your Conv1D layer and expand x 's dimensions.我发现如果您在 Conv1D 图层中指定输入形状并扩展x的尺寸,它会起作用。

model = models.Sequential([
  layers.Conv1D(..., input_shape=(None, 40000)),
  # ...
  layers.Dense(80000)
])

and

history = model.fit(x=np.expand_dims(x, 1), y=y, epochs=100)

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

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