Tensorflow predicts always the same result

后端 未结 2 2045
小蘑菇
小蘑菇 2020-12-06 14:52

I\'m trying to get the TensorFlow example running with my own data, but somehow the classifier always picks the same class for every test example. The input data is always s

相关标签:
2条回答
  • 2020-12-06 15:25

    The other problem you may be having is a Class imbalance. If you have one class that greatly outweighs the other your function may be converging to that value. Try balancing the classes in your training sample as well as using smaller batches. For example if your labels are binary then make sure there is an equal amount of zeros and one labels in your training sample.

    0 讨论(0)
  • 2020-12-06 15:36

    There are three potential issues in your code:

    1. The weights, W, are initialized to zero. This question from stats.stackexchange.com has a good discussion of why this can lead to poor training outcomes (such as getting stuck in a local minimum). Instead, you should initialize them randomly, for example as follows:

      W = tf.Variable(tf.truncated_normal([pixels, labels],
                                          stddev=1./math.sqrt(pixels)))
      
    2. The cross_entropy should be aggregated to a single, scalar value before minimizing it, using for example tf.reduce_mean():

      cross_entropy = tf.reduce_mean(
          tf.nn.softmax_cross_entropy_with_logits(y_prime, y_))
      
    3. You may get faster convergence if you train on mini-batches (or even single examples) rather than training on the entire dataset at once:

      for i in range(10):
              for j in range(4000):
                  res = train_step.run({x: train_images[j:j+1],
                                        y_: train_labels[j:j+1]})
      
    0 讨论(0)
提交回复
热议问题