我正试图理解如何使用Conv1D层从矢量中提取特征。下面是我的代码:
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)
这会产生以下错误:
ValueError: Input 0 of layer conv1d_20 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 40000)
我有点困惑,因为Conv1D层的文档似乎表明它可以处理向量…
这似乎只是你的尺寸错误。我发现如果你在Conv1D层中指定输入形状并扩展x
的维度,它就会起作用。
model = models.Sequential([
layers.Conv1D(..., input_shape=(None, 40000)),
# ...
layers.Dense(80000)
])
和
history = model.fit(x=np.expand_dims(x, 1), y=y, epochs=100)