I have a problem when trying to feed placeholders, with feed_dict ={..} in train_epoch function, it doesn't recognize the placeholders
Here is the code..
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'))
And then I've got another function
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
Error message:TypeError: Cannot interpret feed_dict key as Tensor: The name >'X_data_train' refers to an Operation, not a Tensor. Tensor names must be of the form op_name:output_index.
The placeholder tensor name is the same as the operation name you gave to it. That is causing the error. Give the Op a different name:
X_data_train = tf.placeholder(dtype=tf.float32, shape=(None, self.height, self.width, self.chan), name='x_train_ph')
Same with Y_data_train.