1,使用batch norm层后,计算损失时,注意添加相应操作
def conv_bn_relu(inputs, num_outputs, phase, kernel_size, stride=1, padding='SAME', scope=None,weight_decay=0): with tf.variable_scope(scope): bottom = layers.conv2d(inputs=inputs, num_outputs=num_outputs, kernel_size=kernel_size, activation_fn=None, weights_regularizer=layers.l2_regularizer(weight_decay),reuse=tf.get_variable_scope().reuse, stride=stride, padding=padding, normalizer_fn=None, scope='conv') bottom = layers.batch_norm(bottom, center=True, scale=True, is_training=phase, scope='bn') return tf.nn.relu(bottom, 'relu')
最后计算损失时:
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_step = tf.train.GradientDescentOptimizer(0.01).minimize(total_loss)其中,tf.control_dependencies(update_ops)表示with段中的操作是在update_ops操作执行之后 再执行的
2,稀疏标签转为dense_label (one_hot标签)
# 以下是把稀疏标签ys转为向量形式shape [batch_size, NUM_CLASSES].注意,需要把上面ys的int64替换为int32 batch_size = train_batch_size #假设是100 sparse_labels = tf.reshape(ys, [batch_size, 1]) print(np.shape(sparse_labels)) #[100*1] indices = tf.reshape(tf.range(batch_size), [batch_size, 1]) print(np.shape(indices)) #[100*1] concated = tf.concat( [indices, sparse_labels], 1 ) print(np.shape(concated)) #[100*2] dense_labels = tf.sparse_to_dense(concated, [batch_size, NUM_CLASSES], 1.0, 0.0) #[100*7] # 转换完成 #对应的计算loss: loss = tf.nn.softmax_cross_entropy_with_logits( logits=logits,labels= dense_labels ) loss = tf.reduce_mean(loss) #avg loss #计算accuracy: correct_prediction = tf.equal( tf.argmax( prediction,1 ), tf.argmax( dense_labels,1 ) ) accuracy = tf.reduce_mean( tf.cast(correct_prediction,tf.float32) )
3,待续