简体   繁体   中英

ValueError when using Conv1D layer

I'm trying to understand how to use a Conv1D layer to extract features out of a vector. 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...

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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