@@ -25,6 +25,19 @@ public Tensor(NDArray nd)
2525 _handle = Allocate ( nd ) ;
2626 }
2727
28+ public unsafe Tensor ( byte [ ] buffer )
29+ {
30+ var size = c_api . TF_StringEncodedSize ( ( UIntPtr ) buffer . Length ) ;
31+ _handle = TF_AllocateTensor ( TF_DataType . TF_STRING , IntPtr . Zero , 0 , ( UIntPtr ) ( ( ulong ) size + 8 ) ) ;
32+
33+ IntPtr tensor = c_api . TF_TensorData ( _handle ) ;
34+ Marshal . WriteInt64 ( tensor , 0 ) ;
35+ fixed ( byte * src = & buffer [ 0 ] )
36+ c_api . TF_StringEncode ( src , ( UIntPtr ) buffer . Length , ( sbyte * ) ( tensor + sizeof ( Int64 ) ) , size , status ) ;
37+
38+ status . Check ( true ) ;
39+ }
40+
2841 private IntPtr Allocate ( NDArray nd )
2942 {
3043 IntPtr dotHandle = IntPtr . Zero ;
@@ -43,20 +56,21 @@ private IntPtr Allocate(NDArray nd)
4356 switch ( nd . dtype . Name )
4457 {
4558 case "Int16" :
46- Marshal . Copy ( nd . Data < short > ( ) , 0 , dotHandle , nd . size ) ;
59+ Marshal . Copy ( nd . ravel ( ) . Data < short > ( ) , 0 , dotHandle , nd . size ) ;
4760 break ;
4861 case "Int32" :
49- Marshal . Copy ( nd . Data < int > ( ) , 0 , dotHandle , nd . size ) ;
62+ Marshal . Copy ( nd . ravel ( ) . Data < int > ( ) , 0 , dotHandle , nd . size ) ;
5063 break ;
5164 case "Single" :
52- Marshal . Copy ( nd . Data < float > ( ) , 0 , dotHandle , nd . size ) ;
65+ Marshal . Copy ( nd . ravel ( ) . Data < float > ( ) , 0 , dotHandle , nd . size ) ;
5366 break ;
5467 case "Double" :
55- Marshal . Copy ( nd . Data < double > ( ) , 0 , dotHandle , nd . size ) ;
68+ Marshal . Copy ( nd . ravel ( ) . Data < double > ( ) , 0 , dotHandle , nd . size ) ;
5669 break ;
57- case "Byte" :
58- var bb = nd . Data < byte > ( ) ;
59- var bytes = Marshal . AllocHGlobal ( bb . Length ) ;
70+ //case "Byte":
71+ /*var bb = nd.Data<byte>();
72+ var bytes = Marshal.AllocHGlobal(bb.Length);
73+ Marshal.Copy(bb, 0, bytes, bb.Length);
6074 ulong bytes_len = c_api.TF_StringEncodedSize((ulong)bb.Length);
6175 var dataTypeByte = ToTFDataType(nd.dtype);
6276 // shape
@@ -70,9 +84,10 @@ private IntPtr Allocate(NDArray nd)
7084 dotHandle = c_api.TF_TensorData(tfHandle2);
7185 Marshal.WriteInt64(dotHandle, 0);
7286 c_api.TF_StringEncode(bytes, (ulong)bb.Length, dotHandle + sizeof(Int64), bytes_len, status);
73- return tfHandle2 ;
74- case "String" :
75- string ss = nd . Data < string > ( ) [ 0 ] ;
87+ return tfHandle2;*/
88+ break ;
89+ //case "String":
90+ /*string ss = nd.Data<string>()[0];
7691 var str = Marshal.StringToHGlobalAnsi(ss);
7792 ulong dst_len = c_api.TF_StringEncodedSize((ulong)ss.Length);
7893 var dataType1 = ToTFDataType(nd.dtype);
@@ -87,7 +102,8 @@ private IntPtr Allocate(NDArray nd)
87102 dotHandle = c_api.TF_TensorData(tfHandle1);
88103 Marshal.WriteInt64(dotHandle, 0);
89104 c_api.TF_StringEncode(str, (ulong)ss.Length, dotHandle + sizeof(Int64), dst_len, status);
90- return tfHandle1 ;
105+ return tfHandle1;*/
106+ break ;
91107 default :
92108 throw new NotImplementedException ( "Marshal.Copy failed." ) ;
93109 }
@@ -101,7 +117,7 @@ private IntPtr Allocate(NDArray nd)
101117
102118 var tfHandle = c_api . TF_NewTensor ( dataType ,
103119 dims ,
104- nd . ndim ,
120+ dims . Length ,
105121 dotHandle ,
106122 size ,
107123 deallocator ,
0 commit comments