如何使用 tf 作为 theano 从 keras 层编辑权重

How to edit weights from keras layer with tf as theano

我有以下代码,在创建图层时使用了 Keras 和 Theano:

net.add(Dense(outdim))
Wlast = net.layers[-1].W
Wlast.set_value(Wlast.get_value(borrow=True) * 0.1)

有没有适合TF的改造?我试试这个:

net.add(Dense(outdim))
Wlast = net.layers[-1].W
K.set_value(Wlast, K.get_value(Wlast) * 0.1)

# before it I do some import and set session: 
# from Keras import backend as K
# K.set_session(session)

但我不确定这是否以适当的方式工作...

因为我使用这一层作为概率输出: - 在 Theano 版本中 [-1,-1] 范围内的概率向量 - 从另一方面来说,如果我使用这个 Keras 代码,概率偏差大于 1(如果我手动将这些输出权重降低 0.1 - 概率分布变得接近 Theano)

正确的解决方案是使用 Keras 的 lambdas,例如:

net.add(Dense(outdim))
net.add(Lambda(lambda x: x * 0.1))