感谢这篇出色的文章(http://machinethink.net/blog/coreml-custom-layers/),我了解了如何使用coremltools和带有Keras自定义层的Lambda编写转换。但是,我对这种情况不了解,具有两个参数。
#python
def scaling(x, scale):
return x * scale
Keras层在这里。
#python
up = conv2d_bn(mixed,
K.int_shape(x)[channel_axis],
1,
activation=None,
use_bias=True,
name=name_fmt('Conv2d_1x1'))
x = Lambda(scaling, # HERE !!
output_shape=K.int_shape(up)[1:],
arguments={'scale': scale})(up)
x = add([x, up])
在这种情况下,我怎么能写func evaluate(inputs: [MLMultiArray], outputs: [MLMultiArray])
在custom MLCustomLayer class
上斯威夫特?我只了解一种参数函数情况,像这样,
#swift
func evaluate(inputs: [MLMultiArray], outputs: [MLMultiArray]) throws {
for i in 0..<inputs.count {
let input = inputs[i]
let output = outputs[i]
for j in 0..<input.count {
let x = input[j].floatValue
let y = x / (1 + exp(-x))
output[j] = NSNumber(value: y)
}
}
}
两个参数函数怎么样x * scale
?
完整的代码在这里。
谢谢。
看起来scale
是超参数,而不是可学习的参数,对吗?
在这种情况下,您需要添加scale
到自定义层的参数字典。然后在您的Swift类中,scale
也将在传递到init(parameters)
函数中的参数字典中。将其存储在一个属性中,然后evaluate(inputs, outputs)
再次从该属性中读取。
我的博客文章实际上显示了如何执行此操作。;-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句