1、加入gradient clipping:
例如用的是交叉熵cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))的话,最后softmax层输出y_conv的取值范围在[0,1]页就是说允许取0值,有log(0)出现很有可能出现nan,cross_entropy = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y_conv,1e-15,1.0))),在tensorflow中可以限定一下y_conv的取值范围。
with tf.name_scope('cross_entropy'):
cross_entropy_mean = tf.losses.sparse_softmax_cross_entropy(labels=ground_truth_input, logits=logits)
把上面的替换成下面的:
with tf.name_scope('cross_entropy'):
one_hot_ground_truth_input = tf.one_hot(ground_truth_input, model_settings['label_count'])
softmax = tf.nn.softmax(logits)
cross_entropy_mean = -tf.reduce_mean(one_hot_ground_truth_input * tf.log(tf.clip_by_value(softmax, 1e-15, 1.0)))
2、修改参数初始化方法:
对于CNN可用xavier或者msra的初始化方法。
3、数据归一化:
3.1 白化:减均值、除方差;
3.2 加入normalization,比如BN、L2 norm等。
4、减小学习率、或者batch size;