You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Binding.Util.cs 13 kB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using NumSharp;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.ComponentModel;
  18. using System.Diagnostics;
  19. using System.Linq;
  20. using NumSharp.Utilities;
  21. namespace Tensorflow
  22. {
  23. /// <summary>
  24. /// Binding utilities to mimic python functions.
  25. /// </summary>
  26. public static partial class Binding
  27. {
  28. public static T2 get<T1, T2>(this Dictionary<T1, T2> dict, T1 key)
  29. => key == null ?
  30. default(T2) :
  31. (dict.ContainsKey(key) ? dict[key] : default(T2));
  32. public static void add<T>(this IList<T> list, T element)
  33. => list.Add(element);
  34. public static void append<T>(this IList<T> list, T element)
  35. => list.Add(element);
  36. public static T[] concat<T>(this IList<T> list1, IList<T> list2)
  37. {
  38. var list = new List<T>();
  39. list.AddRange(list1);
  40. list.AddRange(list2);
  41. return list.ToArray();
  42. }
  43. public static void extend<T>(this List<T> list, IEnumerable<T> elements)
  44. => list.AddRange(elements);
  45. private static string _tostring(object obj)
  46. {
  47. switch (obj)
  48. {
  49. case NDArray nd:
  50. return nd.ToString(false);
  51. case Array arr:
  52. if (arr.Rank!=1 || arr.GetType().GetElementType()?.IsArray == true)
  53. arr = Arrays.Flatten(arr);
  54. var objs = toObjectArray(arr);
  55. return $"[{string.Join(", ", objs.Select(_tostring))}]";
  56. default:
  57. return obj?.ToString() ?? "null";
  58. }
  59. object[] toObjectArray(Array arr)
  60. {
  61. var len = arr.LongLength;
  62. var ret = new object[len];
  63. for (long i = 0; i < len; i++)
  64. {
  65. ret[i] = arr.GetValue(i);
  66. }
  67. return ret;
  68. }
  69. }
  70. public static void print(object obj)
  71. {
  72. Console.WriteLine(_tostring(obj));
  73. }
  74. public static void print(string format, params object[] objects)
  75. {
  76. if (!format.Contains("{}"))
  77. {
  78. Console.WriteLine(format + " " + string.Join(" ", objects.Select(x => x.ToString())));
  79. return;
  80. }
  81. foreach(var obj in objects)
  82. {
  83. }
  84. Console.WriteLine(format);
  85. }
  86. public static int len(object a)
  87. {
  88. switch (a)
  89. {
  90. case Array arr:
  91. return arr.Length;
  92. case IList arr:
  93. return arr.Count;
  94. case ICollection arr:
  95. return arr.Count;
  96. case NDArray ndArray:
  97. return ndArray.ndim == 0 ? 1 : ndArray.shape[0];
  98. case IEnumerable enumerable:
  99. return enumerable.OfType<object>().Count();
  100. }
  101. throw new NotImplementedException("len() not implemented for type: " + a.GetType());
  102. }
  103. public static float min(float a, float b)
  104. => Math.Min(a, b);
  105. public static T[] list<T>(IEnumerable<T> list)
  106. => list.ToArray();
  107. public static IEnumerable<int> range(int end)
  108. {
  109. return Enumerable.Range(0, end);
  110. }
  111. public static IEnumerable<int> range(int start, int end)
  112. {
  113. return Enumerable.Range(start, end - start);
  114. }
  115. public static T New<T>() where T : ITensorFlowObject, new()
  116. {
  117. var instance = new T();
  118. instance.__init__();
  119. return instance;
  120. }
  121. [DebuggerStepThrough]
  122. [DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
  123. public static void tf_with(ITensorFlowObject py, Action<ITensorFlowObject> action)
  124. {
  125. try
  126. {
  127. py.__enter__();
  128. action(py);
  129. }
  130. finally
  131. {
  132. py.__exit__();
  133. py.Dispose();
  134. }
  135. }
  136. [DebuggerStepThrough]
  137. [DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
  138. public static void tf_with<T>(T py, Action<T> action) where T : ITensorFlowObject
  139. {
  140. try
  141. {
  142. py.__enter__();
  143. action(py);
  144. }
  145. finally
  146. {
  147. py.__exit__();
  148. py.Dispose();
  149. }
  150. }
  151. [DebuggerStepThrough]
  152. [DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
  153. public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : ITensorFlowObject
  154. {
  155. try
  156. {
  157. py.__enter__();
  158. return action(py);
  159. }
  160. finally
  161. {
  162. py.__exit__();
  163. py.Dispose();
  164. }
  165. }
  166. public static float time()
  167. {
  168. return (float)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  169. }
  170. public static IEnumerable<(T1, T2)> zip<T1, T2>((T1, T1) t1, (T2, T2) t2)
  171. {
  172. for (int i = 0; i < 2; i++)
  173. {
  174. if (i == 0)
  175. yield return (t1.Item1, t2.Item1);
  176. else
  177. yield return (t1.Item2, t2.Item2);
  178. }
  179. }
  180. public static IEnumerable<(T, T)> zip<T>(NDArray t1, NDArray t2)
  181. where T : unmanaged
  182. {
  183. var a = t1.AsIterator<T>();
  184. var b = t2.AsIterator<T>();
  185. while (a.HasNext() && b.HasNext())
  186. yield return (a.MoveNext(), b.MoveNext());
  187. }
  188. public static IEnumerable<(T1, T2)> zip<T1, T2>(IList<T1> t1, IList<T2> t2)
  189. {
  190. for (int i = 0; i < t1.Count; i++)
  191. yield return (t1[i], t2[i]);
  192. }
  193. public static IEnumerable<(T1, T2, T3)> zip<T1, T2, T3>(IList<T1> t1, IList<T2> t2, IList<T3> t3)
  194. {
  195. for (int i = 0; i < t1.Count; i++)
  196. yield return (t1[i], t2[i], t3[i]);
  197. }
  198. public static IEnumerable<(T1, T2)> zip<T1, T2>(NDArray t1, NDArray t2)
  199. where T1: unmanaged
  200. where T2: unmanaged
  201. {
  202. var a = t1.AsIterator<T1>();
  203. var b = t2.AsIterator<T2>();
  204. while(a.HasNext() && b.HasNext())
  205. yield return (a.MoveNext(), b.MoveNext());
  206. }
  207. public static IEnumerable<(T1, T2)> zip<T1, T2>(IEnumerable<T1> e1, IEnumerable<T2> e2)
  208. {
  209. return e1.Zip(e2, (t1, t2) => (t1, t2));
  210. }
  211. public static IEnumerable<(TKey, TValue)> enumerate<TKey, TValue>(Dictionary<TKey, TValue> values)
  212. {
  213. foreach (var item in values)
  214. yield return (item.Key, item.Value);
  215. }
  216. public static IEnumerable<(TKey, TValue)> enumerate<TKey, TValue>(KeyValuePair<TKey, TValue>[] values)
  217. {
  218. var len = values.Length;
  219. for (var i = 0; i < len; i++)
  220. {
  221. var item = values[i];
  222. yield return (item.Key, item.Value);
  223. }
  224. }
  225. public static IEnumerable<(int, T)> enumerate<T>(IList<T> values)
  226. {
  227. var len = values.Count;
  228. for (int i = 0; i < len; i++)
  229. yield return (i, values[i]);
  230. }
  231. public static IEnumerable<(int, T)> enumerate<T>(IEnumerable<T> values, int start = 0)
  232. {
  233. int i = 0;
  234. foreach(var val in values)
  235. {
  236. if (i < start)
  237. {
  238. i++;
  239. continue;
  240. }
  241. yield return (i, val);
  242. }
  243. }
  244. [DebuggerStepThrough]
  245. public static Dictionary<string, object> ConvertToDict(object dyn)
  246. {
  247. var dictionary = new Dictionary<string, object>();
  248. foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dyn))
  249. {
  250. object obj = propertyDescriptor.GetValue(dyn);
  251. string name = propertyDescriptor.Name;
  252. dictionary.Add(name, obj);
  253. }
  254. return dictionary;
  255. }
  256. public static bool all(IEnumerable enumerable)
  257. {
  258. foreach (var e1 in enumerable)
  259. {
  260. if (!Convert.ToBoolean(e1))
  261. return false;
  262. }
  263. return true;
  264. }
  265. public static bool any(IEnumerable enumerable)
  266. {
  267. foreach (var e1 in enumerable)
  268. {
  269. if (Convert.ToBoolean(e1))
  270. return true;
  271. }
  272. return false;
  273. }
  274. public static double sum(IEnumerable enumerable)
  275. {
  276. var typedef = new Type[] { typeof(double), typeof(int), typeof(float) };
  277. var sum = 0.0d;
  278. foreach (var e1 in enumerable)
  279. {
  280. if (!typedef.Contains(e1.GetType()))
  281. throw new Exception("Numeric array expected");
  282. sum += (double)e1;
  283. }
  284. return sum;
  285. }
  286. public static float sum(IEnumerable<float> enumerable)
  287. => enumerable.Sum();
  288. public static int sum(IEnumerable<int> enumerable)
  289. => enumerable.Sum();
  290. public static double sum<TKey, TValue>(Dictionary<TKey, TValue> values)
  291. {
  292. return sum(values.Keys);
  293. }
  294. public static IEnumerable<double> slice(double start, double end, double step = 1)
  295. {
  296. for (double i = start; i < end; i += step)
  297. yield return i;
  298. }
  299. public static IEnumerable<float> slice(float start, float end, float step = 1)
  300. {
  301. for (float i = start; i < end; i += step)
  302. yield return i;
  303. }
  304. public static IEnumerable<int> slice(int start, int end, int step = 1)
  305. {
  306. for (int i = start; i < end; i += step)
  307. yield return i;
  308. }
  309. public static IEnumerable<int> slice(int range)
  310. {
  311. for (int i = 0; i < range; i++)
  312. yield return i;
  313. }
  314. public static bool hasattr(object obj, string key)
  315. {
  316. var __type__ = (obj).GetType();
  317. var __member__ = __type__.GetMembers();
  318. var __memberobject__ = __type__.GetMember(key);
  319. return (__memberobject__.Length > 0) ? true : false;
  320. }
  321. public static IEnumerable TupleToEnumerable(object tuple)
  322. {
  323. Type t = tuple.GetType();
  324. if (t.IsGenericType && (t.FullName.StartsWith("System.Tuple") || t.FullName.StartsWith("System.ValueTuple")))
  325. {
  326. var flds = t.GetFields();
  327. for (int i = 0; i < flds.Length; i++)
  328. {
  329. yield return flds[i].GetValue(tuple);
  330. }
  331. } else
  332. {
  333. throw new System.Exception("Expected Tuple.");
  334. }
  335. }
  336. public static bool isinstance(object Item1, Type Item2)
  337. {
  338. return Item1.GetType() == Item2;
  339. }
  340. public static bool isinstance(object Item1, object tuple)
  341. {
  342. foreach (var t in TupleToEnumerable(tuple))
  343. if (isinstance(Item1, (Type) t))
  344. return true;
  345. return false;
  346. }
  347. }
  348. }