我的虚拟数据集中有12个长度为200的向量,每个向量代表一个样本 . 假设 x_train
是一个形状为 (12, 200)
的数组 .
当我做:
model = Sequential()
model.add(Conv1D(2, 4, input_shape=(1, 200)))
我收到错误:
ValueError: Error when checking model input: expected conv1d_1_input to have 3 dimensions, but got array with shape (12, 200)
如何正确地塑造输入数组?
这是我更新的脚本:
data = np.loadtxt('temp/data.csv', delimiter=' ')
trainData = []
testData = []
trainlabels = []
testlabels = []
with open('temp/trainlabels', 'r') as f:
trainLabelFile = list(csv.reader(f))
with open('temp/testlabels', 'r') as f:
testLabelFile = list(csv.reader(f))
for i in range(2):
for idx in trainLabelFile[i]:
trainData.append(data[int(idx)])
# append 0 to labels for neg, 1 for pos
trainlabels.append(i)
for i in range(2):
for idx in testLabelFile[i]:
testData.append(data[int(idx)])
# append 0 to labels for neg, 1 for pos
testlabels.append(i)
# print(trainData.shape)
X = np.array(trainData)
Y = np.array(trainlabels)
X2 = np.array(testData)
Y2 = np.array(testlabels)
model = Sequential()
model.add(Conv1D(1, 1, input_shape=(12, 1, 200)))
opt = 'adam'
model.compile(loss='mean_squared_error', optimizer=opt, metrics=['accuracy'])
model.fit(X, Y, epochs=epochs)
我现在收到一个新错误:
ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4
2 回答
您需要根据Conv1D图层输入格式重新整形输入数据 -
(batch_size, steps, input_dim)
. 尝试在Keras documentation中,写入
input_shape
是一个形状为(batch_size, steps, input_dim)
的3D张量 . 含义如下:batch_size
是样本数 . 这对你来说是12
.steps
是数据的时间维度 . 您可以将其设置为1
,因为数据中只有一个通道 .input_dim
是一个样本的维度 . 这对你来说是200
.回答您的问题是将您的数据重塑为
(12,1,200)
.