Repository navigation
Expand file tree
/
Copy pathlinalg_ops.cs
More file actions
40 lines (36 loc) · 1.38 KB
/
Copy pathlinalg_ops.cs
File metadata and controls
40 lines (36 loc) · 1.38 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
using System;
using static Tensorflow.Binding;
namespace Tensorflow
{
public class linalg_ops
{
public Tensor eye(int num_rows,
int num_columns = -1,
TensorShape batch_shape = null,
TF_DataType dtype = TF_DataType.TF_FLOAT,
string name = null)
{
return tf_with(ops.name_scope(name, default_name: "eye", new { num_rows, num_columns, batch_shape }), scope =>
{
if (num_columns == -1)
num_columns = num_rows;
bool is_square = num_columns == num_rows;
var diag_size = Math.Min(num_rows, num_columns);
if (batch_shape == null)
batch_shape = new TensorShape(new int[0]);
var diag_shape = batch_shape.dims.concat(new[] { diag_size });
int[] shape = null;
if (!is_square)
shape = batch_shape.dims.concat(new[] { num_rows, num_columns });
var diag_ones = array_ops.ones(diag_shape, dtype: dtype);
if (is_square)
return array_ops.matrix_diag(diag_ones);
else
{
var zero_matrix = array_ops.zeros(shape, dtype: dtype);
return array_ops.matrix_set_diag(zero_matrix, diag_ones);
}
});
}
}
}