| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c102715 commit 1291aa5
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,9 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.UnitTest", "t | |||
| 7 | 7 | EndProject | |
| 8 | 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}" | |
| 9 | 9 | EndProject | |
| 10 | - Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{D314AE83-D586-4589-B653-1937F1138A5A}" | ||
| 10 | + Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}" | ||
| 11 | 11 | EndProject | |
| 12 | - Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}" | ||
| 12 | + Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{1D4158AC-0ED2-46D3-9532-E92232A10743}" | ||
| 13 | 13 | EndProject | |
| 14 | 14 | Global | |
| 15 | 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution | |
@@ -25,14 +25,14 @@ Global | |||
| 25 | 25 | {1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}.Debug|Any CPU.Build.0 = Debug|Any CPU | |
| 26 | 26 | {1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}.Release|Any CPU.ActiveCfg = Release|Any CPU | |
| 27 | 27 | {1B1BC950-2CB0-48E2-B4CD-8172AFF67A10}.Release|Any CPU.Build.0 = Release|Any CPU | |
| 28 | - {D314AE83-D586-4589-B653-1937F1138A5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| 29 | - {D314AE83-D586-4589-B653-1937F1138A5A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| 30 | - {D314AE83-D586-4589-B653-1937F1138A5A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| 31 | - {D314AE83-D586-4589-B653-1937F1138A5A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| 32 | 28 | {1FE60088-157C-4140-91AB-E96B915E4BAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |
| 33 | 29 | {1FE60088-157C-4140-91AB-E96B915E4BAE}.Debug|Any CPU.Build.0 = Debug|Any CPU | |
| 34 | 30 | {1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.ActiveCfg = Release|Any CPU | |
| 35 | 31 | {1FE60088-157C-4140-91AB-E96B915E4BAE}.Release|Any CPU.Build.0 = Release|Any CPU | |
| 32 | + {1D4158AC-0ED2-46D3-9532-E92232A10743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| 33 | + {1D4158AC-0ED2-46D3-9532-E92232A10743}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| 34 | + {1D4158AC-0ED2-46D3-9532-E92232A10743}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| 35 | + {1D4158AC-0ED2-46D3-9532-E92232A10743}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| 36 | 36 | EndGlobalSection | |
| 37 | 37 | GlobalSection(SolutionProperties) = preSolution | |
| 38 | 38 | HideSolutionNode = FALSE | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections.Generic; | |
| 3 | + using System.Runtime.InteropServices; | ||
| 3 | 4 | using System.Text; | |
| 4 | 5 | ||
| 5 | 6 | namespace TensorFlowNET.Core | |
@@ -12,11 +13,21 @@ namespace TensorFlowNET.Core | |||
| 12 | 13 | /// </summary> | |
| 13 | 14 | public class Graph | |
| 14 | 15 | { | |
| 15 | - public IntPtr TFGraph { get; set; } | ||
| 16 | + public IntPtr handle; | ||
| 16 | 17 | ||
| 17 | - public Operation create_op() | ||
| 18 | + public Graph(IntPtr graph) | ||
| 18 | 19 | { | |
| 19 | - var op = new Operation(this); | ||
| 20 | + this.handle = graph; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + public unsafe Operation create_op(object inputs, string op_type = "", string name = "") | ||
| 24 | + { | ||
| 25 | + if (String.IsNullOrEmpty(name)) | ||
| 26 | + { | ||
| 27 | + op_type = name; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + var op = new Operation(this, inputs); | ||
| 20 | 31 | ||
| 21 | 32 | return op; | |
| 22 | 33 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,16 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace TensorFlowNET.Core | ||
| 6 | + { | ||
| 7 | + public sealed class MonoPInvokeCallbackAttribute : Attribute | ||
| 8 | + { | ||
| 9 | + /// <summary> | ||
| 10 | + /// Use this constructor to annotate the type of the callback function that | ||
| 11 | + /// will be invoked from unmanaged code. | ||
| 12 | + /// </summary> | ||
| 13 | + /// <param name="t">T.</param> | ||
| 14 | + public MonoPInvokeCallbackAttribute(Type t) { } | ||
| 15 | + } | ||
| 16 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,10 +7,13 @@ namespace TensorFlowNET.Core | |||
| 7 | 7 | public class Operation | |
| 8 | 8 | { | |
| 9 | 9 | private Graph _graph; | |
| 10 | + private IntPtr _c_op; | ||
| 10 | 11 | ||
| 11 | - public Operation(Graph g) | ||
| 12 | + public Operation(Graph g, object inputs) | ||
| 12 | 13 | { | |
| 13 | 14 | _graph = g; | |
| 15 | + | ||
| 16 | + _c_op = ops._create_c_op(g, inputs); | ||
| 14 | 17 | } | |
| 15 | 18 | } | |
| 16 | 19 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace TensorFlowNET.Core | ||
| 6 | + { | ||
| 7 | + /// <summary> | ||
| 8 | + /// TF_DataType holds the type for a scalar value. E.g., one slot in a tensor. | ||
| 9 | + /// The enum values here are identical to corresponding values in types.proto. | ||
| 10 | + /// </summary> | ||
| 11 | + public enum TF_DataType | ||
| 12 | + { | ||
| 13 | + TF_FLOAT = 1, | ||
| 14 | + TF_DOUBLE = 2, | ||
| 15 | + TF_INT32 = 3, // Int32 tensors are always in 'host' memory. | ||
| 16 | + TF_UINT8 = 4, | ||
| 17 | + TF_INT16 = 5, | ||
| 18 | + TF_INT8 = 6, | ||
| 19 | + TF_STRING = 7, | ||
| 20 | + TF_COMPLEX64 = 8, // Single-precision complex | ||
| 21 | + TF_COMPLEX = 8, // Old identifier kept for API backwards compatibility | ||
| 22 | + TF_INT64 = 9, | ||
| 23 | + TF_BOOL = 10, | ||
| 24 | + TF_QINT8 = 11, // Quantized int8 | ||
| 25 | + TF_QUINT8 = 12, // Quantized uint8 | ||
| 26 | + TF_QINT32 = 13, // Quantized int32 | ||
| 27 | + TF_BFLOAT16 = 14, // Float32 truncated to 16 bits. Only for cast ops. | ||
| 28 | + TF_QINT16 = 15, // Quantized int16 | ||
| 29 | + TF_QUINT16 = 16, // Quantized uint16 | ||
| 30 | + TF_UINT16 = 17, | ||
| 31 | + TF_COMPLEX128 = 18, // Double-precision complex | ||
| 32 | + TF_HALF = 19, | ||
| 33 | + TF_RESOURCE = 20, | ||
| 34 | + TF_VARIANT = 21, | ||
| 35 | + TF_UINT32 = 22, | ||
| 36 | + TF_UINT64 = 23, | ||
| 37 | + } | ||
| 38 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace TensorFlowNET.Core | ||
| 6 | + { | ||
| 7 | + public class Tensor | ||
| 8 | + { | ||
| 9 | + } | ||
| 10 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,36 +3,35 @@ | |||
| 3 | 3 | using System.Runtime.InteropServices; | |
| 4 | 4 | using System.Text; | |
| 5 | 5 | ||
| 6 | + | ||
| 7 | + | ||
| 6 | 8 | namespace TensorFlowNET.Core | |
| 7 | 9 | { | |
| 8 | 10 | public static class Tensorflow | |
| 9 | 11 | { | |
| 10 | - public const string TensorFlowLibName = "libtensorflow"; | ||
| 11 | - | ||
| 12 | - [DllImport(TensorFlowLibName)] | ||
| 13 | - public static extern unsafe IntPtr TF_Version(); | ||
| 12 | + public delegate void Deallocator(IntPtr data, IntPtr size, IntPtr deallocatorData); | ||
| 14 | 13 | ||
| 15 | - public static string VERSION => Marshal.PtrToStringAnsi(TF_Version()); | ||
| 14 | + public static unsafe Tensor constant(object value) | ||
| 15 | + { | ||
| 16 | + var g = ops.get_default_graph(); | ||
| 17 | + g.create_op(value, "Const"); | ||
| 16 | 18 | ||
| 17 | - [DllImport(TensorFlowLibName)] | ||
| 18 | - static extern unsafe IntPtr TF_NewOperation(IntPtr graph, string opType, string oper_name); | ||
| 19 | + return new Tensor(); | ||
| 20 | + } | ||
| 19 | 21 | ||
| 20 | - [DllImport(TensorFlowLibName)] | ||
| 21 | - static extern unsafe IntPtr TF_FinishOperation(IntPtr desc, IntPtr status); | ||
| 22 | + public static Deallocator FreeTensorDataDelegate = FreeTensorData; | ||
| 22 | 23 | ||
| 23 | - public static IntPtr constant<T>(T value) | ||
| 24 | + [MonoPInvokeCallback(typeof(Deallocator))] | ||
| 25 | + internal static void FreeTensorData(IntPtr data, IntPtr len, IntPtr closure) | ||
| 24 | 26 | { | |
| 25 | - var g = Graph(); | ||
| 26 | - return TF_NewOperation(g.TFGraph, "Const", "Const"); | ||
| 27 | + Marshal.FreeHGlobal(data); | ||
| 27 | 28 | } | |
| 28 | 29 | ||
| 29 | - [DllImport(TensorFlowLibName)] | ||
| 30 | - static extern unsafe IntPtr TF_NewGraph(); | ||
| 30 | + public static string VERSION => Marshal.PtrToStringAnsi(c_api.TF_Version()); | ||
| 31 | 31 | ||
| 32 | 32 | public static Graph Graph() | |
| 33 | 33 | { | |
| 34 | - Graph g = new Graph(); | ||
| 35 | - g.TFGraph = TF_NewGraph(); | ||
| 34 | + Graph g = new Graph(c_api.TF_NewGraph()); | ||
| 36 | 35 | return g; | |
| 37 | 36 | } | |
| 38 | 37 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,45 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Runtime.InteropServices; | ||
| 4 | + using System.Text; | ||
| 5 | + | ||
| 6 | + using size_t = System.UIntPtr; | ||
| 7 | + using TF_Graph = System.IntPtr; | ||
| 8 | + using TF_OperationDescription = System.IntPtr; | ||
| 9 | + using TF_Operation = System.IntPtr; | ||
| 10 | + using TF_Status = System.IntPtr; | ||
| 11 | + using TF_Tensor = System.IntPtr; | ||
| 12 | + | ||
| 13 | + using static TensorFlowNET.Core.Tensorflow; | ||
| 14 | + | ||
| 15 | + namespace TensorFlowNET.Core | ||
| 16 | + { | ||
| 17 | + public static class c_api | ||
| 18 | + { | ||
| 19 | + public const string TensorFlowLibName = "libtensorflow"; | ||
| 20 | + | ||
| 21 | + [DllImport(TensorFlowLibName)] | ||
| 22 | + public static unsafe extern TF_Operation TF_FinishOperation(TF_OperationDescription desc, TF_Status status); | ||
| 23 | + | ||
| 24 | + [DllImport(TensorFlowLibName)] | ||
| 25 | + public static unsafe extern IntPtr TF_NewGraph(); | ||
| 26 | + | ||
| 27 | + [DllImport(TensorFlowLibName)] | ||
| 28 | + public static unsafe extern TF_OperationDescription TF_NewOperation(TF_Graph graph, string opType, string oper_name); | ||
| 29 | + | ||
| 30 | + [DllImport(TensorFlowLibName)] | ||
| 31 | + public static unsafe extern TF_Status TF_NewStatus(); | ||
| 32 | + | ||
| 33 | + [DllImport(TensorFlowLibName)] | ||
| 34 | + public static extern unsafe TF_Tensor TF_NewTensor(TF_DataType dataType, IntPtr zeroDims, int num_dims, IntPtr data, size_t len, Deallocator deallocator, IntPtr deallocator_arg); | ||
| 35 | + | ||
| 36 | + [DllImport(TensorFlowLibName)] | ||
| 37 | + public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status); | ||
| 38 | + | ||
| 39 | + [DllImport(TensorFlowLibName)] | ||
| 40 | + public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value); | ||
| 41 | + | ||
| 42 | + [DllImport(TensorFlowLibName)] | ||
| 43 | + public static unsafe extern IntPtr TF_Version(); | ||
| 44 | + } | ||
| 45 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Runtime.InteropServices; | ||
| 4 | + using System.Text; | ||
| 5 | + using System.Threading; | ||
| 6 | + using tf = TensorFlowNET.Core.Tensorflow; | ||
| 7 | + | ||
| 8 | + namespace TensorFlowNET.Core | ||
| 9 | + { | ||
| 10 | + public static class ops | ||
| 11 | + { | ||
| 12 | + public static Graph get_default_graph() | ||
| 13 | + { | ||
| 14 | + return tf.Graph(); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + public static unsafe IntPtr _create_c_op(Graph graph, object inputs) | ||
| 18 | + { | ||
| 19 | + var op_desc = c_api.TF_NewOperation(graph.handle, "Const", "Const0"); | ||
| 20 | + var status = c_api.TF_NewStatus(); | ||
| 21 | + | ||
| 22 | + IntPtr tensor = IntPtr.Zero; | ||
| 23 | + | ||
| 24 | + switch (inputs) | ||
| 25 | + { | ||
| 26 | + case double value: | ||
| 27 | + var v = (double*)Marshal.AllocHGlobal(sizeof(double)); | ||
| 28 | + *v = value; | ||
| 29 | + tensor = c_api.TF_NewTensor(TF_DataType.TF_DOUBLE, IntPtr.Zero, 0, data: (IntPtr)v, len: (UIntPtr)sizeof(double), deallocator: Tensorflow.FreeTensorDataDelegate, deallocator_arg: IntPtr.Zero); | ||
| 30 | + c_api.TF_SetAttrType(op_desc, "dtype", TF_DataType.TF_DOUBLE); | ||
| 31 | + break; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + c_api.TF_SetAttrTensor(op_desc, "value", tensor, status); | ||
| 35 | + | ||
| 36 | + var c_op = c_api.TF_FinishOperation(op_desc, status); | ||
| 37 | + | ||
| 38 | + return c_op; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,8 @@ public void Run() | |||
| 18 | 18 | ||
| 19 | 19 | The value returned by the constructor represents the output | |
| 20 | 20 | of the Constant op.*/ | |
| 21 | - var hello = tf.constant("Hello, TensorFlow!"); | ||
| 21 | + var hello = tf.constant(4.0); | ||
| 22 | + //var hello = tf.constant("Hello, TensorFlow!"); | ||
| 22 | 23 | ||
| 23 | 24 | // Start tf session | |
| 24 | 25 | // var sess = tf.Session(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments