[英]How do I shape my input data for use with Conv1D in keras?
我的虛擬數據集中有 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
您需要根據 Conv1D 層輸入格式 - (batch_size, steps, input_dim)
重塑輸入數據。 嘗試
x_train = x_train.reshape(x_train.shape[0], 1, x_train.shape[1])
有點晚了,但只是為了回答這個問題,輸入形狀是(不同模型批次的數量,每個模型的數據數量,數據的維度)。 在你的情況下(12,200,1)
在Keras 文檔中,寫到input_shape
是一個形狀為(batch_size, steps, input_dim)
的 3D 張量。 含義如下:
batch_size
是樣本數。 對你來說是12
。steps
是數據的時間維度。 您可以將其設置為1
,因為數據中只有一個通道。input_dim
是一個樣本的維度。 對你來說是200
。 您的問題的答案是將您的數據重塑為(12,1,200)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.