在Keras中修改图层权重 [英] Modify layer weights in Keras

查看:75
本文介绍了在Keras中修改图层权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Keras中修改图层的输出。我有一个编码器,可以将时间序列转换为潜在空间,然后,对于每个压缩的时间序列,我想在时间序列中添加一些数字。



例如我有:

  input_d = Input((100 ,))
h1_d =重塑((100,1))(input_d)
h2_d = LSTM(150,return_sequences = True)(h1_d)
h3_d = TimeDistributed(Dense(64,激活='linear'))(h2_d)
h4_d = LSTM(150)(h3_d)
output_d = Dense(30,激活='linear')(h4_d)

我想做这样的事情:

  new_weights = [] 
for i in output_d.weights:
new_weights.append(np.vstack(([[1,2,3],i)))

但是问题是我不知道在哪一刻可以这样做,因为如果在<$ c之后写一个Lambda层$ c> ouput_d 我无法访问砝码。

解决方案

我发现执行此操作的唯一方法是将所需功能实现为回调,您可以在其中进行访问通过 self.model.trainable_weights self.model.get_weights()来确定权重。 / p>

I'm trying to modify the output of a layer in Keras. I have an encoder which transforms a time series into latent space and after that, for each time series compressed I want to add some numbers to the time series.

For example I have:

input_d = Input((100,))
h1_d = Reshape((100, 1))(input_d)
h2_d = LSTM(150, return_sequences=True)(h1_d)
h3_d = TimeDistributed(Dense(64, activation='linear'))(h2_d)
h4_d = LSTM(150)(h3_d)
output_d = Dense(30, activation='linear')(h4_d)

And I want to do something like this:

new_weights = []
for i in outputs_d.weights:
    new_weights.append(np.vstack(([1,2,3], i)))

But the problem is that I don't know in which moment I can do this because if a write a Lambda layer after ouput_d I can't access the weights.

解决方案

The only way I have found to do something like this is by implementing the desired functionality as a Callback, where you have access to the model and thus to the weights through self.model.trainable_weights or self.model.get_weights().

这篇关于在Keras中修改图层权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆