// Overloading the constructor to load from an ONNX proto public Linear(SyftController _controller, GraphProto graph) { init(this.name); this.controller = _controller; _weights = ONNXTools.BuildFloatTensor(graph.Initializer[0], this.controller, autograd: true, keepgrads: true); AttributeProto transB = ONNXTools.FindAttribute(graph.Node[0], "transB"); if (transB != null && transB.I == 1) { _weights = _weights.Transpose(); } parameters.Add(_weights.Id); _input = _weights.Shape[0]; _output = _weights.Shape[1]; _bias = ONNXTools.BuildFloatTensor(graph.Initializer[1], this.controller, autograd: true, keepgrads: true); _biased = true; parameters.Add(_bias.Id); #pragma warning disable 420 id = System.Threading.Interlocked.Increment(ref nCreated); controller.addModel(this); }
private static AttributeProto MakeAttribute(string key, long value) { AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Int; attribute.I = value; return attribute; }
private static AttributeProto MakeAttribute(string key, double value) { AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Float; attribute.F = (float)value; return attribute; }
private static AttributeProto MakeAttribute(string key, GraphProto value) { AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Graph; attribute.G = value; return attribute; }
private static AttributeProto MakeAttribute(string key, ByteString value) { AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.String; attribute.S = value; return attribute; }
public static string AsString(this AttributeProto item) { string val = string.Empty; if (item.HasF) { val = item.F.ToString(); } if (item.HasI) { val = item.I.ToString(); } if (item.HasS) { val = item.S.ToStringUtf8(); } if (item.Strings != null && item.Strings.Any()) { val = string.Join("; ", item.Strings.Select(z => z.ToStringUtf8())); } if (item.Floats != null && item.Floats.Any()) { val = string.Join("; ", item.Floats); } if (item.Ints != null && item.Ints.Any()) { val = string.Join("; ", item.Ints); } return(val); }
private static AttributeProto MakeAttribute(string key, TensorProto.Types.DataType value) { AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Int; attribute.I = (int)value; return(attribute); }
private static AttributeProto MakeAttribute(string key) { Contracts.CheckNonEmpty(key, nameof(key)); var attribute = new AttributeProto(); attribute.Name = key; return attribute; }
private static AttributeProto MakeAttribute(string key, IEnumerable<float> value) { Contracts.CheckValue(value, nameof(value)); AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Floats; attribute.Floats.Add(value.Select(x => x)); return attribute; }
private static AttributeProto MakeAttribute(string key, IEnumerable<GraphProto> value) { Contracts.CheckValue(value, nameof(value)); AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Graphs; attribute.Graphs.Add(value); return attribute; }
private static AttributeProto MakeAttribute(string key, IEnumerable <ByteString> value) { Contracts.CheckValue(value, nameof(value)); AttributeProto attribute = MakeAttribute(key); attribute.Type = AttributeProto.Types.AttributeType.Strings; attribute.Strings.Add(value); return(attribute); }
internal AttributeProto FindAttribute(string name, AttributeProto.Types.AttributeType type = AttributeProto.Types.AttributeType.Undefined) { AttributeProto attr = null; if (TryFindAttribute(name, type, out attr)) { return(attr); } throw new OnnxLayerImportException($"Couldn't find attribute {name} of type {type}"); }
void parseAndAssignValue(AttributeProto attr, string val) { switch (attr.Type) { case AttributeProto.Types.AttributeType.Float: attr.F = float.Parse(val.Replace(",", "."), CultureInfo.InvariantCulture); break; case AttributeProto.Types.AttributeType.Int: attr.I = int.Parse(val); break; case AttributeProto.Types.AttributeType.String: attr.S = ByteString.CopyFromUtf8(val); break; case AttributeProto.Types.AttributeType.Floats: { attr.Floats.Clear(); var aa = val.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(z => float.Parse(z.Replace(",", "."), CultureInfo.InvariantCulture)).ToArray(); foreach (var item in aa) { attr.Floats.Add(item); } } break; case AttributeProto.Types.AttributeType.Ints: { attr.Ints.Clear(); var aa = val.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(z => int.Parse(z)).ToArray(); foreach (var item in aa) { attr.Ints.Add(item); } } break; case AttributeProto.Types.AttributeType.Strings: { attr.Strings.Clear(); var aa = val.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(z => ByteString.CopyFromUtf8(z)).ToArray(); foreach (var item in aa) { attr.Strings.Add(item); } } break; default: throw new NotImplementedException(); } }
private void button2_Click(object sender, EventArgs e) { var aa = new AttributeProto() { Name = textBox1.Text }; aa.Type = (AttributeProto.Types.AttributeType)(comboBox1.SelectedItem as ComboBoxItem).Tag; parseAndAssignValue(aa, textBox2.Text); node.Attribute.Add(aa); updateList(); }
internal bool TryFindAttribute(string name, AttributeProto.Types.AttributeType type, out AttributeProto attr) { const AttributeProto.Types.AttributeType undefined = AttributeProto.Types.AttributeType.Undefined; var attributes = m_ONNXNode.Attribute; for (var i = 0; i < attributes.Count; ++i) { attr = attributes[i]; if (attr.Name == name && (attr.Type == type || attr.Type == undefined || type == undefined)) { return(true); } } attr = null; return(false); }
// Attribute helpers internal bool TryFindAttribute(string name, out AttributeProto attr) { return(TryFindAttribute(name, AttributeProto.Types.AttributeType.Undefined, out attr)); }