一个很简单的例子,使用c#调用tensorflow。
首先需要安装windows 版本的tensowflow,使用64位的python3.5,如果没有安装,需要首先安装python3.5
然后以管理员方式进入命令行,运行
pip install tensorflow
StarCoreFactory starcore = StarCoreFactory.GetFactory();
StarServiceClass Service = starcore._InitSimple("test", "123", 0, 0, null);
StarSrvGroupClass SrvGroup = (StarSrvGroupClass)Service._Get("_ServiceGroup");
//--init python raw interface
SrvGroup._InitRaw("python35", Service);
StarObjectClass python = Service._ImportRawContext("python", "", false, "");
//--import tensorflow as tf
python._Call("eval", "import tensorflow as tf");
StarObjectClass tf = python._GetObject("tf");
Console.WriteLine(tf);
//-- a = tf.add(2,5)
StarObjectClass a = (StarObjectClass)tf._Call("add", 2, 5);
//-- b = tf.multiply(a,5)
StarObjectClass b = (StarObjectClass)tf._Call("multiply", a, 3);
//-- c = tf.constant(2,name="Node_c")
StarObjectClass c = (StarObjectClass)tf._Call("constant", 2, SrvGroup._NewParaPkg("name", "Node_c")._AsDict(true));
Console.WriteLine(c);
//-- sess = tf.Session()
StarObjectClass Session = (StarObjectClass)tf._Get("Session");
StarObjectClass sess = Session._New();
//-- result = sess.run(b,feed_dict={a:25});
StarParaPkgClass pkg = SrvGroup._NewParaPkg(a, 25)._AsDict(true);
Object result = sess._Call("run", b, SrvGroup._NewParaPkg("feed_dict", pkg)._AsDict(true));
Console.WriteLine(result);
//--import tensorflow as tf
python.eval("import tensorflow as tf");
dynamic tf = python.tf;
//-- a = tf.add(2,5)
dynamic a = tf.add(2, 5);
//-- b = tf.multiply(a,5)
dynamic b = tf.multiply(a, 3);
//-- c = tf.constant(2,name="Node_c")
dynamic c = tf.constant(2, SrvGroup._NewParaPkg("name", "Node_c")._AsDict(true));
//-- sess = tf.Session()
dynamic Session = tf.Session;
dynamic sess = Session._New();
//-- result = sess.run(b,feed_dict={a:25});
var pkg = SrvGroup._NewParaPkg(a, 25)._AsDict(true);
dynamic result = sess.run(b, SrvGroup._NewParaPkg("feed_dict", pkg)._AsDict(true));
Console.WriteLine(result);