Skip to content

Commit 7426fd0

Browse files
committed
Add FeedItem to hold feed session dictionary.
1 parent f4b6d50 commit 7426fd0

18 files changed

Lines changed: 156 additions & 27 deletions

File tree

‎docs/source/Attribute.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Chapter. Attribute
2+

‎docs/source/EagerMode.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Chapter. Eager Mode
2+

‎docs/source/Gradient.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Chapter. Gradient
2+

‎docs/source/Placeholder.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
# Chapter. Placeholder
22

3+
In this chapter we will talk about another common data type in TensorFlow: Placeholder. It is a simplified variable that can be passed to the required value by the session when the graph is run, that is, when you build the graph, you don't need to specify the value of that variable, but delay the session to the beginning. In TensorFlow terminology, we then feed data into the graph through these placeholders. The difference between placeholders and constants is that placeholders can specify coefficient values more flexibly without modifying the code that builds the graph. For example, mathematical constants are suitable for Constant, and some model smoothing values can be specified with Placeholder.
4+
5+
这章我们讲一下TensorFlow里的另一种常用数据类型:占位符。它是一种简化的变量,可以在图运行的时候由会话传入所需要的值,就是说你在构建图的时候,不需要具体指定那个变量的值,而是延迟到会话开始的时候以参数的方式从外部传入初始值。占位符和常量的区别是占位符可以更灵活的指定系数值,而不需要修改构建图的代码。比如数学常量就适合用Constant, 有些模型平滑值可以用Placeholder来指定。
6+
7+
```csharp
8+
var x = tf.placeholder(tf.int32);
9+
var y = x * 3;
10+
11+
Python.with<Session>(tf.Session(), sess =>
12+
{
13+
var result = sess.run(y, feed_dict: new FeedItem[]
14+
{
15+
new FeedItem(x, 2)
16+
});
17+
// (int)result should be 6;
18+
});
19+
```
20+

‎docs/source/index.rst‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Welcome to TensorFlow.NET's documentation!
1919
Constant
2020
Placeholder
2121
Variable
22+
Attribute
2223
Operation
2324
Graph
24-
Session
25+
Session
26+
Gradient
27+
EagerMode

‎src/TensorFlowNET.Core/APIs/c_api.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace Tensorflow
2121
/// const char* => string
2222
/// int32_t => int
2323
/// int64_t* => long[]
24-
/// size_t* => unlong[]
25-
/// size_t* => ref uint
24+
/// size_t* => ulong[]
25+
/// size_t* => ref ulong
2626
/// void* => IntPtr
2727
/// string => IntPtr c_api.StringPiece(IntPtr)
2828
/// unsigned char => byte

‎src/TensorFlowNET.Core/Sessions/BaseSession.cs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ public BaseSession(string target = "", Graph graph = null)
3535
c_api.TF_DeleteSessionOptions(opts);
3636
}
3737

38-
public virtual NDArray run(Tensor fetches, Dictionary<Tensor, NDArray> feed_dict = null)
38+
public virtual NDArray run(Tensor fetches, FeedItem[] feed_dict = null)
3939
{
40-
return _run(fetches, feed_dict);
40+
var feed = new Dictionary<Tensor, NDArray>();
41+
42+
if (feed_dict != null)
43+
feed_dict.ToList().ForEach(x => feed.Add(x.Key, x.Value));
44+
45+
return _run(fetches, feed);
4146
}
4247

43-
public virtual NDArray run(Operation fetches, Dictionary<Tensor, NDArray> feed_dict = null)
48+
public virtual NDArray run(Operation fetches, FeedItem[] feed_dict = null)
4449
{
45-
return _run(fetches, feed_dict);
50+
var feed = new Dictionary<Tensor, NDArray>();
51+
52+
if (feed_dict != null)
53+
feed_dict.ToList().ForEach(x => feed.Add(x.Key, x.Value));
54+
55+
return _run(fetches, feed);
4656
}
4757

4858
private NDArray _run<T>(T fetches, Dictionary<Tensor, NDArray> feed_dict = null)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NumSharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Tensorflow
7+
{
8+
/// <summary>
9+
/// Feed dictionary item
10+
/// </summary>
11+
public class FeedItem
12+
{
13+
public Tensor Key { get; }
14+
public NDArray Value { get; }
15+
16+
public FeedItem(Tensor tensor, NDArray nd)
17+
{
18+
Key = tensor;
19+
Value = nd;
20+
}
21+
}
22+
}

‎src/TensorFlowNET.Core/Tensors/Tensor.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public Tensor MaybeMove()
213213
/// <param name="feed_dict">A dictionary that maps `Tensor` objects to feed values.</param>
214214
/// <param name="session">The `Session` to be used to evaluate this tensor.</param>
215215
/// <returns></returns>
216-
public NDArray eval(Dictionary<Tensor, NDArray> feed_dict = null, Session session = null)
216+
public NDArray eval(FeedItem[] feed_dict = null, Session session = null)
217217
{
218218
return ops._eval_using_default_session(this, feed_dict, Graph, session);
219219
}

‎src/TensorFlowNET.Core/Tensors/c_api.tensor.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,41 @@ public partial class c_api
9797
/// <returns></returns>
9898
[DllImport(TensorFlowLibName)]
9999
public static extern TF_DataType TF_TensorType(IntPtr tensor);
100+
101+
/// <summary>
102+
/// Return the size in bytes required to encode a string `len` bytes long into a
103+
/// TF_STRING tensor.
104+
/// </summary>
105+
/// <param name="len">size_t</param>
106+
/// <returns></returns>
107+
[DllImport(TensorFlowLibName)]
108+
public static extern ulong TF_StringEncodedSize(ulong len);
109+
110+
/// <summary>
111+
/// Encode the string `src` (`src_len` bytes long) into `dst` in the format
112+
/// required by TF_STRING tensors. Does not write to memory more than `dst_len`
113+
/// bytes beyond `*dst`. `dst_len` should be at least
114+
/// TF_StringEncodedSize(src_len).
115+
/// </summary>
116+
/// <param name="src">const char*</param>
117+
/// <param name="src_len">size_t</param>
118+
/// <param name="dst">char*</param>
119+
/// <param name="dst_len">size_t</param>
120+
/// <param name="status">TF_Status*</param>
121+
/// <returns>On success returns the size in bytes of the encoded string.</returns>
122+
[DllImport(TensorFlowLibName)]
123+
public static extern ulong TF_StringEncode(string src, ulong src_len, string dst, ulong dst_len, IntPtr status);
124+
125+
/// <summary>
126+
/// Decode a string encoded using TF_StringEncode.
127+
/// </summary>
128+
/// <param name="src">const char*</param>
129+
/// <param name="src_len">size_t</param>
130+
/// <param name="dst">const char**</param>
131+
/// <param name="dst_len">size_t*</param>
132+
/// <param name="status">TF_Status*</param>
133+
/// <returns></returns>
134+
[DllImport(TensorFlowLibName)]
135+
public static extern ulong TF_StringDecode(string src, ulong src_len, IntPtr dst, ref ulong dst_len, IntPtr status);
100136
}
101137
}

0 commit comments

Comments
 (0)