-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBamModelTests.cs
179 lines (138 loc) · 6.35 KB
/
BamModelTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#region License
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
*/
#endregion
using System;
using FluentAssertions;
using MathNet.Numerics.LinearAlgebra;
using NeuralNetworks.Models;
using Xunit;
using Xunit.Abstractions;
namespace NeuralNetworks.Tests
{
public class BamModelTests
{
private readonly ITestOutputHelper _output;
private readonly Vector<float> _image1 = Vector<float>.Build.DenseOfArray(new[] {0f, 1, 1, 1, 0, 1});
private readonly Vector<float> _image2 = Vector<float>.Build.DenseOfArray(new[] {1f, 0, 0, 1, 0, 0});
private readonly Vector<float> _name1 = Vector<float>.Build.DenseOfArray(new[] {1f, 1, 0, 0});
private readonly Vector<float> _name2 = Vector<float>.Build.DenseOfArray(new[] {1f, 0, 1, 0});
public BamModelTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void FirstTrainingShouldSetMatrixDimensions()
{
var bamModel = new BamModel();
bamModel.Teach(_image1, _name1);
_output.WriteLine(bamModel.WeightsMatrix.ToString());
bamModel.WeightsMatrix.RowCount.ShouldBeEquivalentTo(6);
bamModel.WeightsMatrix.ColumnCount.ShouldBeEquivalentTo(4);
}
[Fact]
public void SecondTrainingShouldGiveRightWeightMatrix()
{
var bamModel = new BamModel();
var correctMatrix = Matrix<float>.Build.DenseOfArray(new[,]
{
{0f, -2, 2, 0},
{0, 2, -2, 0},
{0, 2, -2, 0},
{2, 0, 0, -2},
{-2, 0, 0, 2},
{0, 2, -2, 0}
});
bamModel.Teach(_image1, _name1);
bamModel.Teach(_image2, _name2);
_output.WriteLine(bamModel.WeightsMatrix.ToString());
bamModel.WeightsMatrix.ShouldBeEquivalentTo(correctMatrix);
}
[Fact]
public void TrainingByImageWithBadLengthShouldThrowArgumentException()
{
var bamModel = new BamModel();
var imageWithBadLength = Vector<float>.Build.DenseOfArray(new float[3]);
bamModel.Teach(_image1, _name1);
Action validatingImageLength = () => bamModel.Teach(imageWithBadLength, _name2);
validatingImageLength.ShouldThrow<ArgumentException>();
}
[Fact]
public void TrainingByNameWithBadLengthShouldThrowArgumentException()
{
var bamModel = new BamModel();
var nameWithBadLength = Vector<float>.Build.DenseOfArray(new float[5]);
bamModel.Teach(_image1, _name1);
Action validatingImageLength = () => bamModel.Teach(_image2, nameWithBadLength);
validatingImageLength.ShouldThrow<ArgumentException>();
}
[Fact]
public void RecoveringNameWithOneGivenImageVectorShouldReturnRightName()
{
var bamModel = new BamModel();
var imagesMatrix = Matrix<float>.Build.DenseOfRowVectors(_image1);
var namesMatrix = Matrix<float>.Build.DenseOfRowVectors(_name1);
bamModel.Teach(_image1, _name1);
bamModel.Teach(_image2, _name2);
Matrix<float> resultNamesMatrix = bamModel.RecoverName(imagesMatrix);
_output.WriteLine(resultNamesMatrix.ToString());
resultNamesMatrix.ShouldBeEquivalentTo(namesMatrix);
}
[Fact]
public void RecoveringNameWithTwoGivenImageVectorsShouldReturnRightNames()
{
var bamModel = new BamModel();
var imagesMatrix = Matrix<float>.Build.DenseOfRowVectors(_image1, _image2);
var namesMatrix = Matrix<float>.Build.DenseOfRowVectors(_name1, _name2);
bamModel.Teach(_image1, _name1);
bamModel.Teach(_image2, _name2);
Matrix<float> resultNamesMatrix = bamModel.RecoverName(imagesMatrix);
_output.WriteLine(resultNamesMatrix.ToString());
resultNamesMatrix.ShouldBeEquivalentTo(namesMatrix);
}
[Fact]
public void RecoveringNameFromImageVectorWithWrongLengthShouldThrowArgumentException()
{
var bamModel = new BamModel();
var imagesMatrixWithWrongColumnLength = Matrix<float>.Build.DenseOfArray(new float[1, 10]);
bamModel.Teach(_image1, _name1);
Action recoveringNameFromWrongImage = () => bamModel.RecoverName(imagesMatrixWithWrongColumnLength);
recoveringNameFromWrongImage.ShouldThrow<ArgumentException>();
}
[Fact]
public void RecoveringImageWithOneGivenNameVectorShouldReturnRightImage()
{
var bamModel = new BamModel();
var namesMatrix = Matrix<float>.Build.DenseOfRowVectors(_name1);
var imagesMatrix = Matrix<float>.Build.DenseOfRowVectors(_image1);
bamModel.Teach(_image1, _name1);
bamModel.Teach(_image2, _name2);
Matrix<float> resultNamesMatrix = bamModel.RecoverImage(namesMatrix);
_output.WriteLine(resultNamesMatrix.ToString());
resultNamesMatrix.ShouldBeEquivalentTo(imagesMatrix);
}
[Fact]
public void RecoveringImageWithTwoGivenNameVectorsShouldReturnRightImages()
{
var bamModel = new BamModel();
var namesMatrix = Matrix<float>.Build.DenseOfRowVectors(_name1, _name2);
var imagesMatrix = Matrix<float>.Build.DenseOfRowVectors(_image1, _image2);
bamModel.Teach(_image1, _name1);
bamModel.Teach(_image2, _name2);
Matrix<float> resultNamesMatrix = bamModel.RecoverImage(namesMatrix);
_output.WriteLine(resultNamesMatrix.ToString());
resultNamesMatrix.ShouldBeEquivalentTo(imagesMatrix);
}
[Fact]
public void RecoveringImageFromNameVectorWithWrongLengthShouldThrowArgumentException()
{
var bamModel = new BamModel();
var namesMatrixWithWrongColumnLength = Matrix<float>.Build.DenseOfArray(new float[1, 10]);
bamModel.Teach(_image1, _name1);
Action recoveringImageFromWrongName = () => bamModel.RecoverImage(namesMatrixWithWrongColumnLength);
recoveringImageFromWrongName.ShouldThrow<ArgumentException>();
}
}
}