亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Keras 中實現批量依賴損失

在 Keras 中實現批量依賴損失

手掌心 2021-08-05 10:12:59
我在 Keras 中設置了一個自動編碼器。我希望能夠根據預定的“精度”向量對輸入向量的特征進行加權。這個連續值向量與輸入的長度相同,每個元素都在范圍內[0, 1],對應于對應輸入元素的置信度,其中1為完全置信度,0為不置信度。對于每個示例,我都有一個精度向量。我已經定義了一個考慮到這個精度向量的損失。在這里,低置信度特征的重建被降低權重。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根據當前批次進行更新,以便每個精度向量與其觀察正確關聯。我做了以下工作:global y_precy_prec = K.variable(P[:32])這P是一個包含所有精度向量的 numpy 數組,其索引對應于示例。我初始化y_prec為批量大小為 32 的正確形狀。然后我定義以下內容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在這里,我的目標是y_prec在生成批處理的同一函數中進行更新。這似乎正在y_prec按預期更新。然后我定義我的模型架構:最后,我編譯并運行:model2.compile(optimizer='adam', loss=MAEpw_wrapper(y_prec))model2.fit_generator(DataGenerator(32, digits.data), epochs=100)哪里digits.data是一個 numpy 觀察數組。然而,這最終定義了單獨的圖形: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).我已經搜索了 SO 以解決我的問題,但我發現沒有任何效果。任何有關如何正確執行此操作的幫助表示贊賞。
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號