2

One of my black beast in Tensorflow is the question of Shape. I always stack their.

That time I have a multiclassification problem and I need to use one_hot encoding with nn.softmax_cross_entropy_with_logits. I have tried many solution on the net but I still always get this error:

Cannot feed value of shape (100, 1) for Tensor 'input/Y:0', which has shape '(?,)'

Here is the essential part of my code :

Here where I set my placeholder and I apply tf.one_hot:

with tf.name_scope('input'):
    # [BATCH_SIZE, NUM_FEATURES]
    self.X=tf.placeholder(dtype=tf.float32, shape=[None,self.n_input_train], name="X")
    # [BATCH_SIZE]
    self.Y = tf.placeholder(dtype=tf.int32, shape=[None], name='Y')
    self.is_train = tf.placeholder(tf.bool, name="is_train")
    # [BATCH_SIZE, NUM_CLASSES]
    self.Y_onehot = tf.one_hot(indices=self.Y, depth=self.n_classes, on_value=1, off_value=0, name='Y_onehot')

The code stack here in sess.run showing the error above :

for sample in mini_batches:
    batch_x = x_train.iloc[sample, :]
    batch_y =train_output.iloc[sample, :]
    #batch_y = np.reshape(batch_y, (-1))
    feed_dict={self.X: batch_x,self.Y:batch_y, self.is_train:True}
    self.train_summary, _, cost,acc=self.sess.run([self.merged, self.train_step, self.loss_, self.accuracy_],feed_dict=feed_dict)
    avg_cost += cost *len(sample)/n_samples 
    print('epoch[{}] step [{}] train -- loss : {}, accuracy : {}'.format(epoch,step, avg_cost, acc))
    step += 100

My labels look like something like this (it's a vector of one column only containing the values of the factors that represent my classes) :

    0
0   108
1   30
2   30
3   16
4   62
5   126
6   22
7   30
8   48

And here how i declare the last output in my model :

# Output fully connected layer with the output
        out_layer = tf.layers.dense(inputs=layer_3, units= self.n_classes, use_bias=True, kernel_initializer=self._init, name= 'out_layer')

And those are the diff shapes :

The shape of logits  (?, 64)
The shape of Y  (?, 64)
The shape of X  (?, 14)
The shape of tain_input  (847, 14)
The shape of tain_output  (847, 1)
The shape of y_batch  (100, 1) 

Edit:

Here is the model :

 def multilayer_perceptron(self,X):


        # Hidden fully connected layer with n_hidden_1 neurons
        layer_1 = tf.layers.dense(inputs=X, units= self.n_hidden_1, use_bias=True, kernel_initializer=self._init, name= 'layer_1')
        layer_1 = tf.layers.batch_normalization(layer_1,training=self.is_train)
        layer_1 = self.activation(layer_1)     

        # Hidden fully connected layer with n_hidden_2 neurons
        layer_2 = tf.layers.dense(inputs=layer_1, units= self.n_hidden_2, use_bias=True, kernel_initializer=self._init, name= 'layer_2')
        layer_2 = tf.layers.batch_normalization(layer_2,training=self.is_train)
        layer_2 = self.activation(layer_2)   

        # Hidden fully connected layer with n_hidden_3 neurons
        layer_3 = tf.layers.dense(inputs=layer_2, units= self.n_hidden_3, use_bias=True, kernel_initializer=self._init, name= 'layer_3')
        layer_3 = tf.layers.batch_normalization(layer_3, training=self.is_train)
        layer_3 = self.activation(layer_3)  

        # Output fully connected layer with the output
        out_layer = tf.layers.dense(inputs=layer_3, units= self.n_classes, use_bias=True, kernel_initializer=self._init, name= 'out_layer')

        tf.summary.histogram('pre-activations', out_layer)

        return layer_1, layer_2, layer_3, out_layer 

And here is how I calculate the loss:

def loss(self, X, Y):
    _, _, _, self.logits = self.multilayer_perceptron(X) 
    print("The shape of logits ", self.logits.get_shape())
    print("The shape of Y ", self.Y.get_shape())
    print("The shape of X ", X.get_shape())
    with tf.name_scope('loss'):    
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.logits, labels=Y))
    tf.summary.scalar('loss', loss)         
    with tf.name_scope('accuracy'):
            with tf.name_scope('correct_prediction'):
                  correct_prediction = tf.equal(tf.argmax(self.logits, 1), tf.argmax(Y, 1))
            with tf.name_scope('accuracy'):
                 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar('accuracy', accuracy)
    self.merged =  tf.summary.merge_all()
    return loss, accuracy 

And here is the where I call the loss function:

def cross_validation(self,batch_size, n_hidden_1 , n_hidden_2, n_hidden_3, learning_rate):
loss = 0
tf.reset_default_graph()

with tf.name_scope('input'):
    ...
    # [BATCH_SIZE]
    #self.Y=tf.placeholder(dtype=tf.int64, shape=[None,self.y_train.shape[1]], name="Y")
    self.Y = tf.placeholder(dtype=tf.int32, shape=[None], name='Y')          
    # [BATCH_SIZE, NUM_CLASSES]
    ...

self.loss_, self.accuracy_ = self.loss(self.X, self.Y_onehot)
self.train_step = self.optimizer(self.learning_rate).minimize(self.loss_)

# Initiate a tensor session
init = tf.global_variables_initializer()
self.sess = tf.Session()
self.sess.run(init)

#train the model 
loss = self.train()

self.sess.close()
del self.sess
return  loss  

How can I fix that ?

Is their any tips to follow to avoid those problem of shapes?

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
  • 1
    Where is the definition of the cost? Where is the model definition? What does 'pb' mean in 'avoid those pb of shapes'? Without the necessary information, it looks like in your model you are putting the wrong variable name in your model, or not realising it expects input of a different shape. Without showing us your model definition, there is not much we can do. – Alxmrphi May 14 '19 at 22:15
  • 1
    Ok i will add the model and the cost, pb = problem. – DINA TAKLIT May 14 '19 at 22:17
  • 1
    What do you pass into the loss function? It looks like you pass in an X and a Y and then completely ignore them, opting instead to use the class variables via self.X and self.Y. As you can see, your y_batch is not a one-hot vector like it needs to be. You need to be using self.y_onehot (if I can make correct inferences about what the rest of your code must be). – Alxmrphi May 14 '19 at 22:25
  • I did add where i did call loss : here is the complete code here with some modif : https://stackoverflow.com/questions/55923207/my-classifier-has-very-big-loss-and-accuracy-is-always-0. – DINA TAKLIT May 14 '19 at 22:32
  • bui I pass to the loss y_onehot. – DINA TAKLIT May 14 '19 at 22:33
  • 1
    You define the loss in terms of self.logits and the input to the loss function Y, which you have said is actually self.Y_onehot. That's fine so far, but when you take batch_y, it isn't one-hot encoded. By defining your input to take self.Y and then convert it to one-hot, that only happens at model definition time. That is not an automatic step that happens at each iteration / epoch. I think you are treating it like it would happen automatically. Once you get batch_y, make it a one-hot vector. – Alxmrphi May 14 '19 at 22:49
  • Inded I follow this : https://github.com/AFAgarap/multilayer-perceptron/blob/master/MLP.py. where should I put "onehot " under bach_y or inside loss function can you show me how please. – DINA TAKLIT May 14 '19 at 22:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193360/discussion-between-dina-taklit-and-alxmrphi). – DINA TAKLIT May 14 '19 at 23:03
  • Thank you I did fix the issue. – DINA TAKLIT May 16 '19 at 19:21

1 Answers1

1

I did fix the issue finally by using flatten(), it transform 2D array to 1D array :

 batch_y =train_output.iloc[sample, :]
 batch_y = np.array(batch_y).flatten()
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74