Browse Source

TF_NumDims

tags/v0.1.0-Tensor
Oceania2018 6 years ago
parent
commit
a32d2a603a
14 changed files with 105 additions and 47 deletions
  1. +6
    -0
      The Definitive Guide to Tensorflow.NET/CH_1 Tensor.md
  2. +0
    -0
      The Definitive Guide to Tensorflow.NET/CH_2 Constant.md
  3. +0
    -0
      The Definitive Guide to Tensorflow.NET/CH_3 Operation.md
  4. +0
    -0
      The Definitive Guide to Tensorflow.NET/CH_4 Variable.md
  5. +0
    -0
      The Definitive Guide to Tensorflow.NET/CH_5 Session.md
  6. +0
    -0
      The Definitive Guide to Tensorflow.NET/CH_6 Graph.md
  7. +25
    -0
      The Definitive Guide to Tensorflow.NET/INTRO.md
  8. +26
    -0
      The Definitive Guide to Tensorflow.NET/README.md
  9. +0
    -0
      docs/README.md
  10. +0
    -15
      src/TensorFlowNET.Core/Tensor/TF_Tensor.cs
  11. +14
    -5
      src/TensorFlowNET.Core/Tensor/Tensor.cs
  12. +29
    -25
      src/TensorFlowNET.Core/c_api.cs
  13. +1
    -1
      test/TensorFlowNET.UnitTest/OperationsTest.cs
  14. +4
    -1
      test/TensorFlowNET.UnitTest/TensorTest.cs

+ 6
- 0
The Definitive Guide to Tensorflow.NET/CH_1 Tensor.md View File

@@ -0,0 +1,6 @@
# 第一章: Tensor

### Represents one of the outputs of an Operation

### 表示一个操作的输出


+ 0
- 0
The Definitive Guide to Tensorflow.NET/CH_2 Constant.md View File


+ 0
- 0
The Definitive Guide to Tensorflow.NET/CH_3 Operation.md View File


+ 0
- 0
The Definitive Guide to Tensorflow.NET/CH_4 Variable.md View File


+ 0
- 0
The Definitive Guide to Tensorflow.NET/CH_5 Session.md View File


+ 0
- 0
The Definitive Guide to Tensorflow.NET/CH_6 Graph.md View File


+ 25
- 0
The Definitive Guide to Tensorflow.NET/INTRO.md View File

@@ -0,0 +1,25 @@














Why do I start the Tensorflow.NET project?

我为什么会写Tensorflow.NET?

再过几天就是2018年圣诞节,看着孩子一天天长大并懂事,感慨时间过得太快。IT技术更新换代比以往任何时候都更快,各种前后端技术纷纷涌现。大数据,人工智能和区块链,容器技术和微服务,分布式计算和无服务器技术,让人眼花缭乱。Amazon AI服务接口宣称不需要具有任何机器学习经验的工程师就能使用,让像我这样刚静下心来学习了两年并打算将来转行做AI架构的想法泼了一桶凉水。

TensorFlow is an open source project for machine learning especially for deep learning. It's used for both research and production at Google company. It's designed according to dataflow programming pattern across a range of tasks.



为了避免混淆,本书中对TensorFlow中定义的特有类不进行翻译,比如Tensor, Graph, Shape这些词都会保留英文名称。

+ 26
- 0
The Definitive Guide to Tensorflow.NET/README.md View File

@@ -0,0 +1,26 @@
# The Definitive Guide to Tensorflow.NET
# Tensorflow.NET 权威指南




### The CSharp binding for Google's TensorFlow - An Open Source Machine Learning Framework for Everyone
### 谷歌TensorFlow的C#封装库,开源机器学习框架。







<p style='float:right;'>
Haiping Chen & Christian Kahr<br/>
Christmas, 2018<br/>
陈海平 & 克里斯汀 卡尔<br/>
2018年圣诞节
</p>






+ 0
- 0
docs/README.md View File


+ 0
- 15
src/TensorFlowNET.Core/Tensor/TF_Tensor.cs View File

@@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace Tensorflow
{
[StructLayout(LayoutKind.Sequential)]
public struct TF_Tensor
{
public TF_DataType dtype;
public IntPtr shape;
public IntPtr buffer;
}
}

+ 14
- 5
src/TensorFlowNET.Core/Tensor/Tensor.cs View File

@@ -21,15 +21,14 @@ namespace Tensorflow
private readonly IntPtr _handle;
public IntPtr handle => _handle;

private TF_Tensor tensor;

public IntPtr buffer => c_api.TF_TensorData(tensor.buffer);
private readonly int _ndim;
public int ndim => _ndim;

public Tensor(IntPtr handle)
{
_handle = handle;
tensor = Marshal.PtrToStructure<TF_Tensor>(handle);
_dtype = tensor.dtype;
_dtype = c_api.TF_TensorType(_handle);
_ndim = c_api.TF_NumDims(_handle);
}

public Tensor(Operation op, int value_index, TF_DataType dtype)
@@ -43,5 +42,15 @@ namespace Tensorflow
{
return c_api_util.tf_output(_op._c_op, _value_index);
}

public T Data<T>()
{
/*var buffer = new byte[6 * sizeof(float)];
var h1 = c_api.TF_TensorData(handle);
var bytes = Marshal.PtrToStructure<float>(h1);
Marshal.Copy(h1, buffer, 0, 24);*/

return default(T);
}
}
}

+ 29
- 25
src/TensorFlowNET.Core/c_api.cs View File

@@ -3,13 +3,6 @@ using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

using size_t = System.UIntPtr;
using TF_Graph = System.IntPtr;
using TF_Operation = System.IntPtr;
using TF_Status = System.IntPtr;
using TF_Session = System.IntPtr;
using TF_SessionOptions = System.IntPtr;

namespace Tensorflow
{
public static class c_api
@@ -25,31 +18,34 @@ namespace Tensorflow
public static unsafe extern void TF_AddInput(TF_OperationDescription desc, TF_Output input);

[DllImport(TensorFlowLibName)]
public static unsafe extern void TF_DeleteSessionOptions(TF_SessionOptions opts);
public static unsafe extern void TF_DeleteSessionOptions(IntPtr opts);

[DllImport(TensorFlowLibName)]
public static extern unsafe long TF_Dim(IntPtr tensor, int dim_index);

[DllImport(TensorFlowLibName)]
public static unsafe extern TF_Operation TF_FinishOperation(TF_OperationDescription desc, TF_Status status);
public static unsafe extern IntPtr TF_FinishOperation(TF_OperationDescription desc, IntPtr status);

[DllImport(TensorFlowLibName)]
public static extern string TF_GetBuffer(IntPtr buffer);

[DllImport(TensorFlowLibName)]
public static extern unsafe TF_Code TF_GetCode(TF_Status s);
public static extern unsafe TF_Code TF_GetCode(IntPtr s);

[DllImport(TensorFlowLibName)]
public static extern void TF_GraphGetOpDef(TF_Graph graph, string op_name, IntPtr output_op_def, TF_Status status);
public static extern void TF_GraphGetOpDef(IntPtr graph, string op_name, IntPtr output_op_def, IntPtr status);

[DllImport(TensorFlowLibName)]
public static extern unsafe string TF_Message(TF_Status s);
public static extern unsafe string TF_Message(IntPtr s);

[DllImport(TensorFlowLibName)]
public static unsafe extern TF_Graph TF_NewGraph();
public static unsafe extern IntPtr TF_NewGraph();

[DllImport(TensorFlowLibName)]
public static unsafe extern TF_OperationDescription TF_NewOperation(TF_Graph graph, string opType, string oper_name);
public static unsafe extern TF_OperationDescription TF_NewOperation(IntPtr graph, string opType, string oper_name);

[DllImport(TensorFlowLibName)]
public static unsafe extern TF_Status TF_NewStatus();
public static unsafe extern IntPtr TF_NewStatus();

/// <summary>
/// Return a new tensor that holds the bytes data[0,len-1]
@@ -63,16 +59,24 @@ namespace Tensorflow
/// <param name="deallocator_arg"></param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, size_t len, tf.Deallocator deallocator, IntPtr deallocator_arg);
public static extern unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, UIntPtr len, tf.Deallocator deallocator, IntPtr deallocator_arg);

/// <summary>
/// Return the number of dimensions that the tensor has.
/// </summary>
/// <param name="tensor"></param>
/// <returns></returns>
[DllImport(TensorFlowLibName)]
public static extern unsafe int TF_NumDims(IntPtr tensor);

[DllImport(TensorFlowLibName)]
public static extern unsafe int TF_OperationNumOutputs(TF_Operation oper);
public static extern unsafe int TF_OperationNumOutputs(IntPtr oper);

[DllImport(TensorFlowLibName)]
public static extern unsafe void TF_SetAttrValueProto(TF_OperationDescription desc, string attr_name, IntPtr proto, size_t proto_len, TF_Status status);
public static extern unsafe void TF_SetAttrValueProto(TF_OperationDescription desc, string attr_name, IntPtr proto, UIntPtr proto_len, IntPtr status);

[DllImport(TensorFlowLibName)]
public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status);
public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, IntPtr value, IntPtr status);

/// <summary>
///
@@ -90,12 +94,12 @@ namespace Tensorflow
/// <param name="run_metadata"></param>
/// <param name="status"></param>
[DllImport(TensorFlowLibName)]
public static extern unsafe void TF_SessionRun(TF_Session session, IntPtr run_options,
public static extern unsafe void TF_SessionRun(IntPtr session, IntPtr run_options,
TF_Output[] inputs, IntPtr[] input_values, int ninputs,
TF_Output[] outputs, IntPtr[] output_values, int noutputs,
TF_Operation[] target_opers, int ntargets,
IntPtr run_metadata,
TF_Status status);
IntPtr[] target_opers, int ntargets,
IntPtr run_metadata,
IntPtr status);

[DllImport(TensorFlowLibName)]
public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value);
@@ -107,10 +111,10 @@ namespace Tensorflow
public static extern unsafe TF_DataType TF_TensorType(IntPtr tensor);

[DllImport(TensorFlowLibName)]
public static extern TF_Session TF_NewSession(TF_Graph graph, TF_SessionOptions opts, TF_Status status);
public static extern IntPtr TF_NewSession(IntPtr graph, IntPtr opts, IntPtr status);

[DllImport(TensorFlowLibName)]
public static extern TF_SessionOptions TF_NewSessionOptions();
public static extern IntPtr TF_NewSessionOptions();

[DllImport(TensorFlowLibName)]
public static unsafe extern IntPtr TF_Version();


+ 1
- 1
test/TensorFlowNET.UnitTest/OperationsTest.cs View File

@@ -34,7 +34,7 @@ namespace TensorFlowNET.UnitTest
feed_dict.Add(a, 3.0f);
feed_dict.Add(b, 2.0f);

var o = sess.run(c, feed_dict);
//var o = sess.run(c, feed_dict);
}
}



+ 4
- 1
test/TensorFlowNET.UnitTest/TensorTest.cs View File

@@ -13,7 +13,7 @@ namespace TensorFlowNET.UnitTest
public class TensorTest
{
[TestMethod]
public unsafe void NewTF_Tensor()
public void TF_NewTensor()
{
var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3);

@@ -33,6 +33,9 @@ namespace TensorFlowNET.UnitTest
var tensor = new Tensor(handle);

Assert.AreEqual(tensor.dtype, TF_DataType.TF_FLOAT);
Assert.AreEqual(tensor.ndim, nd.ndim);
Assert.AreEqual(nd.shape[0], c_api.TF_Dim(handle, 0));
Assert.AreEqual(nd.shape[1], c_api.TF_Dim(handle, 1));
}
}
}

Loading…
Cancel
Save