@@ -236,13 +236,100 @@ public static void p (string p)
236236 Console . WriteLine ( p ) ;
237237 }
238238
239+ void BasicConstantOps ( )
240+ {
241+ //
242+ // Test the manual GetRunner, this could be simpler
243+ // we should at some point allow Run (a+b);
244+ //
245+ // The session implicitly creates the graph, get it.
246+ using ( var s = new TFSession ( ) ) {
247+ var g = s . Graph ;
248+
249+ var a = g . Const ( 2 ) ;
250+ var b = g . Const ( 3 ) ;
251+ Console . WriteLine ( "a=2 b=3" ) ;
252+
253+ // Add two constants
254+ var results = s . GetRunner ( ) . Run ( g . Add ( a , b ) ) ;
255+ var val = results [ 0 ] . GetValue ( ) ;
256+ Console . WriteLine ( "a+b={0}" , val ) ;
257+
258+ // Multiply two constants
259+ results = s . GetRunner ( ) . Run ( g . Mul ( a , b ) ) ;
260+ Console . WriteLine ( "a*b={0}" , results [ 0 ] . GetValue ( ) ) ;
261+
262+ // TODO: API-wise, perhaps session.Run () can have a simple
263+ // overload where we only care about the fetched values,
264+ // making the above:
265+ // s.Run (g.Mul (a, b));
266+ }
267+ }
268+
269+ //
270+ // Shows how to use placeholders to pass values
271+ //
272+ void BasicVariables ( )
273+ {
274+ Console . WriteLine ( "Using placerholders" ) ;
275+ using ( var g = new TFGraph ( ) ) {
276+ var s = new TFSession ( g ) ;
277+
278+ // We use "shorts" here, so notice the casting to short to get the
279+ // tensor with the right data type.
280+ var var_a = g . Placeholder ( TFDataType . Int16 ) ;
281+ var var_b = g . Placeholder ( TFDataType . Int16 ) ;
282+
283+ var add = g . Add ( var_a , var_b ) ;
284+ var mul = g . Mul ( var_a , var_b ) ;
285+
286+ var runner = s . GetRunner ( ) ;
287+ runner . AddInput ( var_a , new TFTensor ( ( short ) 3 ) ) ;
288+ runner . AddInput ( var_b , new TFTensor ( ( short ) 2 ) ) ;
289+ Console . WriteLine ( "a+b={0}" , runner . Run ( add ) [ 0 ] . GetValue ( ) ) ;
290+
291+ runner = s . GetRunner ( ) ;
292+ runner . AddInput ( var_a , new TFTensor ( ( short ) 3 ) ) ;
293+ runner . AddInput ( var_b , new TFTensor ( ( short ) 2 ) ) ;
294+
295+ Console . WriteLine ( "a*b={0}" , runner . Run ( mul ) [ 0 ] . GetValue ( ) ) ;
296+
297+ // TODO
298+ // Would be nice to have an API that allows me to pass the values at Run time, easily:
299+ // s.Run (add, { var_a: 3, var_b: 2 })
300+ // C# allows something with Dictionary constructors, but you still must provide the type
301+ // signature.
302+ }
303+ }
304+
305+ void BasicMatrix ( )
306+ {
307+ Console . WriteLine ( "Basic matrix" ) ;
308+ using ( var g = new TFGraph ( ) ) {
309+ var s = new TFSession ( g ) ;
310+
311+ // 1x2 matrix
312+ var matrix1 = g . Const ( new double [ , ] { { 3 , 3 } } ) ;
313+ // 2x1 matrix
314+ var matrix2 = g . Const ( new double [ , ] { { 2 } , { 2 } } ) ;
315+
316+ // multiply
317+ var product = g . MatMul ( matrix1 , matrix2 ) ;
318+
319+
320+ var result = s . GetRunner ( ) . Run ( product ) [ 0 ] ;
321+ Console . WriteLine ( "Tensor ToString=" + result ) ;
322+ Console . WriteLine ( "Value [0,0]=" + ( ( double [ , ] ) result . GetValue ( ) ) [ 0 , 0 ] ) ;
323+
324+ } ;
325+ }
239326
240327 public static void Main ( string [ ] args )
241328 {
242329 Console . WriteLine ( Environment . CurrentDirectory ) ;
243330 Console . WriteLine ( "TensorFlow version: " + TFCore . Version ) ;
244331
245- var b = TFCore . GetAllOpList ( ) ;
332+ // var b = TFCore.GetAllOpList ();
246333
247334
248335 var t = new MainClass ( ) ;
@@ -256,8 +343,11 @@ public static void Main (string [] args)
256343
257344
258345 //var n = new Mnist ();
259- //n.ReadDataSets ("/Users/miguel/Downloads", numClasses: numClasses );
346+ //n.ReadDataSets ("/Users/miguel/Downloads", numClasses: 10 );
260347
348+ t . BasicConstantOps ( ) ;
349+ t . BasicVariables ( ) ;
350+ t . BasicMatrix ( ) ;
261351 }
262352 }
263353}
0 commit comments