|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using NumSharp;
- using Tensorflow;
- using Tensorflow.Util;
- using Buffer = Tensorflow.Buffer;
- using static Tensorflow.Binding;
- using Tensorflow.UnitTest;
-
- 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();
- using var buffer = new Buffer(handle);
- var op_list = OpList.Parser.ParseFrom(buffer.DangerousMemoryBlock.Stream());
-
- 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);
-
- using(var sess = tf.Session())
- {
- var o = sess.run(c,
- new FeedItem(a, 3.0f),
- new FeedItem(b, 2.0f));
- Assert.AreEqual((float)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);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(c);
- Assert.AreEqual((float)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);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(b);
- Assert.IsTrue(o.array_equal(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);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(b);
- Assert.IsTrue(o.array_equal(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);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(b);
- Assert.IsTrue(o.array_equal(check));
- }
-
- b = tf.cumsum(a, exclusive: true);
- check = np.array(0, 1, 2, 4, 7, 11);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(b);
- Assert.IsTrue(o.array_equal(check));
- }
-
- b = tf.cumsum(a, reverse: true);
- check = np.array(16, 15, 14, 12, 9, 5);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(b);
- Assert.IsTrue(o.array_equal(check));
- }
-
- b = tf.cumsum(a, exclusive:true, reverse: true);
- check = np.array(15, 14, 12, 9, 5, 0);
-
- using (var sess = tf.Session())
- {
- var o = sess.run(b);
- Assert.IsTrue(o.array_equal(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 });
-
- using (var sess = tf.Session())
- {
- var o = sess.run(d);
- Assert.IsTrue(o.array_equal(check));
- }
-
- d = tf.cast(tf.logical_not(b), tf.int32);
- check = np.array(new[] { 1, 1, 1, 1, 0, 0, 0, 0 });
-
- using (var sess = tf.Session())
- {
- var o = sess.run(d);
- Assert.IsTrue(o.array_equal(check));
- }
-
- d = tf.cast(tf.logical_or(b, c), tf.int32);
- check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
-
- using (var sess = tf.Session())
- {
- var o = sess.run(d);
- Assert.IsTrue(o.array_equal(check));
- }
-
- d = tf.cast(tf.logical_xor(b, c), tf.int32);
- check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
-
- using (var sess = tf.Session())
- {
- var o = sess.run(d);
- Assert.IsTrue(o.array_equal(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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
-
- using (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));
- using (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, int y)`
- c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstIntFeed, 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 = firstFloatFeed.Sum() + secondFloatFeed.Sum();
-
- a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstFloatFeed, 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 = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
-
- a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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(secondFloatVal + a, 1));
- using (var sess = tf.Session())
- {
- var o = sess.run(c,
- new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
- Assert.AreEqual((double)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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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(secondFloatVal - a, 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(gen_math_ops.floor_div(a, b), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(a / secondIntVal, 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > intThreshold, tf.int32), 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < intThreshold, tf.int32), 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= intThreshold, tf.int32), 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
- var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
-
- using (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));
- using (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, int y)
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= intThreshold, tf.int32), 1));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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 TensorShape(rows, cols));
- b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
- c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
-
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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));
- using (var sess = tf.Session())
- {
- var 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);
- }
- }
- }
|