ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2)
I'm new in deep learning .I cannot understand this error : ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).
input shape = (batch, 64, 64)
labels = (1,)
Traceback (most recent call last): File "new.py", line 103, in loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=Y)) File "/usr/local/lib/python3.5/distpackages/tensorflow/python/ops/nn_ops.py", line 2040, in sparse_softmax_cross_entropy_with_logits (labels_static_shape.ndims, logits.get_shape().ndims))
ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).
logits = conv_net(X, weights, biases, keep_prob)
prediction = tf.nn.softmax(logits)
loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(loss_op)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
See also questions close to this topic

Creating a dataset in tensorflow by adding random elements of two datasets
I have two sets of images, say set A and set B. These are available as numpy arrays. I will choose x random images from A and y random images from B and then add them together (taking the average at the end). These new images will become the input to a CNN.
Now my question is how can I do this using Tensorflow's data pipeline approach?
I can obviously do it outside of TensorFlow using numpy, but this precreated data will be too big in size. I can also use feed_dicts, but I wanted to know if there's a way to use pipelines to achieve this because that seems to be favoured approach now.

how to use trained model as recommend system?
I use tensorflow to build and train a Factorization Machine as a recommend system,the input is the combined features of special users and items,and the lable is the score of interest.but how can i use the trained model to recommend to real users,for there is more than 100k users and items respectively,the combination of users and items is so large,and calculate the score of the combination is impossilbe

tf.train.shuffle_batch returning nan's after random number of iterations
While training a pretty standard convolutional net, I discovered a weird bug. Everything starts out fine with a nice loss curve, but suddenly the loss goes to nan. I was able to trace the nans back all the way to the input pipeline.
As you can see, I am printing the errors before and after they are batched using tf.train.shuffle_batch(). The second print comes up as nan, and this is propagated all the way through the question.
What might be causing this? I have played around with different values of capacity, threads, etc.
The code and context is below. Nans are appearing in the batched before/after images, but not the before/after image.
I should note that the tfrecord files have an arbitrary number of examples in them, but I believe that this shouldn't matter for the enqueue/dequeue operations.
def input_pipeline(self, filenames, batch_size, num_epochs=None): """Function that creates a highly abstracted input pipeline consisting of a bunch of threads and queues given a few simple parameters. See https://www.tensorflow.org/versions/r0.10/how_tos/reading_data/index.html#multipleinputpipelines for more information and indepth explanations. Args:  filenames: a list of filenames of tfrecords files random.shuffle(filenames) train_filenames = filenames train_filename_queue = ( tf.train.string_input_producer(train_filenames, num_epochs=num_epochs, shuffle=True, seed=1)) before_image, after_image, mask_image = ( self._read_and_decode_reach_tfrecords(train_filename_queue)) # min_after_dequeue defines how big a buffer we will randomly sample # from  bigger means better shuffling but slower start up and more # memory used. # capacity must be larger than min_after_dequeue and the amount larger # determines the maximum we will prefetch. Recommendation: # min_after_dequeue + (num_threads + a safety margin) * batch_size min_after_dequeue = 1000 saftey_margin = 3 capacity = min_after_dequeue + (3 + saftey_margin) * batch_size capacity = 2000 before_image = tf.Print(before_image,[tf.reduce_mean(before_image + after_image)], "pre_shuffle: ") mask_image = tf.Print(mask_image, [tf.reduce_mean(mask_image)], "pre_shuffle_mask: ") before_images, after_images, mask_images = ( tf.train.shuffle_batch( [before_image, after_image, mask_image], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue, num_threads=5, seed=1)) before_images = tf.Print(before_images,[tf.reduce_mean(before_images + after_images)], "post_shuffle: ")

Cartpolev1 using A2C has very low reward with no reason
I'm using a2c to work on the Cartpolev1. The final goal is to find the mean reward. However, my reward is less than 10. I was thinking whether my code erase part of the rewards or something during the training process? I don't know whats wrong with it. The setup of the code seems fine. The whole process is supposed to have reward around 300~500.
import tensorflow as tf import numpy as np import gym # Supresses compilation warnings import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' # Constants learning_rate = 0.001 #trials = 3 episodes = 1000 moves = 999 discount = 0.99 hidden_size = 32 critic_size = 128 updates = 50 avgRs = [] totRs = [] # Helper function to generate distribution def generate_disRs(hist): dist = [] last_reward = 0 for element in reversed(hist): reward = discount * last_reward + element dist.append(reward) last_reward = reward return list(reversed(dist)) class A2C: def __init__(self): self.game = gym.make('CartPolev1') self.game.reset() self.num_actions = self.game.action_space.n self.state_size = self.game.observation_space.shape[0] self.state_input = tf.placeholder(tf.float32, [None, self.state_size]) self.rewards = tf.placeholder(shape=[None], dtype=tf.float32) # Define any additional placeholders needed for training your agent here: self.state_value = self.critic() self.actProbs = self.actor() self.loss_val = self.loss() self.train_op = self.optimizer() self.session = tf.Session() self.session.run(tf.global_variables_initializer()) def optimizer(self): """ :return: Optimizer for your loss function """ optimizer = tf.train.AdamOptimizer(learning_rate) trainOp = optimizer.minimize(self.loss) return trainOp def critic(self): """ Calculates the estimated value for every state in self.state_input. The critic should not depend on any other tensors besides self.state_input. :return: A tensor of shape [num_states] representing the estimated value of each state in the trajectory. """ # Placeholders for critic loss V1 = tf.Variable(tf.random_normal([4, critic_size], dtype=tf.float32, stddev=.1)) v1Out = tf.nn.relu(tf.matmul(self.state_input, V1)) V2 = tf.Variable(tf.random_normal([critic_size, 1], dtype=tf.float32, stddev=.1)) self.v2Out = tf.matmul(v1Out, V2) return self.v2Out def actor(self): """ Calculates the action probabilities for every state in self.state_input. The actor should not depend on any other tensors besides self.state_input. :return: A tensor of shape [num_states, num_actions] representing the probability distribution over actions that is generated by your actor. """ # Layer 1 # self.state = tf.placeholder(shape=[None, 4], dtype=tf.float32) self.W = tf.Variable(tf.random_uniform([4, hidden_size], dtype=tf.float32)) self.bias = tf.Variable(tf.random_uniform([hidden_size], dtype=tf.float32)) self.hidden = tf.nn.relu(tf.matmul(self.state_input, self.W) + self.bias) # Layer 2 self.O = tf.Variable(tf.random_uniform([hidden_size, 2], dtype=tf.float32)) self.bias2 = tf.Variable(tf.random_uniform([2], dtype=tf.float32)) self.output = tf.nn.softmax(tf.matmul(self.hidden, self.O) + self.bias2) self.actions = tf.placeholder(shape=[None], dtype=tf.int32) self.indices = tf.range(0, tf.shape(self.output)[0]) * 2 + self.actions self.actProbs = tf.gather(tf.reshape(self.output, [1]), self.indices) #self.aloss = tf.reduce_mean(tf.log(self.actProbs) * self.advantage) return self.actProbs #return self.aloss def loss(self): """ :return: A scalar tensor representing the combined actor and critic loss. """ # Placeholders # self.rewards = tf.placeholder(shape=[None], dtype=tf.float32) # self.actions = tf.placeholder(shape=[None], dtype=tf.int32) # self.indices = tf.range(0, tf.shape(self.output)[0]) * 2 + self.actions # self.actProbs = tf.gather(tf.reshape(self.output, [1]), self.indices) self.aloss = tf.reduce_mean(tf.log(self.actProbs) * self.rewards) self.cLoss = tf.reduce_mean(tf.square(self.rewards  self.v2Out)) self.loss = self.aloss + self.cLoss return self.loss def train_episode(self): """ train_episode will be called 1000 times by the autograder to train your agent. In this method, run your agent for a single episode, then use that data to train your agent. Feel free to add any return values to this method. """ # reset st = self.game.reset() # List to store state, action and reward histories state_hist = [] action_hist = [] reward_hist = [] # List to store history of states and values state_value_hist = [] for move in range(moves): # Run actDict, stateVal = self.session.run([self.output, self.v2Out], feed_dict={self.state_input: [st]}) # Get the random action action = np.random.choice(np.array([0, 1]), p=actDict[0]) st1, reward, done, info = self.game.step(action) # Render the game # game.render() # Add to the history action_hist.append(action) reward_hist.append(reward) state_hist.append(st) state_value_hist.append(stateVal[0][0]) # Iterate st = st1 # Update if done or (move % updates == 0 and move != 0): # Get disRs disRs = generate_disRs(reward_hist) # Compute Difference difference = np.array(disRs)  np.array(state_value_hist) # Run feed_dict = {self.state_input: state_hist, self.actions: action_hist, self.rewards: difference} l, _ = self.session.run([self.loss_val, self.train_op], feed_dict=feed_dict) if done: totRs.append(move) # print move, disRs[0] break def check_actor(model): """ The autograder will use your actor() function to test your agent. This function checks that your actor returns a tensor of the right shape for the autograder. :return: True if the model's actor returns a tensor of the correct shape. """ dummy_state = np.ones((10, 4)) #actDict = model.session.run(model.output, feed_dict={model.state_input: [model.game.reset()]}) actor_probs = model.session.run(model.actProbs, feed_dict={ model.state_input: dummy_state) }) return actor_probs.shape == (10, 2) if __name__ == '__main__': # Change __main__ to train your agent for 1000 episodes and print the average reward over the last 100 episodes. # The code below is similar to what our autograder will be running. learner = A2C() for i in range(1000): learner.train_episode() print(str(np.average(totRs[900: 1000])))

Do I need to install CUDA driver for tensorflowgpu manually if I install tf through conda
I followed this tutorial and installed tfgpu using conda (https://www.pugetsystems.com/labs/hpc/TheBestWaytoInstallTensorFlowwithGPUSupportonWindows10WithoutInstallingCUDA1187/) and it worked because I am seeing "...gpu:0" in my printed out log. Before I did the installation, I already have CUDA driver installed, so I am not sure.
Seems to me that conda install tensorflowgpu comes with cuda toolkit and cuDNN,etc. I was wondering if installing CUDA driver is a require step. Another post I found did't mention driver either (https://towardsdatascience.com/tensorflowgpuinstallationmadeeasyusecondainsteadofpip52e5249374bc). But the official GPU guide says it's required, so I am confused. I am doing it on Windows 10.
 What is Ordered max pooling and Orderless pooling?

Image classifier neural network using Tensorflow In C++
I have 2D Trajectory data for various categories such as Line, Circle, Ellipse and Arc.
I want to construct a simple CNN network to train my images data and predict the right classification.
My approach is as follows:
1) Instead of providing the input as "Images", I want to provide the input as a Matrix (lets say, of size 200x200).Since,I have the (x,y) coordinates data, I would be scaling all my coordinates data so that they lie in the range of (0,200).Finally,I would populate the values of 0's (indices in the matrix,which do not consist of the coordinates data) and 1's (indices,which contain the coordinates data) to my initialized matrix (initially,all the indices contain 0's)
2) Train a simple CNN Model and predict its classification.
I would be really glad,if someone can help me achieve this task using C++.As the web help in case of CNN Implementation in C++ is very limited,I am finding it really difficult to achieve it in C++.
Thanks in Advance.

Is it possible to infer more than one parameter from Convolution neural network
I have a question and I am not sure if it's a smart one. But I've been reading quite a lot about convolution neural networks. And so far I understand that the output layer could for example be a softmax layer for a classification problem or you could do regression in order to get a quantitative value. But I was wondering if it is possible to infer more than one parameter. For example, if I have a data and my output label is both price of the house and size of the house. I know it is not a smart example. But I just want to know if it's possible to predict two different output values in the same output layer in the convolution neural network. Or do I need to have two different convolution neural network where one predicts the size of the house and the one predicts price of the house. And how can we combine these two predictions then. And if we can do it in one convolution neural network, then how can we do that?