且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

InvalidArgumentError:您必须为占位符张量 Placeholder 提供一个值

更新时间:2023-01-08 17:08:26

如果在创建占位符后删除 tf.Variable() 行(并修改相应地输入变量).

Your code should work if you remove the tf.Variable() lines after you created the placeholders (and modify the name of the fed variables accordingly).

占位符用于您想要为模型提供数据的变量.变量用于模型的参数(如权重).

Placeholders are for variables that you want to feed your model with. Variables are for parameters of your model (like weights).

因此,您正确地创建了两个占位符,但随后您无缘无故地创建了其他变量,这可能会在 Tensorflow 图中搞砸.

Therefore you correctly created two placeholders, but then you created additional variables for no reason, which probably messes something up in the Tensorflow graph.

函数看起来像:

import tensorflow as tf
import numpy as np

def my_matmult2(mat_1, mat_2):
    #define session
    x_sess1=tf.Session()

    with x_sess1:
        #initialize placeholders
        xmat_1_plh = tf.placeholder(dtype=mat_1.dtype, shape=mat_1.shape)
        xmat_2_plh = tf.placeholder(dtype=mat_2.dtype, shape=mat_2.shape)  

        r1 = tf.matmul(xmat_1_plh, xmat_2_plh)

        x_sess1.run(tf.initialize_all_variables())

        #

        qq1 = x_sess1.run(r1, feed_dict={xmat_1_plh: mat_1 , xmat_2_plh: mat_2})

    return qq1 

mat_1=np.ones((5,5))
mat_2=np.ones((5,5))

b=my_matmult2(mat_1,mat_2)
print b