TensorFlow2:占位符只接受关键字args



嗨,我目前正在尝试使用Tensorflow学习一些基本的机器学习,并尝试使用占位符函数。我一辈子都搞不清楚这只需要关键词args意味着什么,而在互联网上搜索并没有多大帮助。我有下面的代码和错误。有人能给我举一个如何正确使用这个函数的例子,并像我五岁一样向我解释吗?谢谢

代码:

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

堆栈跟踪:

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:UsersMichael.condaenvspythonProject3libsite-packagestensorflowpythonutiltf_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

我假设您并不真正知道自己在做什么。如果是,请跳到错误的解释。

TF2中不再使用占位符。嗯,不完全是这样,它们被用于TensorFlow的内部,但库的最终用户实际上不应该对它们进行太多修改。我怀疑您使用的是基于TF1的TensorFlow教程,并试图将代码调整为适用于TF2。我建议您直接从学习TF2的工作原理开始,它与TF1截然不同。TensorFlow网站为所有级别提供了多个指南和教程,我建议您从那里开始。


说明

该错误意味着您在调用该函数时应仅使用关键字参数。以下是关于关键字参数的python文档摘录:

关键字参数:函数调用中以标识符(例如name=(开头的参数,或作为字典中以**开头的值传递的参数。例如,在以下对complex((的调用中,3和5都是关键字参数:

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

因此错误告诉您应该使用类的关键字参数来构造占位符。阅读tf.raw_ops.Placeholder的文档,我们看到:

Args

dtype一个tf.dtype。张量中元素的类型。

shape可选的tf.TensorShape或int列表。默认为"无"。(可选(张量的形状。如果该形状具有0个维度,则该形状为不受约束。

name操作的名称(可选(。

这意味着占位符必须通过显式声明每个参数的名称来构建。一个例子可能如下:

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

该操作也将因其他原因失败,如文档中所述:

N.B.如果执行此操作,则会失败并出现错误它旨在表示将始终被馈送的值,并提供属性,使馈送的值能够在运行时进行检查。

如前所述,此操作主要用于TensorFlow内部,我建议您远离它,除非您真的知道自己在做什么。

// python code should be:
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()  # disable v2
x = tf.raw_ops.Placeholder(dtype=tf.float64)
y = tf.raw_ops.Placeholder(dtype=tf.double)

最新更新