2017-08-07 77 views
1

我試圖修改Keras中圖層的輸出。我有一個編碼器,將時間序列轉換爲潛在空間,然後,對於每個時間序列壓縮,我想在時間序列中添加一些數字。修改Keras中的圖層權重

比如我有:

input_d = Input((100,)) 
h1_d = Reshape((100, 1))(input_d) 
h2_d = LSTM(150, return_sequences=True)(h1_d) 
h3_d = TimeDistributed(Dense(64, activation='linear'))(h2_d) 
h4_d = LSTM(150)(h3_d) 
output_d = Dense(30, activation='linear')(h4_d) 

我想要做這樣的事情:

new_weights = [] 
for i in outputs_d.weights: 
    new_weights.append(np.vstack(([1,2,3], i))) 

但問題是,我不知道在哪個時刻,我能做到這一點,因爲如果在ouput_d之後寫了一個Lambda層,我無法訪問權重。

回答

1

我發現要做這樣的事情的唯一方法是通過實現所需的功能作爲回調,在那裏你可以訪問模型,從而通過self.model.trainable_weightsself.model.get_weights()訪問權重。