forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_ops.cs
More file actions
111 lines (99 loc) · 4.1 KB
/
check_ops.cs
File metadata and controls
111 lines (99 loc) · 4.1 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
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using static Tensorflow.Binding;
namespace Tensorflow
{
public class check_ops
{
/// <summary>
/// Assert the condition `x == y` holds element-wise.
/// </summary>
/// <param name="t1"></param>
/// <param name="t2"></param>
/// <param name="name"></param>
public static Operation assert_equal<T1, T2>(T1 t1, T2 t2, object[] data = null, string message = null, string name = null)
{
if (message == null)
message = "";
return tf_with(ops.name_scope(name, "assert_equal", new { t1, t2, data }), delegate
{
var x = ops.convert_to_tensor(t1, name: "x");
var y = ops.convert_to_tensor(t2, name: "y");
if (data == null)
{
data = new object[]
{
message,
"Condition x == y did not hold element-wise:",
$"x (%s) = {x.name}",
x,
$"y (%s) = {y.name}",
y
};
}
var eq = gen_math_ops.equal(x, y);
var condition = math_ops.reduce_all(eq);
var x_static = tensor_util.constant_value(x);
var y_static = tensor_util.constant_value(y);
return control_flow_ops.Assert(condition, data);
});
}
public static Operation assert_positive(Tensor x, object[] data = null, string message = null, string name = null)
{
if (message == null)
message = "";
return tf_with(ops.name_scope(name, "assert_positive", new { x, data }), delegate
{
x = ops.convert_to_tensor(x, name: "x");
if (data == null)
{
name = x.name;
data = new object[]
{
message,
"Condition x > 0 did not hold element-wise:",
$"x (%s) = {name}",
x
};
}
var zero = ops.convert_to_tensor(0, dtype: x.dtype);
return assert_less(zero, x, data: data);
});
}
public static Operation assert_less(Tensor x, Tensor y, object[] data = null, string message = null, string name = null)
{
if (message == null)
message = "";
return tf_with(ops.name_scope(name, "assert_less", new { x, y, data }), delegate
{
x = ops.convert_to_tensor(x, name: "x");
y = ops.convert_to_tensor(y, name: "y");
string x_name = x.name;
string y_name = y.name;
if (data == null)
{
data = new object[]
{
message,
"Condition x < y did not hold element-wise:",
$"x (%s) = {x_name}",
$"y (%s) = {y_name}",
y
};
}
var condition = math_ops.reduce_all(gen_math_ops.less(x, y));
return control_flow_ops.Assert(condition, data);
});
}
}
}