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?