0

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'

Did I install a wrong version of python? I installed python 3.7. Or perhaps I did not install tensorflow correctly. The book tensorflow for dummies was published in 2018. Maybe it was intended for another version of python? and/or another version of tensorflow? thank you so much in advance

Here is the code of the "simple.math.py" :

''' 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



After running the following program on windows with latest tensorflow 
installed, I receive the message underneath the python code below:

''' A simple TensorFlow application '''

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

# Create tensor
msg = tf.strings.join(['Hello ', 'TensorFlow!'])

# Launch session
with tf.Session() as sess:
    print(sess.run(msg))



(venv) C:\Users\ME\TENSORFLOW\ch2>python hello_tensorflow.py
2020-04-29 00:28:30.676246: 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-29 00:28:30.684568: 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-29 00:28:32.573201: W 
tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not 
load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
    2020-04-29 00:28:32.580400: E 
tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: 
UNKNOWN ERROR (303)
    2020-04-29 00:28:32.591791: I 
tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA 
diagnostic information for host: DESKTOP-C7DH370
    2020-04-29 00:28:32.599756: I 
tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: 
DESKTOP- 
C7DH370
    2020-04-29 00:28:32.615405: I 
tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1e0678689e0 
initialized for platform Host (this does not guarantee that XLA will be 
used). 
Devices:
    2020-04-29 00:28:32.623598: I 
tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device 
(0): 
Host, Default Version
    Traceback (most recent call last):
      File "hello_tensorflow.py", line 13, in <module>
        with tf.Session() as sess:
    AttributeError: module 'tensorflow' has no attribute 'Session'
15
  • Hello Steven. Please post your code as we will not go to the book reference and find your program. Commented Apr 26, 2020 at 20:26
  • Can you add your tensorflow version and python version. I ran your code with no errors. Commented Apr 26, 2020 at 22:43
  • python 3.6.4 and tensorflow was installed with the command "pip3 install tensorflow" I'm not sure I am installing it correctly. Do I change directories in the command prompt to where python is installed and then enter "pip3 install tensorflow" ? Thank you Commented Apr 26, 2020 at 23:23
  • Changing the directories will not matter. Check my answer and please comment if you have a question. otherwise if it helped you mark it as accepted so you can help others. Commented Apr 26, 2020 at 23:42
  • I am on the official tensorflow site and I am following the directions. I downloaded pyhon 3.8 and checked all boxes, I installed Microsoft Visual C++. long paths are enabled When I come up to the step of entering "pip3 install -U pip virtualenv" on the command line, I receive the folowing error: ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'c:\\program files\\python38\\lib\\site-packages\\pip-19.2.3.dist-info\\entry_points.txt' Consider using the --user option or check the permissions. Commented Apr 29, 2020 at 0:10

1 Answer 1

0

Tensorflow 2.x working sample code

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.divide(const_a, const_b)
print(quot)

# 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)
print(diff)

# 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)
print(dot)

# 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)
print(mat_prod)

Output

Tensor("truediv_3:0", shape=(), dtype=float32)
Tensor("Sub_4:0", shape=(3,), dtype=float32)
Tensor("Tensordot_4:0", shape=(1,), dtype=float32)
Tensor("MatMul_4:0", shape=(3, 3), dtype=int32)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.