Python 错误:在Tensorflow1.x中输入占位符时,无法识别Tensor。有什么建议吗?

Python 错误:在Tensorflow1.x中输入占位符时,无法识别Tensor。有什么建议吗?,python,tensorflow,placeholder,Python,Tensorflow,Placeholder,我在尝试输入占位符时遇到了一个问题,在train_epoch函数中使用feed_dict={..},它无法识别占位符 这是密码 class CNN(object): ###...... def define_train_opeartions(self): X_data_train = tf.placeholder(dtype=tf.float32, shape=(None, self.height,self.width,self.chan),name='X_da

我在尝试输入占位符时遇到了一个问题,在train_epoch函数中使用feed_dict={..},它无法识别占位符

这是密码

 class CNN(object):
    ###......
    def define_train_opeartions(self):
        X_data_train = tf.placeholder(dtype=tf.float32, shape=(None, self.height,self.width,self.chan),name='X_data_train')

        Y_data_train = tf.placeholder(dtype=tf.int32, shape=(None, self.n_classes),name='Y_data_train')  # Define this

        # Network prediction
        Y_net_train = self.inference(
            X_data_train,reuse=False)

        # Loss of train data tf.nn.softmax_cross_entropy_with_logits
        self.train_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y_data_train, logits=Y_net_train, name='train_loss'))

        # define learning rate decay method
        global_step = tf.Variable(0, trainable=False, name='global_step')
        # Define it--play with this
        learning_rate = 0.001

        # define the optimization algorithm
        # Define it --shall we try different type of optimizers
        optimizer = tf.train.AdamOptimizer(learning_rate)

        trainable = tf.trainable_variables()  # may be the weights??
        self.update_ops = optimizer.minimize(
            self.train_loss, var_list=trainable, global_step=global_step)

        # --- Validation computations
        X_data_valid = tf.placeholder(dtype=tf.float32, shape=(None, self.height, self.width, self.chan))  # Define this
        Y_data_valid = tf.placeholder(dtype=tf.int32, shape=(None, self.n_classes))  # Define this

        # Network prediction
        Y_net_valid = self.inference(X_data_valid,reuse=True)

        # Loss of validation data
        self.valid_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(
            labels=Y_data_valid, logits=Y_net_valid, name='valid_loss'))
然后我得到了另一个函数

def train_epoch(self, sess):
        train_loss = 0
        total_batches = 0
        keep_probability=0.2     #dropout probability
        n_batches = self.train_size / self.batch_size  # ??
        indx=0
        while (total_batches < n_batches):     # loop through train batches:
            X,Y=self.shuffling(self.Xtrain_in,self.Ytrain_in)  # shuffle X ,Y data
            Xbatch,Ybatch,indx=self.read_nxt_batch(X,Y,self.batch_size,indx)    # take the right batch
            mean_loss, _ = sess.run([self.train_loss, self.update_ops], feed_dict={X_data_train: Xbatch ,Y_data_train: Ybatch })
            if math.isnan(mean_loss):
                print('train cost is NaN')
                break
            train_loss += mean_loss
            total_batches += 1

        if total_batches > 0:
            train_loss /= total_batches

        return train_loss
def列(self,sess):
列车损耗=0
批次总数=0
保留概率=0.2#辍学概率
n_批次=self.train_size/self.batch_size??
indx=0
而(总批次0:
列车损失/=总批次
回程列车损耗
错误消息:TypeError:无法将馈送dict键解释为张量:名称>'X\u data\u train'指的是操作,而不是张量。 张量名称的形式必须为op_name:output_index


占位符张量名称与您为其指定的操作名称相同。这是导致错误的原因。为Op指定一个不同的名称:

X_data_train = tf.placeholder(dtype=tf.float32, shape=(None, self.height, self.width, self.chan), name='x_train_ph')
与Y_数据_列车相同