forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorOperate.cs
More file actions
184 lines (163 loc) · 5.53 KB
/
Copy pathTensorOperate.cs
File metadata and controls
184 lines (163 loc) · 5.53 KB
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
180
181
182
183
184
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tensorflow.NumPy;
using System.Linq;
using static Tensorflow.Binding;
namespace TensorFlowNET.UnitTest.ManagedAPI
{
[TestClass]
public class TensorOperate
{
[TestMethod]
public void TransposeTest()
{
// https://www.tensorflow.org/api_docs/python/tf/transpose#for_example_2
var x = tf.constant(new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 }
});
var transpose_x = tf.transpose(x);
Assert.AreEqual(new[] { 1, 4 }, transpose_x[0].numpy());
Assert.AreEqual(new[] { 2, 5 }, transpose_x[1].numpy());
Assert.AreEqual(new[] { 3, 6 }, transpose_x[2].numpy());
#region constant a
var a = tf.constant(np.array(new[, , ,]
{
{
{
{ 1, 11, 2, 22 }
},
{
{ 3, 33, 4, 44 }
}
},
{
{
{ 5, 55, 6, 66 }
},
{
{ 7, 77, 8, 88 }
}
}
}));
#endregion
var actual_transposed_a = tf.transpose(a, new[] { 3, 1, 2, 0 });
#region constant transpose_a
var expected_transposed_a = tf.constant(np.array(new[, , ,]
{
{
{ { 1, 5 } }, { { 3, 7 } }
},
{
{ { 11, 55 } }, { { 33, 77 } }
},
{
{
{ 2, 6 }
},
{
{ 4, 8 }
}
},
{
{
{ 22, 66 }
},
{
{ 44, 88 }
}
}
}));
#endregion
Assert.AreEqual((4, 2, 1, 2), actual_transposed_a.shape);
Assert.AreEqual(expected_transposed_a.numpy(), actual_transposed_a.numpy());
}
[TestMethod]
public void InitTensorTest()
{
var a = tf.constant(np.array(new[, ,]
{
{ { 1 }, { 2 }, { 3 } },
{ { 4 }, { 5 }, { 6 } }
}));
Assert.IsTrue(Enumerable.SequenceEqual(new long[] { 2, 3, 1 }, a.shape.dims));
var b = tf.constant(new[, ,]
{
{ { 1 }, { 2 }, { 3 } },
{ { 4 }, { 5 }, { 6 } }
});
Assert.IsTrue(Enumerable.SequenceEqual(new long[] { 2, 3, 1 }, b.shape.dims));
}
[TestMethod]
public void ConcatTest()
{
var a = tf.constant(new[,] { { 1, 2 }, { 3, 4 } });
var b = tf.constant(new[,] { { 5, 6 }, { 7, 8 } });
var c = tf.constant(new[,] { { 9, 10 }, { 11, 12 } });
var concatValue = tf.concat(new[] { a, b, c }, axis: 0);
Assert.IsTrue(Enumerable.SequenceEqual(new long[] { 6, 2 }, concatValue.shape.dims));
}
[TestMethod]
public void ConcatDoubleTest()
{
var a = tf.constant(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } });
var b = tf.constant(new[,] { { 5.0, 6.0 }, { 7.0, 8.0 } });
var c = tf.constant(new[,] { { 9.0, 10.0 }, { 11.0, 12.0 } });
var concatValue = tf.concat(new[] { a, b, c }, axis: 0);
Assert.IsTrue(Enumerable.SequenceEqual(new long[] { 6, 2 }, concatValue.shape.dims));
}
[TestMethod]
public void ConcatAndSplitTest()
{
var a = tf.constant(new[,] { { 1, 2 }, { 3, 4 } });
var b = tf.constant(new[,] { { 5, 6 }, { 7, 8 } });
var c = tf.constant(new[,] { { 9, 10 }, { 11, 12 } });
var value = tf.concat(new[] { a, b, c }, axis: 0);
var splitValue = tf.split(value, 3, axis: 0);
Assert.AreEqual(3, splitValue.Length);
Assert.IsTrue(Enumerable.SequenceEqual(new long[] { 2, 2 }, splitValue[0].shape.dims));
}
#region ones/zeros like
[TestMethod]
public void TestOnesLike()
{
#region 2-dimension
var ones2D = tf.ones_like(new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 }
});
Assert.AreEqual(new[] { 1, 1, 1 }, ones2D[0].numpy());
Assert.AreEqual(new[] { 1, 1, 1 }, ones2D[1].numpy());
#endregion
#region 1-dimension
var ones1D = tf.ones_like(new int[,]
{
{ 1, 2, 3 }
});
Assert.AreEqual(new[] { 1, 1, 1 }, ones1D[0].numpy());
#endregion
}
[TestMethod]
public void TestZerosLike()
{
#region 2-dimension
var zeros2D = tf.zeros_like(new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 }
});
Assert.AreEqual(new[] { 0, 0, 0 }, zeros2D[0].numpy());
Assert.AreEqual(new[] { 0, 0, 0 }, zeros2D[1].numpy());
#endregion
#region 1-dimension
var zeros1D = tf.zeros_like(new int[,]
{
{ 1, 2, 3 }
});
Assert.AreEqual(new[] { 0, 0, 0 }, zeros1D[0].numpy());
#endregion
}
#endregion
}
}