簡體   English   中英

TensorFlow2:占位符只接受關鍵字參數

[英]TensorFlow2: placeholder only takes keyword args

嗨,我目前正在嘗試使用 Tensorflow 學習一些基本的機器學習,並且我正在嘗試使用占位符 function。 我一生都無法弄清楚它只需要關鍵字 args 意味着什么,並且在互聯網上進行搜索並沒有太大幫助。 我有下面的代碼和錯誤。 有人可以向我展示如何正確處理這個 function 並像我五歲一樣向我解釋嗎? 謝謝!

代碼:

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:\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

我假設您並不真正知道自己在做什么。 如果是,請跳至錯誤說明。

在 TF2 中不再使用占位符。 嗯,不完全是,它們被用於 TensorFlow 的內部,但是庫的最終用戶不應該對它們進行過多的修改。 我懷疑您正在使用基於 TF1 的 TensorFlow 教程,並試圖將代碼調整為 TF2。 我建議您直接從學習 TF2 的工作原理開始,這與 TF1 完全不同。 TensorFlow 網站為各個級別提供了多個指南和教程,我建議您從那里開始。


解釋

該錯誤意味着您在調用 function 時應該只使用關鍵字 arguments。 以下是有關關鍵字 arguments 的 python 文檔的摘錄:

關鍵字參數:在 function 調用中以標識符(例如 name=)開頭的參數,或作為字典中的值傳遞,以 ** 開頭。 例如,在以下對 complex() 的調用中,3 和 5 都是關鍵字 arguments:

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

所以錯誤告訴你應該使用 class 的關鍵字 arguments 來構造占位符。 閱讀tf.raw_ops.Placeholder 的文檔,我們看到:

精氨酸

dtype一個 tf.DType。 張量中元素的類型。

shape一個可選的 tf.TensorShape 或整數列表。 默認為無。 (可選)張量的形狀。 如果形狀的維度為 0,則形狀不受約束。

name操作的名稱(可選)。

這意味着必須通過明確說明每個參數的名稱來構建占位符。 一個示例可能如下:

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

此操作也將因其他原因而失敗,如文檔中所述:

注意如果執行此操作將失敗並出現錯誤。 它旨在作為一種表示將始終被饋送的值的方式,並提供能夠在運行時檢查饋送值的屬性。

如開頭所說,這個op主要用在TensorFlow內部,除非你真的知道自己在做什么,否則我建議遠離它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM