首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >错误:张量不被识别,当在Tensorflow1.x 1.x中提供占位符时,有什么建议吗?

错误:张量不被识别,当在Tensorflow1.x 1.x中提供占位符时,有什么建议吗?
EN

Stack Overflow用户
提问于 2018-06-08 12:41:19
回答 1查看 431关注 0票数 0

当我尝试输入占位符时,如果使用feed_dict ={..}在train_epoch函数中,它不识别占位符

这是密码。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 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'))

然后我又有了另一个功能

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

错误消息:TypeError:无法将feed_dict键解释为张量:名称>“X_data_train”指的是操作,而不是张量。张量名称必须是op_name:output_index形式的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 12:47:34

占位符张量名称与您给它的操作名称相同。这导致了错误。给行动组取一个不同的名字:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
X_data_train = tf.placeholder(dtype=tf.float32, shape=(None, self.height, self.width, self.chan), name='x_train_ph')

Y_data_train也是如此。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文