Tensorflow Adjusting Cost Function for Imbalanced Data
This seems like a good solution for the loss function. I've had success with a similar approach recently, but I think you'd want to reorder where you multiply in the class_weight. 

Thinking about it logically, the class_weight will be a constant w.r.t. the output, so it will be carried along and applied to the gradient in the same way it's being applied to the cost function. There is one problem though.

The way you have it, the class_weight would affect the prediction value. But you want it to affect the scale of the gradient. If I'm not wrong I think you'd want to reverse the order of operations:

# Take the cost like normal
error = tf.nn.softmax_cross_entropy_with_logits(pred, y)

# Scale the cost by the class weights
scaled_error = tf.mul(error, class_weight)

# Reduce
cost = tf.reduce_mean(scaled_error)


I'd be very interested to know how this performs in comparison to simply oversampling the underrepresented class, which is more typical. So if you gain some insight there post about it! :)

Interestingly I successfully used a very similar technique in a different problem domain just recently (which brought me to this post):

Multi-task learning, finding a loss function that &quot;ignores&quot; certain samples