C语言 Tensorflow 中的有效操作列表



我正在通过渴望的C API使用(学习(Tensorflow,或者通过围绕它的FreePascal包装器更精确地使用Tensorflow。 当我想做例如矩阵乘法时,我调用

TFE_Execute(Op, @OutTensH, @NumOutVals, Status);

其中Op.Op_Name是"MatMul"。我还有其他一些说明,例如"转置","Softmax","Inv"等,但我没有完整的列表。特别是我想获取矩阵的行列式,但找不到它(假设它存在(。我试图在网上以及 GitHub 上的源代码中找到它,但没有成功。 在Python中有tf.linalg.det,但我已经在API C++找不到它。

  1. 有人可以将我引导到可以找到受支持操作的完整列表的地方吗?
  2. 有人可以告诉我如何使用 Tensorflow 计算行列式吗?

编辑:应高拉夫的要求,我附上了一个小程序。如上所述,它是在 Pascal 中,并通过包装器调用 C API。因此,我也在这里复制了包装器的相关部分(完整版:https://macpgmr.github.io/(。设置有效,"唯一"问题是我没有找到支持的操作列表。

// A minimal program to transpose a matrix
program test;
uses
SysUtils,
TF;
var
Tensor:TTensor;
begin
Tensor:=TTensor.CreateSingle([2,1],[1.0,2.0]);
writeln('Before transpose ',Tensor.Dim[0],' x ',Tensor.Dim[1]);         // 2 x 1
Tensor:=Tensor.Temp.ExecOp('Transpose',TTensor.CreateInt32([1,0]).Temp);
writeln('After transpose  ',Tensor.Dim[0],' x ',Tensor.Dim[1]);         // 1 x 2
FreeAndNil(Tensor);                                                            
end.

// extract from TF.pas ( (C) Phil Hess ). It basically re-packages the operation
// and calls the relevant C TFE_Execute, with the same operation name passed on:
// in our case 'Transpose'.
// I am looking for a complete list of supported operations.
function TTensor.ExecOp(const OpName  : string;
Tensor2 : TTensor = nil;
Tensor3 : TTensor = nil;
Tensor4 : TTensor = nil) : TTensor;
var
Status     : TF_StatusPtr;
Op         : TFE_OpPtr;
NumOutVals : cint;
OutTensH   : TFE_TensorHandlePtr;
begin
Result := nil;
Status := TF_NewStatus();
Op := TFE_NewOp(Context, PAnsiChar(OpName), Status);
try
if not CheckStatus(Status) then
Exit;
{Add operation input tensors}
TFE_OpAddInput(Op, TensorH, Status);
if not CheckStatus(Status) then
Exit;
if Assigned(Tensor2) then  {Operation has 2nd tensor input?}
begin
TFE_OpAddInput(Op, Tensor2.TensorH, Status);
if not CheckStatus(Status) then
Exit;
end;
if Assigned(Tensor3) then  {Operation has 3rd tensor input?}
begin
TFE_OpAddInput(Op, Tensor3.TensorH, Status);
if not CheckStatus(Status) then
Exit;
end;
if Assigned(Tensor4) then  {Operation has 4th tensor input?}
begin
TFE_OpAddInput(Op, Tensor4.TensorH, Status);
if not CheckStatus(Status) then
Exit;
end;
{Set operation attributes}
TFE_OpSetAttrType(Op, 'T', DataType);  //typically result type same as input's
if OpName = 'MatMul' then
begin
TFE_OpSetAttrBool(Op, 'transpose_a', #0);  //default (False)
TFE_OpSetAttrBool(Op, 'transpose_b', #0);  //default (False)
end
else if OpName = 'Transpose' then
TFE_OpSetAttrType(Op, 'Tperm', Tensor2.DataType)  //permutations type
else if OpName = 'Sum' then
begin
TFE_OpSetAttrType(Op, 'Tidx', Tensor2.DataType);  //reduction_indices type
TFE_OpSetAttrBool(Op, 'keep_dims', #0);          //default (False)
end
else if (OpName = 'RandomUniform') or (OpName = 'RandomStandardNormal') then
begin
TFE_OpSetAttrInt(Op, 'seed', 0);           //default
TFE_OpSetAttrInt(Op, 'seed2', 0);          //default
TFE_OpSetAttrType(Op, 'dtype', TF_FLOAT);  //for now, use this as result type
end
else if OpName = 'OneHot' then
begin
TFE_OpSetAttrType(Op, 'T', Tensor3.DataType);  //result type must be same as on/off
TFE_OpSetAttrInt(Op, 'axis', -1);              //default
TFE_OpSetAttrType(Op, 'TI', DataType);         //indices type
end;
NumOutVals := 1;
try
// **** THIS IS THE ACTUAL CALL TO THE C API, WHERE Op HAS THE OPNAME
TFE_Execute(Op, @OutTensH, @NumOutVals, Status);
// ***********************************************************************
except on e:Exception do
raise Exception.Create('TensorFlow unable to execute ' + OpName +
' operation: ' + e.Message);
end;
if not CheckStatus(Status) then
Exit;
Result := TTensor.CreateWithHandle(OutTensH);
finally
if Assigned(Op) then
TFE_DeleteOp(Op);
TF_DeleteStatus(Status);
{Even if exception occurred, don't want to leave any temps dangling}
if Assigned(Tensor2) and Tensor2.IsTemp then
Tensor2.Free;
if Assigned(Tensor3) and Tensor3.IsTemp then
Tensor3.Free;
if Assigned(Tensor4) and Tensor4.IsTemp then
Tensor4.Free;
if IsTemp then
Free;
end;
end;

与此同时,我在以下位置找到了TensorFlow的描述文件:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/ops/ops.pbtxt。这包括所有操作及其详细规范。 如果有人对 TF 的 pascal 接口感兴趣,我在 https://github.com/zsoltszakaly/tensorflowforpascal 创建了一个。

以下是从 Python 获取有效操作名称列表的方法:

from tensorflow.python.framework.ops import op_def_registry
registered_ops = op_def_registry.get_registered_ops()
valid_op_names = sorted(registered_ops.keys())
print(len(valid_op_names))  # Number of operation names in TensorFlow 2.0
1223
print(*valid_op_names, sep='n')
# Abort
# Abs
# AccumulateNV2
# AccumulatorApplyGradient
# AccumulatorNumAccumulated
# AccumulatorSetGlobalStep
# AccumulatorTakeGradient
# Acos
# Acosh
# Add
# ...

最新更新