您需要添加屏蔽操作邏輯作爲TensorFlow graph的一部分。具體而言,control flow operators類似tf.logical_and,tf.logical_or和tf.case允許您在運行模型時有條件地選擇所需的數據。
例如,下面的代碼顯示瞭如何基於兩個訓練數據或標籤輸入的狀態建立一個4元張量。你應該能夠對你的情況應用類似的邏輯。
def const_v(val):
return tf.constant(val, tf.float32)
def const_1():
return const_v(1)
def const_0():
return const_v(0)
def cond_and(cond_1, cond_2, val_1, val_2):
return tf.logical_and(tf.equal(cond_1, val_1),
tf.equal(cond_2, val_2))
def vec4(c1, c2, c3, c4):
return [const_v(c1), const_v(c2), const_v(c3), const_v(c4)]
# c1 c2 vector
# 1 1 [1, 0 , 0 ,0]
# 1 0 [0, 1, 0, 0]
# 0 1 [0, 0, 1, 0]
# 0 0 [0, 0, 0, 1]
def combined_conditions(cond_1, cond_2):
return tf.stack(tf.case({cond_and(cond_1, cond_2, const_1(), const_1()): lambda: vec4(1, 0, 0, 0),
cond_and(cond_1, cond_2, const_1(), const_0()): lambda: vec4(0, 1, 0, 0),
cond_and(cond_1, cond_2, const_0(), const_1()): lambda: vec4(0, 0, 1, 0),
cond_and(cond_1, cond_2, const_0(), const_0()): lambda: vec4(0, 0, 0, 1)
}, default=lambda: vec4(0, 0, 0, 0), exclusive=True))
林不知道這會做伎倆。讓我舉一個具體的例子。假設我的特徵是長度爲100,000的矢量。我用10萬步訓練。在每一步中,我想用掩碼向量中的1和0掩碼輸入要素,其中1的個數只是訓練步數。圖表不知何故必須知道這個數字,並在每一步創建一個新的面具。請注意,它所需的數字不能從輸入數據集本身收集。 –
我從來沒有使用過它們,但看起來您可以從[Training Utility](https://www.tensorflow.org/versions/master/api_guides/python/train#Training_Utilities)函數中獲取培訓步驟 - - tf.train.get_global_step或tf.train.global_step。這加上圖形邏輯和一些聰明的Python可能會解決你的問題。 –