77using tf = TensorFlowNET . Core . Tensorflow ;
88using TF_DataType = Tensorflow . DataType ;
99using node_def_pb2 = Tensorflow ;
10+ using Google . Protobuf ;
1011
1112namespace TensorFlowNET . Core
1213{
@@ -20,49 +21,17 @@ public static Graph get_default_graph()
2021 public static unsafe IntPtr _create_c_op ( Graph graph , NodeDef node_def , object inputs )
2122 {
2223 var op_desc = c_api . TF_NewOperation ( graph . handle , node_def . Op , node_def . Name ) ;
23- var status = c_api . TF_NewStatus ( ) ;
24-
25- // Doesn't work
26- /*foreach(var attr in node_def.Attr)
27- {
28- if (attr.Value.Tensor != null)
29- {
30- switch (attr.Value.Tensor.Dtype)
31- {
32- case DataType.DtDouble:
33- var proto = (double*)Marshal.AllocHGlobal(sizeof(double));
34- *proto = attr.Value.Tensor.DoubleVal[0];
35- c_api.TF_SetAttrValueProto(op_desc, attr.Key, proto, proto_len: (UIntPtr)sizeof(double), status: status);
36- break;
37- }
38- }
39- else
40- {
41- //c_api.TF_SetAttrValueProto(op_desc, attr.Key, null, proto_len: UIntPtr.Zero, status: status);
42- }
43- } */
24+ var status = new Status ( ) ;
4425
4526 foreach ( var attr in node_def . Attr )
4627 {
47- if ( attr . Value . Tensor == null ) continue ;
48- switch ( attr . Value . Tensor . Dtype )
49- {
50- case DataType . DtDouble :
51- var v = ( double * ) Marshal . AllocHGlobal ( sizeof ( double ) ) ;
52- * v = attr . Value . Tensor . DoubleVal [ 0 ] ;
53- var tensor = c_api . TF_NewTensor ( TF_DataType . DtDouble , 0 , 0 , data : ( IntPtr ) v , len : ( UIntPtr ) sizeof ( double ) , deallocator : Tensorflow . FreeTensorDataDelegate , deallocator_arg : IntPtr . Zero ) ;
54- c_api . TF_SetAttrTensor ( op_desc , "value" , tensor , status ) ;
55- c_api . TF_SetAttrType ( op_desc , "dtype" , TF_DataType . DtDouble ) ;
56- break ;
57- case DataType . DtString :
58-
59- var proto = Marshal . StringToHGlobalAnsi ( attr . Value . Tensor . StringVal [ 0 ] . ToStringUtf8 ( ) ) ;
60- c_api . TF_SetAttrValueProto ( op_desc , attr . Key , proto . ToPointer ( ) , proto_len : ( UIntPtr ) 32 , status : status ) ;
61- break ;
62- }
28+ var bytes = attr . Value . ToByteArray ( ) ;
29+ var proto = Marshal . AllocHGlobal ( bytes . Length ) ;
30+ Marshal . Copy ( bytes , 0 , proto , bytes . Length ) ;
31+ c_api . TF_SetAttrValueProto ( op_desc , attr . Key , proto , proto_len : ( UIntPtr ) bytes . Length , status : status . Handle ) ;
6332 }
6433
65- var c_op = c_api . TF_FinishOperation ( op_desc , status ) ;
34+ var c_op = c_api . TF_FinishOperation ( op_desc , status . Handle ) ;
6635
6736 return c_op ;
6837 }
0 commit comments