|
1 | | -using System; |
| 1 | +/***************************************************************************** |
| 2 | + Copyright 2023 Haiping Chen. All Rights Reserved. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +******************************************************************************/ |
| 16 | + |
| 17 | +using System; |
2 | 18 | using System.Linq; |
3 | | -using static Tensorflow.TensorShapeProto.Types; |
| 19 | +using static Tensorflow.Binding; |
4 | 20 |
|
5 | | -namespace Tensorflow.Operations.Initializers |
| 21 | +namespace Tensorflow.Operations.Initializers; |
| 22 | + |
| 23 | +public class Orthogonal : IInitializer |
6 | 24 | { |
7 | | - public class Orthogonal : IInitializer |
| 25 | + float _gain = 0f; |
| 26 | + int? _seed; |
| 27 | + |
| 28 | + public Orthogonal(float gain = 1.0f, int? seed = null) |
8 | 29 | { |
9 | | - float _gain = 0f; |
| 30 | + _gain = gain; |
| 31 | + _seed = seed; |
| 32 | + } |
10 | 33 |
|
11 | | - public Orthogonal(float gain = 1.0f, int? seed = null) |
12 | | - { |
| 34 | + public Tensor Apply(InitializerArgs args) |
| 35 | + { |
| 36 | + return _generate_init_val(args.Shape, args.DType == TF_DataType.DtInvalid ? TF_DataType.TF_FLOAT : args.DType); |
| 37 | + } |
13 | 38 |
|
14 | | - } |
| 39 | + private Tensor _generate_init_val(Shape shape, TF_DataType dtype) |
| 40 | + { |
| 41 | + var num_rows = 1L; |
| 42 | + foreach (var dim in shape.dims.Take(shape.ndim - 1)) |
| 43 | + num_rows *= dim; |
| 44 | + var num_cols = shape.dims.Last(); |
| 45 | + var flat_shape = (Math.Max(num_cols, num_rows), Math.Min(num_cols, num_rows)); |
15 | 46 |
|
16 | | - public Tensor Apply(InitializerArgs args) |
17 | | - { |
18 | | - return _generate_init_val(args.Shape, args.DType); |
19 | | - } |
| 47 | + var a = tf.random.stateless_normal(flat_shape, dtype: dtype); |
| 48 | + // Compute the qr factorization |
| 49 | + var (q, r) = tf.linalg.qr(a, full_matrices: false); |
| 50 | + // Make Q uniform |
| 51 | + var d = tf.linalg.tensor_diag_part(r); |
| 52 | + q *= tf.sign(d); |
20 | 53 |
|
21 | | - private Tensor _generate_init_val(Shape shape, TF_DataType dtype) |
| 54 | + if (num_rows < num_cols) |
22 | 55 | { |
23 | | - var num_rows = 1L; |
24 | | - foreach (var dim in shape.dims.Take(shape.ndim - 1)) |
25 | | - num_rows *= dim; |
26 | | - var num_cols = shape.dims.Last(); |
27 | | - var flat_shape = (Math.Max(num_cols, num_rows), Math.Min(num_cols, num_rows)); |
28 | | - |
| 56 | + // q = tf.linalg.matrix_transpose(q); |
29 | 57 | throw new NotImplementedException(""); |
30 | 58 | } |
| 59 | + |
| 60 | + return _gain * tf.reshape(q, shape); |
31 | 61 | } |
32 | 62 | } |
0 commit comments