using System.Collections.Generic;
using System;
using System.Linq;
namespace Tensorflow.Eager
{
public class Execute
{
///
/// Execute a TensorFlow operation.
///
///
/// Name of the TensorFlow operation (see REGISTER_OP in C++ code) to
/// execute.
///
///
/// The number of outputs of the operation to fetch.
///
///
/// A list of inputs to the operation. Each entry should be a Tensor, or
/// a value which can be passed to the Tensor constructor to create one.
///
///
/// A tuple with alternating string attr names and attr values for this
/// operation.
///
/// The value of context.context().
/// Customized name for the operation.
/// List of output Tensor objects. The list is empty if there are no outputs
public EagerTensor[] execute(Context ctx, string op_name, int num_outputs,
EagerTensor[] inputs, object[] attrs,
string name = null)
{
ctx.ensure_initialized();
using var status = new Status();
BindingArray results = c_api.TFE_QuickExecute(ctx,
ctx.device_name,
op_name,
inputs.Select(x => x.EagerTensorHandle).ToArray(),
inputs.Length,
op => wrap_tfe_src.SetOpAttrs(op, attrs),
status);
status.Check(true);
return results.Data().Select(x => new EagerTensor(x)).ToArray();
}
public (TF_DataType, EagerTensor[]) args_to_matching_eager(Context ctx, TF_DataType default_dtype = TF_DataType.DtInvalid, object[] args = null)
{
if (args.Length == 0 && default_dtype != TF_DataType.DtInvalid)
return (default_dtype, null);
if (args.Count(x => x is EagerTensor) == args.Length)
return ((args[0] as EagerTensor).dtype, args.Select(x => x as EagerTensor).ToArray());
var dtype = TF_DataType.DtInvalid;
foreach (var x in args)
{
if (x is EagerTensor et)
dtype = et.dtype;
}
if (dtype == TF_DataType.DtInvalid)
{
var ret = new List();
foreach (var t in args)
{
ret.Add(ops.convert_to_tensor(t, dtype, preferred_dtype: default_dtype, ctx: ctx) as EagerTensor);
if (dtype == TF_DataType.DtInvalid)
dtype = ret.Last().dtype;
}
return (dtype, ret.ToArray());
}
else
throw new NotImplementedException("");
}
}
}