Skip to content

Using Tensorflow with pythonnet #473

Closed

Description

@mphielipp

Environment

  • Pythonnet version: 2.3.0
  • Python version:3.5.2 (Anaconda)
  • Operating System:Windows 10

Details

                                dynamic tf = Py.Import("tensorflow");
                               dynamic data = np.ones(5);
                                dynamic x = tf.constant(data, Py.kw("name", 'x'));

THIS LINE>> dynamic y = tf.Variable((x * x) * 5 - 3 * x + 15, Py.kw("name", 'y'));

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

Additional information: Operator '*' cannot be applied to operands of type 'int' and 'Python.Runtime.PyObject'

Activity

  1. den-run-ai commented on May 25, 2017

    @den-run-ai
    Contributor

    @mphielipp i think this is the same as this issue, which is limitation of .NET DLR:

    #86

    You need to cast dynamic to corresponding type (e.g. double) when dynamic is NOT 1st argument:

    #249 (comment)

  2. mphielipp commented on May 25, 2017

    @mphielipp
    Author

    I try different castings (dynamic, int, double) but does not work:

    dynamic y = tf.Variable( (cast)3 * x ), Py.kw("name", 'y'));

    An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in PythonNet2.exe

    Additional information: Operator '*' cannot be applied to operands of type 'int' and 'Python.Runtime.PyObject'

  3. den-run-ai commented on May 25, 2017

    @den-run-ai
    Contributor

    Here is how casting works:

    dynamic y = tf.Variable( 3 * ((int)x) ), Py.kw("name", 'y'));

    I put extra brackets to disambiguate the operation ordering.

  4. mphielipp commented on May 25, 2017

    @mphielipp
    Author

    I typed:
    dynamic y = tf.Variable((3 * ((int)x)), Py.kw("name", 'y'));
    also tried
    dynamic y = tf.Variable(3 * ((int)x), Py.kw("name", 'y'));

    same error: (An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in ...Additional information: Cannot convert type 'Python.Runtime.PyObject' to 'int')

  5. mphielipp commented on May 25, 2017

    @mphielipp
    Author

    wait x is not an int, x was declare in the previous line:
    dynamic x = tf.constant(data, Py.kw("name", 'x'));

  6. den-run-ai commented on May 25, 2017

    @den-run-ai
    Contributor
  7. den-run-ai commented on May 26, 2017

    @den-run-ai
    Contributor

    I don't have much experience with tensorflow. Can you point to any tutorial that does the same in Python API?

    Can't you build your expressions only using tensorflow math functions, not Python operators?

    https://www.tensorflow.org/api_guides/python/math_ops

  8. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    Yes. This is a sample code:
    import tensorflow as tf
    import numpy as np

    data = np.random.randint(1000, size=10000)

    print (data)

    x = tf.constant(data, name='x')
    y = tf.Variable((x * x) * 5-3*x+15, name='y')

    model = tf.global_variables_initializer()
    with tf.Session() as session:
    session.run(model)
    print(session.run(y))

  9. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    it seems that the operators are pass as Python ones in this example.

  10. den-run-ai commented on May 26, 2017

    @den-run-ai
    Contributor

    looks like possible using only tensorflow math:

    activate py35
    
    (py35) C:\Users\denis.akhiyarov>conda update python
    (py35) C:\Users\denis.akhiyarov>pip install tensorflow
    (py35) C:\Users\denis.akhiyarov>python
    Python 3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v
    .1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tensorflow
    >>> import tensorflow as tf
    >>> import numpy as np
    >>> data = np.ones(5)
    >>> x=tf.constant(data, name='x')
    >>> y = tf.Variable((x * x) * 5 - 3 * x + 15, name='y')
    >>> z = tf.Variable(tf.multiply(tf.pow(x,2),5), name='z')
    >>> exit()
    
    (py35) C:\Users\denis.akhiyarov>pip install pythonnet -U
    (py35) C:\Users\denis.akhiyarov>set PYTHONHOME=C:\Python\Python27_64b\envs\py35
    (py35) C:\Users\denis.akhiyarov>scriptcs
    scriptcs (ctrl-c to exit or :help for help)
    
    > #r C:\Python\Python27_64b\envs\py35\lib\site-packages\Python.Runtime.dll
    > using Python.Runtime;
    > dynamic sys;
    > using (Py.GIL()) {sys=Py.Import("sys");}
    > dynamic np;
    > using (Py.GIL()) {np=Py.Import("numpy");}
    > dynamic tf;
    > using (Py.GIL()) {tf=Py.Import("tensorflow");}
    > dynamic data = np.ones(5);
    > dynamic x=tf.constant(data, Py.kw("name","x"));
    > dynamic z = tf.Variable(tf.multiply(tf.pow(x,2),5),  Py.kw("name","z"));
    
  11. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    ok. thanks!!!

  12. mphielipp commented on May 26, 2017

    @mphielipp
    Author
                dynamic data = np.ones(5.0);
                dynamic x = tf.constant(data, Py.kw("name", 'x'));
                dynamic y = tf.Variable(tf.multiply(tf.constant(3) ,x), Py.kw("name", 'y'));
    

    TypeError : Input 'y' of 'Mul' Op has type float64 that does not match type int32 of argument 'x'."}
    x is type ndarray and each element if float 64

  13. den-run-ai commented on May 26, 2017

    @den-run-ai
    Contributor

    well i guess you need to specify the types:

    dynamic y = tf.Variable(tf.multiply(tf.constant(3.0,Py.kw("dtype","float64")),x), Py.kw("name", 'y'));
    
  14. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    Yes!! Thanks!!

  15. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    I tried the specification but did not change the 3 to 3.0. Thanks again!

  16. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    I did run:
    dynamic x = tf.constant(data,Py.kw("dtype","float64"), Py.kw("name", 'x'));
    dynamic y = tf.Variable(tf.multiply(tf.constant(3.0, Py.kw("dtype", "float64")), x), Py.kw("name", 'y'));
    dynamic model = tf.global_variables_initializer();
    dynamic res = tf.Session().run(model);
    Console.WriteLine(tf.Session().run(y));

    But it does not initialize properly:

    {"FailedPreconditionError : Attempting to use uninitialized value y\n\t [[Node: _send_y_0 = _SendT=DT_DOUBLE, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=3877403296068384333, tensor_name="y:0", _device="/job:localhost/replica:0/task:0/cpu:0"]]"}

  17. den-run-ai commented on May 26, 2017

    @den-run-ai
    Contributor
    dynamic ssn=tf.Session();
    ssn.run(model);
    ssn.run(y);
    
  18. mphielipp commented on May 26, 2017

    @mphielipp
    Author

    Great!! Thanks!!

  19. mphielipp commented on May 26, 2017

    @mphielipp
    Author
                // w = tf.Variable([1.0, 2.0], name = "w")
                dynamic wa = np.array(new List<double> { 1.0, 2.0});
                dynamic w = tf.Variable(wa, Py.kw("dtype", "float64"), Py.kw("name", 'w'));
    

    {"TypeError : float() argument must be a string or a number, not '0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'"}

  20. den-run-ai commented on May 26, 2017

    @den-run-ai
    Contributor

    But can you do the same in Python? Try skipping the dtype argument or also providing the shape argument. In this case TF may be able to resolve dtype and shape based on value.

  21. mphielipp commented on May 28, 2017

    @mphielipp
    Author
  22. changed the title [-]An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll[/-] [+]Using Tensorflow with pythonnet[/+] on Jun 26, 2017
  23. den-run-ai commented on Sep 14, 2017

    @den-run-ai
    Contributor

    @mphielipp here is the full code sample, I hope you don't mind that I used it in one presentation:

    // RUN THIS IN SCRIPTCS
    #r C:\Python\Python35_64b\lib\site-packages\Python.Runtime.dll
    using Python.Runtime;
    dynamic sys; using (Py.GIL()) {sys=Py.Import("sys");}
    dynamic np; using (Py.GIL()) {np=Py.Import("numpy");}
    dynamic tf; using (Py.GIL()) {tf=Py.Import("tensorflow");}
    dynamic data = np.ones(5);
    dynamic x=tf.constant(data, Py.kw("name","x"));
    dynamic z = tf.Variable(tf.multiply(tf.pow(x,2),5),  Py.kw("name","z"));
    dynamic y = tf.Variable(tf.multiply(tf.constant(3.0,Py.kw("dtype","float64")),x), Py.kw("name", 'y'));
    dynamic model = tf.global_variables_initializer();
    dynamic ssn=tf.Session(); ssn.run(model); ssn.run(y).ToString();
  24. danieljaro commented on Sep 27, 2018

    @danieljaro

    @denfromufa
    Because you were very active in the past, I adress you directly. Maybe you can help me. Thanks a lot in advance.

    I have installed "python3.6" over anaconda3 and added "tensorflow" with the published cmd operations.
    It works fine and it is possible to run the application (for instance, var tf=Py.Import("tensorflow"); ...)
    But I get a failure b< one of you last statements in the above example.

    2018-09-27 13:36:18.984929: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

    Do you have any idea?

  25. den-run-ai commented on Sep 27, 2018

    @den-run-ai
    Contributor

    That is a warning, not an error

  26. danieljaro commented on Sep 27, 2018

    @danieljaro

    ok! thank's for the answer.

  27. danieljaro commented on Sep 28, 2018

    @danieljaro

    @denfromufa
    I am searching in the internet for an example, that uses keras over pythonnet. You wrote in an other thread a year ago, that it is possible? I have no luck by finding a syntax in c# for this. Do you have any sample code? Best regards, Daniel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions