Skip to content

Commit 6badc99

Browse files
committed
Initializer that generates tensors with constant values with tf.constant_initializer().
1 parent 8762754 commit 6badc99

10 files changed

Lines changed: 65 additions & 8 deletions

File tree

‎src/TensorFlowNET.Core/APIs/tf.init.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Tensorflow
2020
{
2121
public partial class tensorflow
2222
{
23+
public IInitializer constant_initializer<T>(T value, TF_DataType dtype = TF_DataType.TF_FLOAT, bool verify_shape = false)
24+
=> new Constant<T>(value, dtype: dtype, verify_shape: verify_shape);
2325
public IInitializer zeros_initializer => new Zeros();
2426
public IInitializer ones_initializer => new Ones();
2527
public IInitializer glorot_uniform_initializer => new GlorotUniform();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. 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+
namespace Tensorflow.Operations.Initializers
18+
{
19+
public class Constant<T> : IInitializer
20+
{
21+
TF_DataType dtype;
22+
T value;
23+
bool _verify_shape;
24+
25+
public Constant(T value, TF_DataType dtype = TF_DataType.TF_FLOAT, bool verify_shape = false)
26+
{
27+
this.value = value;
28+
this.dtype = dtype;
29+
_verify_shape = verify_shape;
30+
}
31+
32+
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid, bool? verify_shape = null)
33+
{
34+
if (dtype == TF_DataType.DtInvalid)
35+
dtype = this.dtype;
36+
37+
if (!verify_shape.HasValue)
38+
verify_shape = _verify_shape;
39+
40+
return constant_op._constant_impl(value, dtype, shape,
41+
name: "Const",
42+
verify_shape: verify_shape.Value,
43+
allow_broadcast: false);
44+
}
45+
46+
public object get_config()
47+
{
48+
return new
49+
{
50+
value,
51+
dtype = dtype.name()
52+
};
53+
}
54+
}
55+
}

‎src/TensorFlowNET.Core/Operations/Initializers/IInitializer.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Tensorflow
1818
{
1919
public interface IInitializer
2020
{
21-
Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid);
21+
Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid, bool? verify_shape = null);
2222
object get_config();
2323
}
2424
}

‎src/TensorFlowNET.Core/Operations/Initializers/Ones.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Ones(TF_DataType dtype = TF_DataType.TF_FLOAT)
2525
this.dtype = dtype;
2626
}
2727

28-
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid)
28+
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid, bool? verify_shape = null)
2929
{
3030
if (dtype == TF_DataType.DtInvalid)
3131
dtype = this.dtype;

‎src/TensorFlowNET.Core/Operations/Initializers/RandomNormal.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public RandomNormal(float mean = 0.0f,
3838
this.dtype = dtype;
3939
}
4040

41-
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid)
41+
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid, bool? verify_shape = null)
4242
{
4343
if (dtype == TF_DataType.DtInvalid)
4444
dtype = this.dtype;

‎src/TensorFlowNET.Core/Operations/Initializers/RandomUniform.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public RandomUniform()
2828

2929
}
3030

31-
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid)
31+
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid, bool? verify_shape = null)
3232
{
3333
return random_ops.random_uniform(shape,
3434
minval: minval,

‎src/TensorFlowNET.Core/Operations/Initializers/TruncatedNormal.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public TruncatedNormal(float mean = 0.0f,
3434
this.dtype = dtype;
3535
}
3636

37-
public Tensor call(TensorShape shape, TF_DataType dtype)
37+
public Tensor call(TensorShape shape, TF_DataType dtype, bool? verify_shape = null)
3838
{
3939
return random_ops.truncated_normal(shape, mean, stddev, dtype : dtype, seed: seed);
4040
}

‎src/TensorFlowNET.Core/Operations/Initializers/VarianceScaling.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public VarianceScaling(float scale = 1.0f,
4545
_dtype = dtype;
4646
}
4747

48-
public Tensor call(TensorShape shape, TF_DataType dtype)
48+
public Tensor call(TensorShape shape, TF_DataType dtype, bool? verify_shape = null)
4949
{
5050
var (fan_in, fan_out) = _compute_fans(shape);
5151
if (_mode == "fan_in")

‎src/TensorFlowNET.Core/Operations/Initializers/Zeros.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Zeros(TF_DataType dtype = TF_DataType.TF_FLOAT)
2525
this.dtype = dtype;
2626
}
2727

28-
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid)
28+
public Tensor call(TensorShape shape, TF_DataType dtype = TF_DataType.DtInvalid, bool? verify_shape = null)
2929
{
3030
if (dtype == TF_DataType.DtInvalid)
3131
dtype = this.dtype;

‎src/TensorFlowNET.Core/Tensors/TensorShape.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public override string ToString()
155155
public static implicit operator TensorShape(int[] dims) => new TensorShape(dims);
156156

157157
public static explicit operator int(TensorShape shape) => shape.size;
158-
public static explicit operator TensorShape(int dim) => new TensorShape(dim);
158+
public static implicit operator TensorShape(int dim) => new TensorShape(dim);
159159

160160
public static explicit operator (int, int)(TensorShape shape) => shape.dims.Length == 2 ? (shape.dims[0], shape.dims[1]) : (0, 0);
161161
public static implicit operator TensorShape((int, int) dims) => new TensorShape(dims.Item1, dims.Item2);

0 commit comments

Comments
 (0)