tensorflow训练时的一些注意事项
2017-06-28 21:56
381 查看
1,使用batch norm层后,计算损失时,注意添加相应操作
最后计算损失时:
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,待续
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,待续
相关文章推荐
- Tensorflow利用GPU训练注意事项
- 狗狗训练11招 及 训犬注意事项
- 编程中的一些注意事项
- ExecuteReader方法中CommandBehavior.CloseConnection的一些注意事项
- 胡百敬老师的《撰写信息书籍注意事项》以及我自己的一些感想
- 初学SWT的一些经验分享以及注意事项
- WarIII 对战中的一些注意事项
- 乒乓球训练注意事项
- Google 广告的一些注意事项
- Web登陆模块的常见解决方案及一些注意事项
- NMS SIP的一些工程注意事项
- 关于安全编写代码的一些注意事项[参照msdn],这些在编程基础中都已经相当重要了!
- 在jsp使用标签库的一些注意事项
- 无模式对话框的一些注意事项
- 水晶报表打包的一些注意事项
- 入门训练:写程序应注意的事项
- 在Tomcat中配置应用Context的一些差异与注意事项。
- 关于使用存储过程的一些好处以及注意事项
- 总结出来的一些ASP.NET程序性能优化的注意事项[不断补充]
- 关于使用存储过程的一些好处以及注意事项