Esempio n. 1
0
        /// <summary>
        /// Get the tensor data as a flatten single dimension array
        /// </summary>
        /// <typeparam name="T">The type of the data array</typeparam>
        /// <returns>The tensor data as a flatten single dimension array</returns>
        public T[] Flat <T>() where T : struct
        {
            Type t = TfInvoke.GetNativeType(this.Type);

            if (typeof(T) != t)
            {
                throw new ArgumentException(String.Format("Tensor type {0} is incompatible with the generic type {1}", this.Type, t.ToString()));
            }
            return(GetData(false) as T[]);
        }
Esempio n. 2
0
        /// <summary>
        /// Get a copy of the tensor data as a managed array
        /// </summary>
        /// <param name="jagged">If true, return a jagged array, otherwise, a flatten single dimension array</param>
        /// <returns>A copy of the tensor data as a managed array</returns>
        public Array GetData(bool jagged = true)
        {
            DataType type = this.Type;
            Type     t    = TfInvoke.GetNativeType(type);

            if (t == null)
            {
                return(null);
            }

            Array array;
            int   byteSize = ByteSize;

            if (jagged)
            {
                int[] dim = this.Dim;
                array = Array.CreateInstance(t, dim);
            }
            else
            {
                int len = byteSize / Marshal.SizeOf(t);
                array = Array.CreateInstance(t, len);
            }

            /*
             * int[] dim = Dim;
             * int byteSize = ByteSize;
             * Array array;
             *
             * if (dim.Length == 0)
             * {
             *  int len = byteSize/ Marshal.SizeOf(t);
             *  array = Array.CreateInstance(t, len);
             * } else if (jagged)
             *  array = Array.CreateInstance(t, dim);
             * else
             * {
             *  int len = 0;
             *  if (dim.Length > 0)
             *      len = dim[0];
             *  for (int i = 1; i < dim.Length; i++)
             *      len *= dim[i];
             *  array = Array.CreateInstance(t, len);
             * }*/
            GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);

            TfInvoke.tfeMemcpy(handle.AddrOfPinnedObject(), DataPointer, byteSize);
            handle.Free();
            return(array);
        }