Skip to content

Commit 0723408

Browse files
authored
Merge pull request #32 from roberino/networkspec
Networkspec
2 parents cab1054 + 3758d4e commit 0723408

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1108
-227
lines changed

build.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/sh
22
dotnet restore "LinqInfer.sln"
3-
dotnet build "LinqInfer.sln"
3+
dotnet build "LinqInfer.sln"
4+
dotnet pack "src\LinqInfer\LinqInfer.csproj" --output ..\..\artifacts
5+
dotnet pack "src\LinqInfer.Microservices\LinqInfer.Microservices.csproj" --output ..\..\artifacts

src/LinqInfer/Data/BinaryVectorDocument.cs

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace LinqInfer.Data
1414
/// General purpose document for serialising vector and general object data.
1515
/// The document supports serialising as XML and to a binary stream
1616
/// </summary>
17-
public class BinaryVectorDocument : IBinaryPersistable, IXmlExportable, IXmlImportable
17+
public class BinaryVectorDocument : IBinaryPersistable, IXmlExportable, IXmlImportable, IEquatable<BinaryVectorDocument>
1818
{
1919
private const string PropertiesName = "PROP";
2020
private const string BlobName = "BLOB";
@@ -102,8 +102,22 @@ public long Checksum
102102
{
103103
var type = instance?.GetType() ?? typeof(T);
104104

105-
Properties["AssemblyQualifiedName"] = type.AssemblyQualifiedName;
106-
Properties["TypeName"] = type.Name;
105+
SetType(type);
106+
}
107+
108+
internal void SetType(Type type)
109+
{
110+
Properties[nameof(AssemblyQualifiedName)] = type.AssemblyQualifiedName;
111+
Properties[nameof(TypeName)] = type.Name;
112+
}
113+
114+
internal string AssemblyQualifiedName => PropertyOrDefault(nameof(AssemblyQualifiedName), string.Empty);
115+
116+
internal string TypeName => PropertyOrDefault(nameof(TypeName), string.Empty);
117+
118+
public BinaryVectorDocument FindChild<T>()
119+
{
120+
return QueryChildren(new { AssemblyQualifiedName = typeof(T).AssemblyQualifiedName }).FirstOrDefault();
107121
}
108122

109123
public IDictionary<string, string> Properties
@@ -122,6 +136,29 @@ public IDictionary<string, byte[]> Blobs
122136
}
123137
}
124138

139+
public IEnumerable<BinaryVectorDocument> QueryChildren(object propertyQuery)
140+
{
141+
var query = propertyQuery.ToDictionary();
142+
143+
return Children.Where(c =>
144+
{
145+
bool found = true;
146+
147+
foreach (var q in query)
148+
{
149+
if (c.Properties.TryGetValue(q.Key, out string v) && v == q.Value?.ToString())
150+
{
151+
continue;
152+
}
153+
154+
found = false;
155+
break;
156+
}
157+
158+
return found;
159+
});
160+
}
161+
125162
public bool HasProperty(string name)
126163
{
127164
return _properties.ContainsKey(name);
@@ -212,12 +249,20 @@ internal T PropertyOrDefault<T>(Expression<Func<object>> keyExpression, T defaul
212249
return PropertyOrDefault(propName, defaultValue);
213250
}
214251

215-
internal void SetPropertyFromExpression(Expression<Func<object>> expression)
252+
internal void SetPropertyFromExpression(Expression<Func<object>> expression, object value = null)
216253
{
217254
var propName = LinqExtensions.GetPropertyName(expression);
218-
var value = expression.Compile().Invoke();
219255

220-
if (value != null)
256+
if (value == null)
257+
{
258+
value = expression.Compile().Invoke();
259+
260+
if (value != null)
261+
{
262+
Properties[propName] = value.ToString();
263+
}
264+
}
265+
else
221266
{
222267
Properties[propName] = value.ToString();
223268
}
@@ -227,7 +272,7 @@ internal BinaryVectorDocument GetChildDoc<T>(Type type = null, int? index = null
227272
{
228273
var tname = (type ?? typeof(T)).GetTypeInf().Name;
229274

230-
var childNode = index.HasValue ? Children[index.Value] : Children.SingleOrDefault(c => c.HasProperty("TypeName") && c.Properties["TypeName"] == tname);
275+
var childNode = index.HasValue ? Children[index.Value] : Children.SingleOrDefault(c => c.TypeName == tname);
231276

232277
if (childNode == null && !ignoreIfMissing) throw new FormatException("Child object not found : " + tname);
233278

@@ -259,22 +304,23 @@ internal T ReadChildObject<T>(T obj, int? index = null, bool ignoreIfMissing = f
259304
throw new NotSupportedException();
260305
}
261306

262-
internal void WriteChildObject(object obj)
307+
internal void WriteChildObject(object obj, object attributes = null)
263308
{
264309
if (obj == null) return;
265310

266-
var childType = obj.GetType().GetTypeInf();
267-
var tc = Type.GetTypeCode(obj.GetType());
311+
var childType = obj.GetType();
312+
var tc = Type.GetTypeCode(childType);
268313

269314
if (tc == TypeCode.Object)
270315
{
271-
if (obj is IExportableAsVectorDocument && obj is IImportableAsVectorDocument)
316+
if (obj is IExportableAsVectorDocument)
272317
{
273318
var childDoc = ((IExportableAsVectorDocument)obj).ToVectorDocument();
274319

275-
childDoc.Properties["TypeName"] = childType.Name;
276-
childDoc.Properties["QualifiedTypeName"] = childType.AssemblyQualifiedName;
320+
SetProperties(childDoc, attributes);
277321

322+
childDoc.SetType(childType);
323+
278324
Children.Add(childDoc);
279325
}
280326
else
@@ -283,8 +329,8 @@ internal void WriteChildObject(object obj)
283329
{
284330
var childDoc = new BinaryVectorDocument();
285331

286-
childDoc.Properties["TypeName"] = childType.Name;
287-
childDoc.Properties["QualifiedTypeName"] = childType.AssemblyQualifiedName;
332+
SetProperties(childDoc, attributes);
333+
childDoc.SetType(childType);
288334
childDoc.Properties["Data"] = ((IBinaryPersistable)obj).ToClob();
289335

290336
Children.Add(childDoc);
@@ -354,6 +400,19 @@ protected void Read(BinaryReader reader, int level)
354400
}
355401
}
356402

403+
private void SetProperties(BinaryVectorDocument doc, object obj)
404+
{
405+
if (obj != null)
406+
{
407+
var data = obj.ToDictionary();
408+
409+
foreach (var item in data)
410+
{
411+
doc.Properties[item.Key] = item.Value.ToString();
412+
}
413+
}
414+
}
415+
357416
private XDocument ExportAsXml(bool isRoot, bool base64v)
358417
{
359418
var date = DateTime.UtcNow;
@@ -518,5 +577,30 @@ protected void Write(BinaryWriter writer, int level)
518577
child.Write(writer, level + 1);
519578
}
520579
}
580+
581+
public bool Equals(BinaryVectorDocument other)
582+
{
583+
if (other == null) return false;
584+
if (ReferenceEquals(this, other)) return true;
585+
if (Checksum != other.Checksum) return false;
586+
587+
var xml1 = ExportAsXml();
588+
var xml2 = other.ExportAsXml();
589+
590+
xml1.Root.Attribute("exported").Remove();
591+
xml2.Root.Attribute("exported").Remove();
592+
593+
return string.Equals(xml1.ToString(), xml2.ToString());
594+
}
595+
596+
public override bool Equals(object obj)
597+
{
598+
return Equals(obj as BinaryVectorDocument);
599+
}
600+
601+
public override int GetHashCode()
602+
{
603+
return ExportAsXml().ToString().GetHashCode();
604+
}
521605
}
522606
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace LinqInfer.Learning.Classification
2+
{
3+
public interface IClassifierTrainer : IAssistedLearningProcessor
4+
{
5+
/// <summary>
6+
/// Removes inputs from the classifier.
7+
/// Subsequent training and classification should also
8+
/// comply with the new input size.
9+
/// </summary>
10+
void PruneInputs(params int[] inputIndexes);
11+
12+
/// <summary>
13+
/// Resets the error back to null
14+
/// </summary>
15+
void ResetError();
16+
17+
/// <summary>
18+
/// Gets the classifier
19+
/// </summary>
20+
IVectorClassifier Output { get; }
21+
}
22+
}

src/LinqInfer/Learning/Classification/IClassifierTrainingContext.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,36 @@ namespace LinqInfer.Learning.Classification
77
/// for training a classifier.
88
/// </summary>
99
/// <typeparam name="TParameters">The parameters used to create the classifier</typeparam>
10-
public interface IClassifierTrainingContext<TParameters> : IRawClassifierTrainingContext<TParameters>, ICloneableObject<IClassifierTrainingContext<TParameters>>
10+
public interface IClassifierTrainingContext<TParameters> : IClassifierTrainer, ICloneableObject<IClassifierTrainingContext<TParameters>>
1111
{
12+
/// <summary>
13+
/// Gets a localised id for the training context
14+
/// </summary>
15+
int Id { get; }
16+
17+
/// <summary>
18+
/// A counter that can be incremented to track iterations for this context
19+
/// </summary>
20+
int IterationCounter { get; set; }
21+
22+
/// <summary>
23+
/// Returns the parameters used by this training instance
24+
/// </summary>
25+
TParameters Parameters { get; }
26+
27+
/// <summary>
28+
/// Returns the current rate of error change.
29+
/// </summary>
30+
double? RateOfErrorChange { get; }
31+
32+
/// <summary>
33+
/// Returns the error accumulated from training
34+
/// </summary>
35+
double? CumulativeError { get; }
36+
37+
/// <summary>
38+
/// Returns the average error from training
39+
/// </summary>
40+
double? AverageError { get; }
1241
}
1342
}

src/LinqInfer/Learning/Classification/IRawClassifierTrainingContext.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)