|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Tensorflow.NumPy;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Tensorflow;
- using static Tensorflow.Binding;
- using Buffer = Tensorflow.Buffer;
-
- namespace TensorFlowNET.UnitTest.Basics
- {
- [TestClass]
- public class OperationsTest : GraphModeTestBase
- {
- /// <summary>
- /// Port from tensorflow\c\c_api_test.cc
- /// `TEST(CAPI, GetAllOpList)`
- /// </summary>
- [TestMethod]
- public void GetAllOpList()
- {
- var handle = c_api.TF_GetAllOpList();
- var buffer = new Buffer(handle);
- var op_list = OpList.Parser.ParseFrom(buffer.ToArray());
-
- var _registered_ops = new Dictionary<string, OpDef>();
- foreach (var op_def in op_list.Op)
- _registered_ops[op_def.Name] = op_def;
-
- // r1.14 added NN op
- var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
- Assert.IsTrue(op_list.Op.Count > 1000);
- }
-
- [TestMethod]
- public void addInPlaceholder()
- {
- var a = tf.placeholder(tf.float32);
- var b = tf.placeholder(tf.float32);
- var c = tf.add(a, b);
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, 3.0f),
- new FeedItem(b, 2.0f));
- Assert.AreEqual(o, 5.0f);
- }
-
- [TestMethod]
- public void addInConstant()
- {
- var a = tf.constant(4.0f);
- var b = tf.constant(5.0f);
- var c = tf.add(a, b);
-
- var sess = tf.Session();
- var o = sess.run(c);
- Assert.AreEqual(o, 9.0f);
- }
-
- [TestMethod]
- public void isFinite()
- {
- var a = tf.constant(new[] { 1, np.nan, 2, np.nan, 3, np.nan, 4, np.nan });
- var b = tf.cast(tf.is_finite(a), tf.float32);
- var check = np.array(1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
-
- var sess = tf.Session();
- var o = sess.run(b);
- Assert.IsTrue(np.array_equal(o, check));
- }
-
- [TestMethod]
- public void isNan()
- {
- var a = tf.constant(new[] { 1, np.nan, 2, np.nan, 3, np.nan, 4, np.nan });
- var b = tf.cast(tf.is_nan(a), tf.float32);
- var check = np.array(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
-
- var sess = tf.Session();
- var o = sess.run(b);
- Assert.IsTrue(np.array_equal(o, check));
- }
-
- [TestMethod]
- public void cumSumTest()
- {
- var a = tf.constant(new[] { 1, 1, 2, 3, 4, 5 });
- var b = tf.cumsum(a);
- var check = np.array(1, 2, 4, 7, 11, 16);
-
- var sess = tf.Session();
- var o = sess.run(b);
- Assert.IsTrue(np.array_equal(o, check));
-
- b = tf.cumsum(a, exclusive: true);
- check = np.array(0, 1, 2, 4, 7, 11);
-
- sess = tf.Session();
- o = sess.run(b);
- Assert.IsTrue(np.array_equal(o, check));
-
- b = tf.cumsum(a, reverse: true);
- check = np.array(16, 15, 14, 12, 9, 5);
-
- sess = tf.Session();
- o = sess.run(b);
- Assert.IsTrue(np.array_equal(o, check));
-
- b = tf.cumsum(a, exclusive: true, reverse: true);
- check = np.array(15, 14, 12, 9, 5, 0);
-
- sess = tf.Session();
- o = sess.run(b);
- Assert.IsTrue(np.array_equal(o, check));
- }
-
- [TestMethod]
- public void logicalOpsTest()
- {
- var a = tf.constant(new[] { 1f, 2f, 3f, 4f, -4f, -3f, -2f, -1f });
- var b = tf.less(a, 0f);
- var c = tf.greater(a, 0f);
- var d = tf.cast(tf.logical_and(b, c), tf.int32);
- var check = np.array(new[] { 0, 0, 0, 0, 0, 0, 0, 0 });
-
- var sess = tf.Session();
- var o = sess.run(d);
- Assert.IsTrue(np.array_equal(o, check));
-
- d = tf.cast(tf.logical_not(b), tf.int32);
- check = np.array(new[] { 1, 1, 1, 1, 0, 0, 0, 0 });
-
- sess = tf.Session();
- o = sess.run(d);
- Assert.IsTrue(np.array_equal(o, check));
-
- d = tf.cast(tf.logical_or(b, c), tf.int32);
- check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
-
- sess = tf.Session();
- o = sess.run(d);
- Assert.IsTrue(np.array_equal(o, check));
-
- d = tf.cast(tf.logical_xor(b, c), tf.int32);
- check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
-
- sess = tf.Session();
- o = sess.run(d);
- Assert.IsTrue(np.array_equal(o, check));
- }
-
- [TestMethod]
- public void addOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int firstIntVal = 2;
- const int secondIntVal = 3;
-
- var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
- var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, intResult);
-
- // Testing `operator +(Tensor x, Tensor y)`
- c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, intResult);
-
- // Testing `operator +(Tensor x, int y)`
- c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, intResult);
-
- // Testing `operator +(int x, Tensor y)`
- c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, intResult);
- #endregion
-
- #region floatTest
- const float firstFloatVal = 2.0f;
- const float secondFloatVal = 3.0f;
-
- var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
- var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
- var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, floatResult);
-
- // Testing `operator +(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, floatResult);
-
- // Testing `operator +(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, floatResult);
-
- // Testing `operator +(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, floatResult);
- #endregion
-
- #region doubleTest
- const double firstDoubleVal = 2.0;
- const double secondDoubleVal = 3.0;
-
- var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
- var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator +(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, doubleResult);
-
- // Testing `operator +(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(a + secondDoubleVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, doubleResult);
-
- // Testing `operator +(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(secondDoubleVal + a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual(o, doubleResult);
- #endregion
- }
-
- [TestMethod]
- public void subOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int firstIntVal = -2;
- const int secondIntVal = 3;
-
- var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
- var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
- var intResultTwo = -firstIntFeed.Sum();
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator -(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator -(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator -(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, Math.Abs(intResult));
-
- // Testing `operator -(Tensor x)
- c = tf.reduce_sum(tf.reduce_sum(-a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResultTwo);
- #endregion
-
- #region floatTest
- const float firstFloatVal = -2.0f;
- const float secondFloatVal = 3.0f;
-
- var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
- var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
- var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
- var floatResultTwo = -firstFloatFeed.Sum();
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator -(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator -(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator -(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, Math.Abs(floatResult));
-
- // Testing `operator -(Tensor x)
- c = tf.reduce_sum(tf.reduce_sum(-a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResultTwo);
- #endregion
-
- #region doubleTest
- const double firstDoubleVal = -2.0;
- const double secondDoubleVal = 3.0;
-
- var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
- var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
- var doubleResultTwo = -firstDoubleFeed.Sum();
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator -(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator -(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(a - secondDoubleVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator -(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(secondDoubleVal - a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, Math.Abs(doubleResult));
-
- // Testing `operator -(Tensor x)
- c = tf.reduce_sum(tf.reduce_sum(-a, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResultTwo);
- #endregion
- }
-
- private IEnumerable<int> MultiplyArray(IReadOnlyCollection<int> first, IReadOnlyCollection<int> second)
- {
- if (first.Count != second.Count)
- throw new ArgumentException("Arrays should be of equal size!");
-
- var firstEnumerator = first.GetEnumerator();
- var secondEnumerator = second.GetEnumerator();
- var result = new List<int>();
- while (firstEnumerator.MoveNext())
- {
- secondEnumerator.MoveNext();
- result.Add(firstEnumerator.Current * secondEnumerator.Current);
- }
-
- firstEnumerator.Dispose();
- secondEnumerator.Dispose();
-
- return result;
- }
- private IEnumerable<float> MultiplyArray(IReadOnlyCollection<float> first, IReadOnlyCollection<float> second)
- {
- if (first.Count != second.Count)
- throw new ArgumentException("Arrays should be of equal size!");
-
- var firstEnumerator = first.GetEnumerator();
- var secondEnumerator = second.GetEnumerator();
- var result = new List<float>();
- while (firstEnumerator.MoveNext())
- {
- secondEnumerator.MoveNext();
- result.Add(firstEnumerator.Current * secondEnumerator.Current);
- }
-
- firstEnumerator.Dispose();
- secondEnumerator.Dispose();
-
- return result;
- }
- private IEnumerable<double> MultiplyArray(IReadOnlyCollection<double> first, IReadOnlyCollection<double> second)
- {
- if (first.Count != second.Count)
- throw new ArgumentException("Arrays should be of equal size!");
-
- var firstEnumerator = first.GetEnumerator();
- var secondEnumerator = second.GetEnumerator();
- var result = new List<double>();
- while (firstEnumerator.MoveNext())
- {
- secondEnumerator.MoveNext();
- result.Add(firstEnumerator.Current * secondEnumerator.Current);
- }
-
- firstEnumerator.Dispose();
- secondEnumerator.Dispose();
-
- return result;
- }
-
- [TestMethod]
- public void mulOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int firstIntVal = 2;
- const int secondIntVal = 3;
-
- var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
- var intResult = MultiplyArray(firstIntFeed, secondIntFeed).Sum();
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator *(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator *(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator *(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(firstIntVal * b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
- #endregion
-
- #region floatTest
- const float firstFloatVal = 2.0f;
- const float secondFloatVal = 3.0f;
-
- var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
- var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
- var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed).Sum();
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator *(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator *(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator *(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
- #endregion
-
- #region doubleTest
- const double firstDoubleVal = 2.0;
- const double secondDoubleVal = 3.0;
-
- var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
- var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed).Sum();
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator *(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator *(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(a * secondDoubleVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator *(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(firstDoubleVal * b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
- #endregion
- }
-
- [Ignore]
- [TestMethod]
- public void divOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int firstIntVal = 6;
- const int secondIntVal = 3;
-
- var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
- var intResult = (int)(firstIntFeed.Sum() / (float)secondIntVal);
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(gen_math_ops.floor_div(a, b), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator /(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator /(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(a / secondIntVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator /(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(firstIntVal / b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
- #endregion
-
- #region floatTest
- const float firstFloatVal = 6.0f;
- const float secondFloatVal = 3.0f;
-
- var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
- var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
- var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed.Select(x => 1 / x).ToArray()).Sum();
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator /(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator /(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
-
- // Testing `operator /(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((float)o, floatResult);
- #endregion
-
- #region doubleTest
- const double firstDoubleVal = 6.0;
- const double secondDoubleVal = 3.0;
-
- var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
- var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed.Select(x => 1 / x).ToArray()).Sum();
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator /(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator /(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
-
- // Testing `operator /(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)o, doubleResult);
- #endregion
- }
-
- [TestMethod]
- public void greaterThanOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int intThreshold = 10;
-
- var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
- var intResult = firstIntFeed.Count(elem => elem > intThreshold);
- var intResultTwo = firstIntFeed.Count(elem => elem < intThreshold);
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator >(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator >(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > intThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator >(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold > a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResultTwo);
- #endregion
-
- #region floatTest
- const float floatThreshold = 10.0f;
-
- var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
- var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
- var floatResult = firstFloatFeed.Count(elem => elem > floatThreshold);
- var floatResultTwo = firstFloatFeed.Count(elem => elem < floatThreshold);
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator >(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator >(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > floatThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator >(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold > a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResultTwo);
- #endregion
-
- #region doubleTest
- const double doubleThreshold = 10.0;
-
- var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
- var doubleResult = firstDoubleFeed.Count(elem => elem > doubleThreshold);
- var doubleResultTwo = firstDoubleFeed.Count(elem => elem < doubleThreshold);
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator >(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator >(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > doubleThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator >(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold > a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResultTwo);
- #endregion
- }
-
- [TestMethod]
- public void lessThanOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int intThreshold = 10;
-
- var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
- var intResult = firstIntFeed.Count(elem => elem < intThreshold);
- var intResultTwo = firstIntFeed.Count(elem => elem > intThreshold);
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator <(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator <(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < intThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator <(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold < a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResultTwo);
- #endregion
-
- #region floatTest
- const float floatThreshold = 10.0f;
-
- var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
- var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
- var floatResult = firstFloatFeed.Count(elem => elem < floatThreshold);
- var floatResultTwo = firstFloatFeed.Count(elem => elem > floatThreshold);
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator <(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator <(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < floatThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator <(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold < a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResultTwo);
- #endregion
-
- #region doubleTest
- const double doubleThreshold = 10.0;
-
- var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
- var doubleResult = firstDoubleFeed.Count(elem => elem < doubleThreshold);
- var doubleResultTwo = firstDoubleFeed.Count(elem => elem > doubleThreshold);
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator <(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator <(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < doubleThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator <(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold < a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResultTwo);
- #endregion
- }
-
- [TestMethod]
- public void greaterOrEqualThanOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int intThreshold = 10;
-
- var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
- var intResult = firstIntFeed.Count(elem => elem >= intThreshold);
- var intResultTwo = firstIntFeed.Count(elem => elem <= intThreshold);
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator >=(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator >=(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= intThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator >=(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold >= a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResultTwo);
- #endregion
-
- #region floatTest
- const float floatThreshold = 10.0f;
-
- var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
- var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
- var floatResult = firstFloatFeed.Count(elem => elem >= floatThreshold);
- var floatResultTwo = firstFloatFeed.Count(elem => elem <= floatThreshold);
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator >=(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator >=(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= floatThreshold, tf.int32), 1));
- sess = tf.Session();
- sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator >=(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold >= a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResultTwo);
- #endregion
-
- #region doubleTest
- const double doubleThreshold = 10.0;
-
- var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
- var doubleResult = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
- var doubleResultTwo = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator >=(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator >=(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= doubleThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator >=(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold >= a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResultTwo);
- #endregion
- }
-
- [TestMethod]
- public void lessOrEqualThanOpTests()
- {
- const int rows = 2; // to avoid broadcasting effect
- const int cols = 10;
-
- #region intTest
- const int intThreshold = 10;
-
- var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
- var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
- var intResult = firstIntFeed.Count(elem => elem <= intThreshold);
- var intResultTwo = firstIntFeed.Count(elem => elem >= intThreshold);
-
- var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
-
- var sess = tf.Session();
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator <=(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator <=(Tensor x, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= intThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResult);
-
- // Testing `operator <=(int x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold <= a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, intResultTwo);
- #endregion
-
- #region floatTest
- const float floatThreshold = 10.0f;
-
- var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
- var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
- var floatResult = firstFloatFeed.Count(elem => elem <= floatThreshold);
- var floatResultTwo = firstFloatFeed.Count(elem => elem >= floatThreshold);
-
- a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator <=(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator <=(Tensor x, float y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= floatThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResult);
-
- // Testing `operator <=(float x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold <= a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, floatResultTwo);
- #endregion
-
- #region doubleTest
- const double doubleThreshold = 10.0;
-
- var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
- var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
- var doubleResult = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
- var doubleResultTwo = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
-
- a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
-
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator <=(Tensor x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
- new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator <=(Tensor x, double y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= doubleThreshold, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResult);
-
- // Testing `operator <=(double x, Tensor y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold <= a, tf.int32), 1));
- sess = tf.Session();
- o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((int)o, doubleResultTwo);
- #endregion
- }
-
- [Ignore("Not finished yet")]
- [TestMethod]
- public void map_fn()
- {
- var a = tf.constant(new[] { 1, 2, 3, 4 });
- var b = tf.constant(new[] { 17, 12, 11, 10 });
- var ab = tf.stack(new[] { a, b }, 1);
-
- Func<Tensor, Tensor> map_operation = (value_ab) =>
- {
- var value_a = value_ab[0];
- var value_b = value_ab[1];
- return value_a + value_b;
- };
-
- var map_result = tf.map_fn(map_operation, ab);
- }
- }
- }
|