Browse Source

fix gradients parameters issue.

tags/v0.10
Oceania2018 6 years ago
parent
commit
654fc76d0c
8 changed files with 29 additions and 80 deletions
  1. +1
    -1
      src/TensorFlowNET.Core/Gradients/array_grad.cs
  2. +2
    -2
      src/TensorFlowNET.Core/Gradients/gradients_util.cs
  3. +1
    -1
      src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs
  4. +1
    -1
      src/TensorFlowNET.Core/Operations/random_ops.py.cs
  5. +13
    -67
      src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs
  6. +2
    -2
      test/TensorFlowNET.UnitTest/ConstantTest.cs
  7. +7
    -4
      test/TensorFlowNET.UnitTest/GradientTest.cs
  8. +2
    -2
      test/TensorFlowNET.UnitTest/nest_test/NestTest.cs

+ 1
- 1
src/TensorFlowNET.Core/Gradients/array_grad.cs View File

@@ -96,7 +96,7 @@ namespace Tensorflow.Gradients
new Tensor[] { non_neg_concat_dim, tf.constant(0) },
new Tensor[] { tf.constant(1), tf.constant(-1) });
var squeeze_sizes = array_ops.squeeze(slice);
out_grads = gen_array_ops.split(grad, squeeze_sizes, (int)non_neg_concat_dim.Data<int>()[0]).ToList();
out_grads = gen_array_ops.split(grad, squeeze_sizes, (int)non_neg_concat_dim).ToList();
}
else
{


+ 2
- 2
src/TensorFlowNET.Core/Gradients/gradients_util.cs View File

@@ -146,7 +146,7 @@ namespace Tensorflow
string name1 = scope1;
if (grad_fn != null)
{
in_grads = _MaybeCompile(grad_scope, op, out_grads[0].ToArray(), null, grad_fn);
in_grads = _MaybeCompile(grad_scope, op, out_grads.Select(x => x[0]).ToArray(), null, grad_fn);
_VerifyGeneratedGradients(in_grads, op);
}

@@ -333,7 +333,7 @@ namespace Tensorflow
throw new ValueError("_AggregatedGrads out_grad.Length == 0");
}

out_grads[i] = out_grad;
out_grads[i] = new List<Tensor> { out_grad[0] };
}
else
{


+ 1
- 1
src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs View File

@@ -141,7 +141,7 @@ namespace Tensorflow
// Use static rank.
var rank_diff = weights_rank - labels_rank;
if (rank_diff == 1)
weights = (int)array_ops.squeeze(weights_tensor, new int[] { -1 });
weights = (float)array_ops.squeeze(weights_tensor, new int[] { -1 });
return (labels, predictions, weights_tensor);
}



+ 1
- 1
src/TensorFlowNET.Core/Operations/random_ops.py.cs View File

@@ -89,7 +89,7 @@ namespace Tensorflow
{
name = scope;
var minTensor = ops.convert_to_tensor(minval, dtype: dtype, name: "min");
var maxTensor = ops.convert_to_tensor(maxval == null ? 1 : maxval, dtype: dtype, name: "max");
var maxTensor = ops.convert_to_tensor(maxval == null ? 1 : (int)maxval, dtype: dtype, name: "max");
var (seed1, seed2) = random_seed.get_seed(seed);
if (dtype.is_integer())
{


+ 13
- 67
src/TensorFlowNET.Core/Tensors/Tensor.Implicit.cs View File

@@ -1,79 +1,25 @@
/*****************************************************************************
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 NumSharp;
using System;
using System.Collections.Generic;
using System.Text;

namespace Tensorflow
{
public partial class Tensor
{
public static implicit operator Tensor(bool value)
/// <summary>
/// Issue unresolved, will cause name_scope problem.
/// </summary>
/// <param name="scalar"></param>
/*public static implicit operator Tensor(double scalar)
{
return tf.constant(value, TF_DataType.TF_BOOL);
}

public static implicit operator Tensor(sbyte value)
{
return tf.constant(value, TF_DataType.TF_INT8);
}

public static implicit operator Tensor(byte value)
{
return tf.constant(value, TF_DataType.TF_INT16);
}

public static implicit operator Tensor(ushort value)
{
return tf.constant(value, TF_DataType.TF_UINT16);
}

public static implicit operator Tensor(short value)
{
return tf.constant(value, TF_DataType.TF_INT16);
}
return constant_op.constant(scalar);
}*/

public static implicit operator Tensor(int value)
/*public static implicit operator Tensor(int scalar)
{
return tf.constant(value, TF_DataType.TF_INT32);
}

public static implicit operator Tensor(uint value)
{
return tf.constant(value, TF_DataType.TF_UINT32);
}

public static implicit operator Tensor(long value)
{
return tf.constant(value, TF_DataType.TF_INT64);
}

public static implicit operator Tensor(ulong value)
{
return tf.constant(value, TF_DataType.TF_UINT64);
}

public static implicit operator Tensor(float value)
{
return tf.constant(value, TF_DataType.TF_FLOAT);
}

public static implicit operator Tensor(double value)
{
return tf.constant(value, TF_DataType.TF_DOUBLE);
}
return constant_op.constant(scalar);
}*/

public static implicit operator IntPtr(Tensor tensor)
{


+ 2
- 2
test/TensorFlowNET.UnitTest/ConstantTest.cs View File

@@ -19,7 +19,7 @@ namespace TensorFlowNET.UnitTest
var tensor3 = tf.constant(6.0); // double
}

[DataTestMethod]
/*[DataTestMethod]
[DataRow(int.MinValue)]
[DataRow(-1)]
[DataRow(0)]
@@ -85,7 +85,7 @@ namespace TensorFlowNET.UnitTest
var result = sess.run(tensor);
Assert.IsTrue(result.Data<string>()[0] == str);
});
}
}*/

[TestMethod]
public void ZerosConst()


+ 7
- 4
test/TensorFlowNET.UnitTest/GradientTest.cs View File

@@ -91,11 +91,14 @@ namespace TensorFlowNET.UnitTest

var g = tf.gradients(y, new Tensor[] { slice, slice });

var r = slice.eval();
with(tf.Session(graph), sess =>
{
var r = sess.run(slice);

Assert.IsTrue(Enumerable.SequenceEqual(r.shape, new[] { 2, 1, 2 }));
Assert.IsTrue(Enumerable.SequenceEqual(r[0].GetData<int>(), new[] { 11, 13 }));
Assert.IsTrue(Enumerable.SequenceEqual(r[1].GetData<int>(), new[] { 51, 53 }));
Assert.IsTrue(Enumerable.SequenceEqual(r.shape, new[] { 2, 1, 2 }));
Assert.IsTrue(Enumerable.SequenceEqual(r[0].GetData<int>(), new[] { 11, 13 }));
Assert.IsTrue(Enumerable.SequenceEqual(r[1].GetData<int>(), new[] { 51, 53 }));
});
}
}
}

+ 2
- 2
test/TensorFlowNET.UnitTest/nest_test/NestTest.cs View File

@@ -56,7 +56,7 @@ namespace TensorFlowNET.UnitTest.nest_test
//# Check that flatten fails if attributes are not iterable
// with self.assertRaisesRegexp(TypeError, "object is not iterable"):
// flat = nest.flatten(NestTest.BadAttr())
[Ignore]
[TestMethod]
public void testFlattenAndPack()
{
@@ -380,7 +380,7 @@ namespace TensorFlowNET.UnitTest.nest_test
// def testHeterogeneousComparison(self):
// nest.assert_same_structure({"a": 4}, _CustomMapping(a= 3))
// nest.assert_same_structure(_CustomMapping(b=3), {"b": 4})
[Ignore]
[TestMethod]
public void testMapStructure()
{


Loading…
Cancel
Save