How to create nnet.layers usable for custom training loops as well as trainNetwork?

1 次查看(过去 30 天)
Hei all,
I would like to create a layer that can be pretrained in a custom training loop and afterwards finetuned with the trainNetwork function. I created a class with the needed properties and a predict function:
function Z = predict(layer, X)
Z = sigmoid(fullyconnect(X, layer.weights, layer.biases));
end
Using this function works fine during the pretraining. Using trainNetwork afterwards leads to the error:
Error using validateDataFormat (line 14)
Specify 'DataFormat' when first argument is an unlabeled dlarray or numeric array.
This is caused by fullyconnect as it needs dlarrays or the correct format string. I try to make it as universal as possible, so i don't want to set the Format property on the fullyconnect command. Is there a way to force trainNetwork to use dlarrays?
Thanks in advance,
Niklas

回答(1 个)

Sai Bhargav Avula
Sai Bhargav Avula 2019-10-24
Hi,
As you mentioned the fullyconnect applies fully connect operation to dlarray data. For your custom layer to be made compactable to trainNetwork you should convert that using the fullyConnetedLayer by following template.
Or
You should finetune it by converting it into a dlnetwork.

类别

Help CenterFile Exchange 中查找有关 Custom Layers 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by