/// <summary> /// The ResizeModel method gives the custom trainer the opportunity to resize the model if needed. /// </summary> /// <param name="strModel">Specifies the model descriptor.</param> /// <param name="rgVocabulary">Specifies the vocabulary.</param> /// <param name="log">Specifies the output log.</param> /// <returns>A new model discriptor is returned (or the same 'strModel' if no changes were made).</returns> /// <remarks>Note, this method is called after PreloadData.</remarks> string IXMyCaffeCustomTrainerRNN.ResizeModel(Log log, string strModel, BucketCollection rgVocabulary) { if (rgVocabulary == null || rgVocabulary.Count == 0) { return(strModel); } int nVocabCount = rgVocabulary.Count; NetParameter p = NetParameter.FromProto(RawProto.Parse(strModel)); string strEmbedName = ""; EmbedParameter embed = null; string strIpName = ""; InnerProductParameter ip = null; foreach (LayerParameter layer in p.layer) { if (layer.type == LayerParameter.LayerType.EMBED) { strEmbedName = layer.name; embed = layer.embed_param; } else if (layer.type == LayerParameter.LayerType.INNERPRODUCT) { strIpName = layer.name; ip = layer.inner_product_param; } } if (embed != null) { if (embed.input_dim != (uint)nVocabCount) { log.WriteLine("WARNING: Embed layer '" + strEmbedName + "' input dim changed from " + embed.input_dim.ToString() + " to " + nVocabCount.ToString() + " to accomodate for the vocabulary count."); embed.input_dim = (uint)nVocabCount; } } if (ip.num_output != (uint)nVocabCount) { log.WriteLine("WARNING: InnerProduct layer '" + strIpName + "' num_output changed from " + ip.num_output.ToString() + " to " + nVocabCount.ToString() + " to accomodate for the vocabulary count."); ip.num_output = (uint)nVocabCount; } m_rgVocabulary = rgVocabulary; RawProto proto = p.ToProto("root"); return(proto.ToString()); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Sets up the inner product. </summary> /// /// <exception cref="Exception"> Thrown when an exception error condition occurs. </exception> /// /// <param name="param"> The parameter. </param> /// <param name="blobs"> The blobs. </param> /// <param name="name"> The name. </param> /// <param name="inputNames"> List of names of the inputs. </param> /// <param name="outputNames"> List of names of the outputs. </param> /// /// <returns> A Linear. </returns> //////////////////////////////////////////////////////////////////////////////////////////////////// static Linear SetupInnerProduct(InnerProductParameter param, List <BlobProto> blobs, string name, string[] inputNames, string[] outputNames) { if (param.Axis != 1) { throw new Exception("Non-default axis in InnerProduct is not supported"); } int width = GetWidth(blobs[0]); int height = GetHeight(blobs[0]); float[] w = blobs[0].Datas; if (param.BiasTerm) { return(new Linear(width, height, !param.BiasTerm, w, blobs[1].Datas, name: name, inputNames: inputNames, outputNames: outputNames)); } return(new Linear(width, height, !param.BiasTerm, w, name: name)); }
/// <summary> /// The ResizeModel method gives the custom trainer the opportunity to resize the model if needed. /// </summary> /// <param name="strModel">Specifies the model descriptor.</param> /// <param name="rgVocabulary">Specifies the vocabulary.</param> /// <returns>A new model discriptor is returned (or the same 'strModel' if no changes were made).</returns> /// <remarks>Note, this method is called after PreloadData.</remarks> public string ResizeModel(string strModel, BucketCollection rgVocabulary) { if (rgVocabulary == null || rgVocabulary.Count == 0) { return(strModel); } int nVocabCount = rgVocabulary.Count; NetParameter p = NetParameter.FromProto(RawProto.Parse(strModel)); EmbedParameter embed = null; InnerProductParameter ip = null; foreach (LayerParameter layer in p.layer) { if (layer.type == LayerParameter.LayerType.EMBED) { embed = layer.embed_param; } else if (layer.type == LayerParameter.LayerType.INNERPRODUCT) { ip = layer.inner_product_param; } } if (embed != null) { embed.input_dim = (uint)nVocabCount; } ip.num_output = (uint)nVocabCount; m_rgVocabulary = rgVocabulary; RawProto proto = p.ToProto("root"); return(proto.ToString()); }