Search code examples
pythontensorflowtensorflow2.0

TensorFlow2: placeholder only takes keyword args


Hi I am currently trying to learn some basic machine learning with Tensorflow and I am trying to use the placeholder function. I cannot for the life of me figure out what it means that it only takes keyword args and searches around the internet have not been much help. I have the code and error below. Would someone be able to show me an example of how to correctly this function and explain it to me like I am five? Thanks!

code:

x= tf.raw_ops.Placeholder( tf.constant(n_input, dtype=tf.float64))
y = tf.raw_ops.Placeholder( tf.double(0))

Stack Trace:

2020-12-11 14:31:38.333136: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-12-11 14:31:38.333255: 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.
Loaded training data...
2020-12-11 14:31:39.708405: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library nvcuda.dll
2020-12-11 14:31:39.724685: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1716] Found device 0 with properties: 
pciBusID: 0000:26:00.0 name: GeForce GTX 1050 Ti computeCapability: 6.1
coreClock: 1.43GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2020-12-11 14:31:39.725427: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2020-12-11 14:31:39.725912: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
2020-12-11 14:31:39.726384: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2020-12-11 14:31:39.726849: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2020-12-11 14:31:39.727368: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
2020-12-11 14:31:39.727821: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
2020-12-11 14:31:39.728328: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2020-12-11 14:31:39.728410: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1753] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2020-12-11 14:31:39.729060: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-12-11 14:31:39.736101: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2b6623f81b0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-12-11 14:31:39.736250: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2020-12-11 14:31:39.736436: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1257] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-12-11 14:31:39.736549: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1263]      
Traceback (most recent call last):
  File "C:/Users/Michael/PycharmProjects/pythonProject3/TEXTNNIMPL.py", line 57, in <module>
    x = tf.raw_ops.Placeholder( tf.constant(n_input, dtype=tf.float64))
  File "C:\Users\Michael\.conda\envs\pythonProject3\lib\site-packages\tensorflow\python\util\tf_export.py", line 400, in wrapper
    raise TypeError(
TypeError: placeholder only takes keyword args (possible keys: ['dtype', 'shape', 'name']). Please pass these args as kwargs instead.

Process finished with exit code 1

Solution

  • I am assuming that you don't really know what you are doing. If you are, skip to the explanation of the error.

    Placeholders are not used anymore in TF2. Well, not exactly, they are used in the internals of TensorFlow, but the end user of the library is not really supposed to tinker with them too much. I suspect you are using a TensorFlow tutorial based on TF1 and are trying to adapt the code to TF2. I suggest you rather start directly by learning how TF2 works, which is quite different from TF1. The TensorFlow website provides multiple guides and tutorials for all levels, I suggest you start there.


    Explanation

    The error means that you should use only keyword arguments when calling that function. Here is an excerpt of the python documentation about keyword arguments :

    keyword argument: an argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by **. For example, 3 and 5 are both keyword arguments in the following calls to complex():

    complex(real=3, imag=5)
    complex(**{'real': 3, 'imag': 5})
    

    So the error tells you that you should instead construct your Placeholder using the keyword arguments of the class. Reading the documentation of tf.raw_ops.Placeholder, we see:

    Args

    dtype A tf.DType. The type of elements in the tensor.

    shape An optional tf.TensorShape or list of ints. Defaults to None. (Optional) The shape of the tensor. If the shape has 0 dimensions, the shape is unconstrained.

    name A name for the operation (optional).

    Meaning that the placeholder must be built by stating explicitly the name of each argument. One example could be the following:

    a = tf.raw_ops.Placeholder(dtype=tf.float32, shape=(None, 10), name="my_placeholder")
    

    This op will also fail for an other reason, stated in the doc :

    N.B. This operation will fail with an error if it is executed. It is intended as a way to represent a value that will always be fed, and to provide attrs that enable the fed value to be checked at runtime.

    As stated in the beginning, this op is mainly used in TensorFlow internals, I suggest to stay away from it unless you really know what you are doing.