1

我有一个定制的正则化器,需要分析模型输出张量。基本上我不能这样放。

model = Sequential()
model.add(Dense(128, name="dense_1", W_regularizer=Custom(0.1)))
model.add(Dense(nb_classes, name='dense_2'))
model.compile(loss='mse', optimizer='adam')
model.fit(..)

自定义函数需要目标标签张量的地方,不幸的是,这种张量尚未实现。

我也尝试设置此类图层的属性:

model.add(Dense(128, name="dense_1"))
model.get_layer('dense_1').W_regularizer = Custom(0.1)

get_config()我看到这样的层时,它已正确应用,但在训练期间似乎不起作用。或者也许这种方式不是一个明智的实施方式。

4

2 回答 2

0

要更改此类内容,您需要重新编译模型。这是因为编译过程会生成随后要最小化的训练函数。要更改训练功能,您需要再次编译。

打电话

model.compile(...)

在您进行更改之后,它应该按照您期望的方式工作。

于 2017-03-06T10:04:34.313 回答
0

谢谢@Thomas,我尝试重新编译但没有按预期工作(如下图所示)

我尝试了以下方法..
1)直接指定(工作)

#put inf number here for testing
x = Dense(128,name="dense_1", W_regularizer = l2(1E300))
model.add(x)
...
model.compile(...)

2)之后设置属性并编译(不工作)

x = Dense(128,name="dense_1")
model.add(x)
x.W_regularizer = l2(1E300)
...
model.compile(...)

3)add_weight在topology.layer中使用(有效)

x = Dense(128,name="dense_1")
model.add(x)
x.W = x.add_weight((x.input_dim, x.output_dim),
               initializer='zero',
               name='{}_W'.format(x.name),
               constraint=x.W_constraint,
               regularizer=l2(1E300))
model.compile(...)

对于 1) 3) 我得到loss: nan这意味着修改已成功应用,并且 3) 是我想要的。而第二个没有。但必须有一个聪明的方法来实现。

于 2017-03-06T10:57:14.930 回答