[英]Simple network for arbitrary shape input
我試圖創建自動編碼Keras
與Tensorflow
后端。 我按照本教程制作自己的教程 。 輸入網絡的方法是任意的,即每個樣本都是一個二維列,具有固定的列數(在這種情況下為12
),但行的范圍在4
到24
之間。
到目前為止,我嘗試過的是:
# Generating random data
myTraces = []
for i in range(100):
num_events = random.randint(4, 24)
traceTmp = np.random.randint(2, size=(num_events, 12))
myTraces.append(traceTmp)
myTraces = np.array(myTraces) # (read Note down below)
這是我的樣本模型
input = Input(shape=(None, 12))
x = Conv1D(64, 3, padding='same', activation='relu')(input)
x = MaxPool1D(strides=2, pool_size=2)(x)
x = Conv1D(128, 3, padding='same', activation='relu')(x)
x = UpSampling1D(2)(x)
x = Conv1D(64, 3, padding='same', activation='relu')(x)
x = Conv1D(12, 1, padding='same', activation='relu')(x)
model = Model(input, x)
model.compile(optimizer='adadelta', loss='binary_crossentropy')
model.fit(myTraces, myTraces, epochs=50, batch_size=10, shuffle=True, validation_data=(myTraces, myTraces))
注意 :根據Keras Doc ,它說輸入應該是一個numpy數組,如果這樣做,則會出現以下錯誤:
ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (100, 1)
如果我不將其轉換為numpy數組,並將其作為numpy數組的列表,則會出現以下錯誤:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 100 arrays: [array([[0, 1, 0, 0 ...
我不知道我在做什么錯。 我也是Keras
的新手。 我真的很感謝對此的任何幫助。
Numpy不知道如何處理行大小不同的數組列表(請參閱此答案 )。 當您使用traceTmp調用np.array時,它將返回一個數組列表,而不是3D數組(形狀為(100,1)的數組表示100個數組的列表)。 Keras也將需要一個齊整的數組,這意味着所有輸入數組都應具有相同的形狀。
您可以做的就是用零填充數組,使它們都具有形狀(24,12):然后np.array可以返回3維數組,而keras輸入層不會抱怨。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.