簡體   English   中英

Tensorflow:InvalidArgumentError:您必須使用dtype int32為占位符張量'yy'提供值

[英]Tensorflow: InvalidArgumentError: You must feed a value for placeholder tensor 'yy' with dtype int32

import tensorflow as tf
y_hat = tf.constant(36, name='y_hat')            # Define y_hat constant. Set to 36.
yy = tf.placeholder(tf.int32, shape=[])

loss = tf.Variable((yy - y_hat)**2, name='loss')  # Create a variable for the loss

init = tf.global_variables_initializer()    

with tf.Session() as session:
    session.run(tf.global_variables_initializer(), feed_dict = {yy: 39})
    print(session.run(loss, feed_dict={yy: 39}))

作為Tensorflow的新手,我很難理解在這個框架中如何管理占位符。

如果我第一次運行上面的代碼,它將返回9(正確的值)。 但是如果我在同一個jupyter會話中再次運行它,我會得到以下錯誤。 就好像我使用“with”來關閉會話一樣,全局變量(在本例中為占位符)沒有得到清理

堆棧跟蹤:

InvalidArgumentError: You must feed a value for placeholder tensor 'yy' with dtype int32
     [[Node: yy = Placeholder[dtype=DT_INT32, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'yy', defined at:
  File "/opt/conda/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/opt/conda/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)

知道發生了什么以及如何解決它? 謝謝

import tensorflow as tf右下方添加行tf.reset_default_graph() import tensorflow as tf ,這樣每次運行代碼時都會重置張量流圖。 那你就不會得到這個錯誤。

順便說一下,您並不需要將loss指定為變量。 你可以跑

import tensorflow as tf
y_hat = tf.constant(36, name='y_hat')    
yy = tf.placeholder(tf.int32, shape=[])

loss = (yy - y_hat)**2  

with tf.Session() as session:
    print(session.run(loss, feed_dict={yy: 39}))

上面的代碼打印9。

暫無
暫無

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

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