我在 Keras 中設(shè)置了一個自動編碼器。我希望能夠根據(jù)預(yù)定的“精度”向量對輸入向量的特征進(jìn)行加權(quán)。這個連續(xù)值向量與輸入的長度相同,每個元素都在范圍內(nèi)[0, 1],對應(yīng)于對應(yīng)輸入元素的置信度,其中1為完全置信度,0為不置信度。對于每個示例,我都有一個精度向量。我已經(jīng)定義了一個考慮到這個精度向量的損失。在這里,低置信度特征的重建被降低權(quán)重。def MAEpw_wrapper(y_prec): def MAEpw(y_true, y_pred): return K.mean(K.square(y_prec * (y_pred - y_true))) return MAEpw我的問題是精度張量y_prec取決于批次。我希望能夠y_prec根據(jù)當(dāng)前批次進(jìn)行更新,以便每個精度向量與其觀察正確關(guān)聯(lián)。我做了以下工作:global y_precy_prec = K.variable(P[:32])這P是一個包含所有精度向量的 numpy 數(shù)組,其索引對應(yīng)于示例。我初始化y_prec為批量大小為 32 的正確形狀。然后我定義以下內(nèi)容DataGenerator:class DataGenerator(Sequence): def __init__(self, batch_size, y, shuffle=True): self.batch_size = batch_size self.y = y self.shuffle = shuffle self.on_epoch_end() def on_epoch_end(self): self.indexes = np.arange(len(self.y)) if self.shuffle == True: np.random.shuffle(self.indexes) def __len__(self): return int(np.floor(len(self.y) / self.batch_size)) def __getitem__(self, index): indexes = self.indexes[index * self.batch_size: (index+1) * self.batch_size] # Set precision vector. global y_prec new_y_prec = K.variable(P[indexes]) y_prec = K.update(y_prec, new_y_prec) # Get training examples. y = self.y[indexes] return y, y在這里,我的目標(biāo)是y_prec在生成批處理的同一函數(shù)中進(jìn)行更新。這似乎正在y_prec按預(yù)期更新。然后我定義我的模型架構(gòu):最后,我編譯并運行:model2.compile(optimizer='adam', loss=MAEpw_wrapper(y_prec))model2.fit_generator(DataGenerator(32, digits.data), epochs=100)哪里digits.data是一個 numpy 觀察數(shù)組。然而,這最終定義了單獨的圖形:StopIteration: Tensor("Variable:0", shape=(32, 64), dtype=float32_ref) must be from the same graph as Tensor("Variable_4:0", shape=(32, 64), dtype=float32_ref).我已經(jīng)搜索了 SO 以解決我的問題,但我發(fā)現(xiàn)沒有任何效果。任何有關(guān)如何正確執(zhí)行此操作的幫助表示贊賞。
添加回答
舉報
0/150
提交
取消