Skip to content

Commit 91fb30f

Browse files
committed
Add PersistentTape unit test.
1 parent c1dcb8c commit 91fb30f

5 files changed

Lines changed: 26 additions & 43 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*****************************************************************************
2-
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
2+
Copyright 2020 The TensorFlow.NET Authors. All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -20,9 +20,15 @@ namespace Tensorflow
2020
{
2121
public partial class tensorflow
2222
{
23+
/// <summary>
24+
/// Record operations for automatic differentiation.
25+
/// </summary>
26+
/// <param name="persistent"></param>
27+
/// <param name="watch_accessed_variables"></param>
28+
/// <returns></returns>
2329
public GradientTape GradientTape(bool persistent = false,
24-
bool watch_accessed_variables = true)
25-
=> new GradientTape(persistent: persistent,
30+
bool watch_accessed_variables = true)
31+
=> new GradientTape(persistent: persistent,
2632
watch_accessed_variables: watch_accessed_variables);
2733

2834
public Tensor[] gradients(Tensor[] ys,

‎src/TensorFlowNET.Core/Eager/c_api.eager.cs‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -389,42 +389,6 @@ public delegate void delete_backward_function_callback(string op_name,
389389
[DllImport(TensorFlowLibName)]
390390
public static extern TFE_Executor TFE_ContextGetExecutorForThread(SafeContextHandle ctx);
391391

392-
/// <summary>
393-
///
394-
/// </summary>
395-
/// <param name="ctx"></param>
396-
/// <param name="device_name"></param>
397-
/// <param name="op_name"></param>
398-
/// <param name="name"></param>
399-
/// <param name="args"></param>
400-
/// <param name="input_size"></param>
401-
/// <param name="set_op_attrs"></param>
402-
/// <param name="status"></param>
403-
/// <returns>EagerTensorHandle</returns>
404-
[DllImport(TensorFlowLibName)]
405-
public static extern SafeStatusHandle TFE_FastPathExecute(SafeContextHandle ctx,
406-
string device_name,
407-
string op_name,
408-
string name,
409-
IntPtr[] inputs,
410-
int input_size,
411-
string attrs_string,
412-
TFE_FastPathExecute_SetOpAttrs set_op_attrs,
413-
IntPtr[] outputs,
414-
int output_size);
415-
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
416-
public delegate void TFE_FastPathExecute_SetOpAttrs(IntPtr op);
417-
418-
[DllImport(TensorFlowLibName)]
419-
public static extern SafeStatusHandle TFE_QuickExecute(SafeContextHandle ctx,
420-
string device_name,
421-
string op_name,
422-
IntPtr[] inputs,
423-
int input_size,
424-
TFE_FastPathExecute_SetOpAttrs set_op_attrs,
425-
IntPtr[] outputs,
426-
int output_size);
427-
428392
[DllImport(TensorFlowLibName)]
429393
public static extern IntPtr TFE_TapeSetNew(bool persistent, bool watch_accessed_variables);
430394

‎src/TensorFlowNET.Core/Gradients/GradientTape.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ public Tensor[] gradient(Tensor target, ResourceVariable[] sources)
142142
return results;
143143
}
144144

145+
/// <summary>
146+
/// Temporarily stops recording operations on this tape.
147+
/// </summary>
148+
public void stop_recording()
149+
{
150+
_pop_tape();
151+
}
152+
145153
public void Dispose()
146154
{
147155
if (_recording)

‎src/TensorFlowNET.Core/Operations/array_ops.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ public static Tensor ones(TensorShape shape, TF_DataType dtype = TF_DataType.TF_
328328
{
329329
dtype = dtype.as_base_dtype();
330330
name = scope;
331-
var shape_tensor = constant_op._tensor_shape_tensor_conversion_function(shape);
331+
332332
Tensor ones = null;
333333
switch (dtype)
334334
{
@@ -342,6 +342,11 @@ public static Tensor ones(TensorShape shape, TF_DataType dtype = TF_DataType.TF_
342342
ones = constant(1);
343343
break;
344344
}
345+
346+
if (shape.ndim == 0)
347+
return ones;
348+
349+
var shape_tensor = constant_op._tensor_shape_tensor_conversion_function(shape);
345350
return fill(shape_tensor, ones, name: name);
346351
});
347352

‎test/TensorFlowNET.UnitTest/NativeAPI/Eager/GradientEagerTest.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public void PersistentTape()
4646
tape.watch(x);
4747
var y = tf.reduce_sum(x);
4848
var z = tf.multiply(y, y);
49+
tape.Dispose();
50+
4951
var dz_dx = tape.gradient(z, x);
5052

5153
var expected = new float[] { 8.0f, 8.0f, 8.0f, 8.0f };
5254
Assert.IsTrue(Enumerable.SequenceEqual(dz_dx.ToArray<float>(), expected));
5355

5456
var dz_dy = tape.gradient(z, y);
55-
56-
expected = new float[] { 8.0f, 8.0f, 8.0f, 8.0f };
57-
Assert.IsTrue(Enumerable.SequenceEqual(dz_dx.ToArray<float>(), expected));
57+
Assert.AreEqual((float)dz_dy, 8.0f);
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)