我在尝试提供占位符时遇到问题,在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
错误消息:TypeError:无法将feed_dict键解释为Tensor:名称&gt;'X_data_train'指的是Operation,而不是Tensor。 张量名称的格式必须为op_name:output_index。
答案 0 :(得分:0)
占位符张量名称与您为其指定的操作名称相同。那是造成错误的原因。给Op一个不同的名字:
X_data_train = tf.placeholder(dtype=tf.float32, shape=(None, self.height, self.width, self.chan), name='x_train_ph')
与Y_data_train相同。