我正在尝试冻结一些选定重量的东西。举个例子
from keras.models import Sequential
from keras.layers import Dense,Input
model = Sequential()
model.add(Dense(4, input_shape=(4,),activation='relu'))
model.add(Dense(3,name="hidden",activation='relu'))
model.add(Dense(2,activation='sigmoid'))
model.compile(loss='mse', optimizer='adam')
print(model.layers[1].get_weights()[0])
这会将输入打印到隐藏层权重。
# Weights input x hidden
# Freeze 2Rx3C and 4Rx2C
# 2Rx3C=0.14362943; 4Rx2C=-0.23868048
array([[-0.05557871, 0.10941017, -0.59108734],
[ 0.37056673, 0.2968588 , 0.14362943],
[-0.05471832, -0.21425706, 0.6455065 ],
[-0.7883829 , -0.23868048, -0.517396 ]], dtype=float32)
我想从这个权重矩阵中冻结(2nd Row, 3rd Column)
和(4th Row, 2nd Column)
中的值,分别是 0.14362943 和 -0.23868048 。我不想在backprop上更新这些值。如何冻结这些选定的重量?
答案 0 :(得分:1)
您需要使用tf.identity,它用于在设备之间显式传输张量时使用。
matrixVariable = tf.Variable(<your matrix>)
matrixVariableSliced = matrixVariable[<sliced matrix>] #take out your required weights
matrixVariable_stop = tf.stop_gradient(tf.identity(matrixVariableSliced))
matrixVariable = tf.concat((matrixVariableSliced, matrixVariable_stop), axis=1)