python code with tensorflow does not work


  • How can Tensorflow have no "div" attribute? When I try to run a python program from the command prompt (from tensorflow for dummies page 23 (simple program) I receive this error:

    C:\Users\ME\TENSORFLOW\ch3>python simple_math.py
    2020-04-26 13:50:29.871436: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
    2020-04-26 13:50:29.880652: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
    2020-04-26 13:50:32.204590: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
    2020-04-26 13:50:32.212936: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
    2020-04-26 13:50:32.228615: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-C7DH370
    2020-04-26 13:50:32.236344: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-C7DH370
    Traceback (most recent call last):
      File "simple_math.py", line 13, in <module>
        quot = tf.div(const_a, const_b)
    AttributeError: module 'tensorflow' has no attribute 'div'
    

    thank you so much in advance



  • @Tony-Cardinal said in python code with tensorflow does not work:

    module 'tensorflow' has no attribute 'div'

    This might be a Tensorflow 2.0 versus 1.0 issue? I don't program in python but I found the following stackoverflow answer that might help:

    https://stackoverflow.com/questions/55142951/tensorflow-2-0-attributeerror-module-tensorflow-has-no-attribute-session

    If I am right you can do something like:

     tf.compat.v1.div(x,y)
    

    or

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    


  • Hey @Tony-Cardinal I think you forgot to share the actual source file simple_math.py. Please share that so we have a better chance of answering your question.



  • ''' Perform simple math operations with TensorFlow '''

    from future import absolute_import
    from future import division
    from future import print_function

    import tensorflow as tf

    Math with constant tensors

    const_a = tf.constant(3.6)
    const_b = tf.constant(1.2)
    total = const_a + const_b
    quot = tf.div(const_a, const_b)

    Math with random tensors

    rand_a = tf.random_normal([3], 2.0)
    rand_b = tf.random_uniform([3], 1.0, 4.0)
    diff = tf.subtract(rand_a, rand_b)

    Vector multiplication

    vec_a = tf.linspace(0.0, 3.0, 4)
    vec_b = tf.fill([4, 1], 2.0)
    prod = tf.multiply(vec_a, vec_b)
    dot = tf.tensordot(vec_a, vec_b, 1)

    Matrix multiplication

    mat_a = tf.constant([[2, 3], [1, 2], [4, 5]])
    mat_b = tf.constant([[6, 4, 1], [3, 7, 2]])
    mat_prod = tf.matmul(mat_a, mat_b)

    Execute the operations

    with tf.Session() as sess:
    print('Sum: %f' % sess.run(total))
    print('Quotient: %f' % sess.run(quot))
    print('Difference: ', sess.run(diff))
    print('Element-wise product: ', sess.run(prod))
    print('Dot product: ', sess.run(dot))
    print('Matrix product: ', sess.run(mat_prod))

    Thank you



  • @Tony-Cardinal said in python code with tensorflow does not work:

    module 'tensorflow' has no attribute 'div'

    This might be a Tensorflow 2.0 versus 1.0 issue? I don't program in python but I found the following stackoverflow answer that might help:

    https://stackoverflow.com/questions/55142951/tensorflow-2-0-attributeerror-module-tensorflow-has-no-attribute-session

    If I am right you can do something like:

     tf.compat.v1.div(x,y)
    

    or

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()