Browse Source

implemented _log_prob in normal.py

tags/v0.8.0
Bo Peng 6 years ago
parent
commit
115d4892dc
4 changed files with 58 additions and 6 deletions
  1. +26
    -5
      src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs
  2. +8
    -1
      src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs
  3. +19
    -0
      src/TensorFlowNET.Core/Operations/gen_math_ops.cs
  4. +5
    -0
      src/TensorFlowNET.Core/Operations/math_ops.py.cs

+ 26
- 5
src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs View File

@@ -35,7 +35,7 @@ namespace Tensorflow
/// <param name="name"> Python `str` prepended to names of ops created by this function.</param>
/// <returns>log_prob: a `Tensor` of shape `sample_shape(x) + self.batch_shape` with values of type `self.dtype`.</returns>

/*
public Tensor log_prob(Tensor value, string name = "log_prob")
{
return _call_log_prob(value, name);
@@ -45,18 +45,39 @@ namespace Tensorflow
{
with(ops.name_scope(name, "moments", new { value }), scope =>
{
value = _convert_to_tensor(value, "value", _dtype);
try
{
return _log_prob(value);
}
catch (Exception e1)
{
try
{
return math_ops.log(_prob(value));
} catch (Exception e2)
{
throw new NotImplementedException();
}
}
});
return null;
}

private Tensor _log_prob(Tensor value)
{
throw new NotImplementedException();

}

private Tensor _convert_to_tensor(Tensor value, string name = null, TF_DataType preferred_dtype)
private Tensor _prob(Tensor value)
{
throw new NotImplementedException();
}
*/

public TF_DataType dtype()
{
return this._dtype;
}

/// <summary>
/// Constructs the `Distribution'


+ 8
- 1
src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Tensorflow;

@@ -80,7 +81,7 @@ namespace Tensorflow

private Tensor _log_prob(Tensor x)
{
return _log_unnormalized_prob(_z(x));
return _log_unnormalized_prob(_z(x)) -_log_normalization();
}

private Tensor _log_unnormalized_prob (Tensor x)
@@ -92,5 +93,11 @@ namespace Tensorflow
{
return (x - this._loc) / this._scale;
}

private Tensor _log_normalization()
{
Tensor t = new Tensor(Math.Log(2.0 * Math.PI));
return 0.5 * t + math_ops.log(scale());
}
}
}

+ 19
- 0
src/TensorFlowNET.Core/Operations/gen_math_ops.cs View File

@@ -48,6 +48,12 @@ namespace Tensorflow
return _op.outputs[0];
}
/// <summary>
/// Computes square of x element-wise.
/// </summary>
/// <param name="x"> A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `int32`, `int64`, `complex64`, `complex128`.</param>
/// <param name="name"> A name for the operation (optional).</param>
/// <returns> A `Tensor`. Has the same type as `x`.</returns>
public static Tensor square(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Square", name, args: new { x });
@@ -55,6 +61,19 @@ namespace Tensorflow
return _op.outputs[0];
}
/// <summary>
/// Computes natural logarithm of x element-wise.
/// </summary>
/// <param name="x"> A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `complex64`, `complex128`.</param>
/// <param name="name"> name: A name for the operation (optional).</param>
/// <returns> A `Tensor`. Has the same type as `x`.</returns>
public static Tensor log(Tensor x, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Log", name, args: new { x });
return _op.outputs[0];
}
public static Tensor cast(Tensor x, TF_DataType DstT, bool Truncate= false, string name= "")
{
var _op = _op_def_lib._apply_op_helper("Cast", name, args: new { x, DstT, Truncate });


+ 5
- 0
src/TensorFlowNET.Core/Operations/math_ops.py.cs View File

@@ -60,6 +60,11 @@ namespace Tensorflow
return gen_math_ops.square(x, name);
}

public static Tensor log(Tensor x, string name = null)
{
return gen_math_ops.log(x, name);
}

/// <summary>
/// Helper function for reduction ops.
/// </summary>


Loading…
Cancel
Save