tensorflow:3.1)add_to_collection和L2正则化
2017-06-03 15:38
561 查看
1.add_to_collection
add_to_collectio为Graph的一个方法,可以简单地认为Graph下维护了一个字典,key为name,value为list,而add_to_collection就是把变量添加到对应key下的list中
下面给出一个简单的demo,其目的是为了说明在collection中的变量是对象引用
2.L2正则化
你可以简单理解L2为欧式距离,具体如何正则化权重,请参考neural-networks-and-deeplearning,这里我只进行简单的介绍。loss0表示原始的损失函数,后面的部分为L2正则化。
loss=loss0+λ2∑ww2
规范化的效果是让网络倾向于学习小一点的权重,大的权重只有能够给出代价函数第一项足够的提升时才被允许。换言之,规范化可以当做一种寻找小的权重和最小化原始的代价函数之间的折中。这两部分之前相对的重要性就由的值来控制了λ越小,就偏向于最小化原始代价函数,反之,倾向于小的权重。
在下一节,因为要使用L2正则化权重,所以使用collection来保存多个权重向量的L2值,在把它们累加即λ2Σw2。
add_to_collectio为Graph的一个方法,可以简单地认为Graph下维护了一个字典,key为name,value为list,而add_to_collection就是把变量添加到对应key下的list中
add_to_collection(name,value) Stores value in the collection with the given name. Note that collections are not sets, so it is possible to add a value to a collection several times. Args: name: The key for the collection. The GraphKeys class contains many standard names for collections. value: The value to add to the collection.
下面给出一个简单的demo,其目的是为了说明在collection中的变量是对象引用
sess=tf.InteractiveSession() #初始化2个Variable v1=tf.Variable(tf.constant(1)) v2=tf.Variable(tf.constant(1)) #设置保存到collection的name为collection name='collection' #把v1和v2添加到默认graph的collection中 tf.add_to_collection(name,v1) tf.add_to_collection(name,v2) #获得名为name的集合 c1 = tf.get_collection(name) tf.global_variables_initializer().run() print("the first collection: %s"%sess.run(c1)) #修改v1和v2的值,必须使用eval()或run()进行执行 tf.assign(v1,tf.constant(3)).eval() tf.assign(v2,tf.constant(4)).eval() #再次查看collection中的值 c2 = tf.get_collection(name) print("the second collection: %s"%sess.run(c2)) print("the sum of collection: %s"%sess.run(tf.add_n(c2)) #resut: #the first collection: [1, 1] #the second collection: [3, 4] #the sum of collection: 7
2.L2正则化
你可以简单理解L2为欧式距离,具体如何正则化权重,请参考neural-networks-and-deeplearning,这里我只进行简单的介绍。loss0表示原始的损失函数,后面的部分为L2正则化。
loss=loss0+λ2∑ww2
规范化的效果是让网络倾向于学习小一点的权重,大的权重只有能够给出代价函数第一项足够的提升时才被允许。换言之,规范化可以当做一种寻找小的权重和最小化原始的代价函数之间的折中。这两部分之前相对的重要性就由的值来控制了λ越小,就偏向于最小化原始代价函数,反之,倾向于小的权重。
在下一节,因为要使用L2正则化权重,所以使用collection来保存多个权重向量的L2值,在把它们累加即λ2Σw2。
相关文章推荐
- TensorFlow学习--tf.add_to_collection与tf.get_collection使用
- tensorflow: tf.add_to_collection 探究
- L2正则化—tensorflow实现
- 【tensorflow 学习】给LSTM加上L2正则化
- add_to_collection的用法
- how to solve Error cannot add duplicate collection entry of type add with unique key attribute 'valu
- Failed to add FS to collection at ResourceService.SetResource in Map 3D 2013
- tf.add_to_collection()
- 如何利用tf.add_to_collection、tf.get_collection以及tf.add_n来简化正则项的计算
- How to create Navigation Collection ,publish it as a pagelet and add to homepage ?
- tensorflow add_to_collection用法
- tf.add_to_collection、tf.get_collection、tf.add_n用法浅析
- tf.add_to_collection,tf.get_collection和tf.add_n的用法
- add_featurelayer_to_map
- Suggestion: add 'tools:replace="android:icon"' to <application> element at AndroidManifest.xml
- Genymotion模拟器add时候出现Unable to create virtual device Fail to create SD card解决办法
- 在VC6.0 Add file to project 出现“MSDEV.EXE-应用程序错误
- Github:failed to add file / to index
- uvcvideo: Add FIX_BANDWIDTH quirk to HP Webcam found on HP Mini 5103 netbook
- XNA 3.1 TO XNA 4.0 Conversion Cheat Sheet