Tensorflow的一些基本用法
2017-09-06 09:11
495 查看
在使用TensorFlow中会遇到一些其基本的用法,再次作为记录备忘!
在计算整体的loss是会将不同部分的loss放入一个集合中,最后计算整体的loss,因此会用到tf.add_to_collection,具体参考TensorFlow中的cifar10的例子,用法如下所示:
2
3
1
2
3
一般这样用:
2
1
2
关于TensorFlow中softmax和entropy的计算:
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
大部分应用中标识的分布都会比较稀疏,可以使用下面方式提高效率:
1
http://blog.csdn.net/john_xyz/article/details/60882535
tf.add_to_collection
在计算整体的loss是会将不同部分的loss放入一个集合中,最后计算整体的loss,因此会用到tf.add_to_collection,具体参考TensorFlow中的cifar10的例子,用法如下所示:tf.add_to_collection:把变量放入一个集合,把很多变量变成一个列表 tf.get_collection:从一个结合中取出全部变量,是一个列表 tf.add_n:把一个列表的东西都依次加起来1
2
3
1
2
3
一般这样用:
tf.add_n(tf.get_collection("losses"),name='name') 其中losses是一个集合1
2
1
2
softmax
关于TensorFlow中softmax和entropy的计算:pre = tf.Variable([[10.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]]) lab =tf.Variable([[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]]) #计算softmax y = tf.nn.softmax(pre) #计算交叉熵 cross_entropy = tf.reduce_mean(-tf.reduce_sum(lab*tf.log(y),reduction_indices=[1])) #一次性计算softmax and entropy loss = tf.contrib.losses.softmax_cross_entropy(pre,lab)1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
大部分应用中标识的分布都会比较稀疏,可以使用下面方式提高效率:
tf.contib.losses.sparse_softmax_cross_entropy(logits,labels)1
1
tf.nn.embedding_lookup
http://blog.csdn.net/john_xyz/article/details/60882535
layers_core.Dense
import tensorflow as tf from tensorflow.python.layers import core as layers_core ones = tf.Variable([[1.0,2],[3,4]]) output_layer = layers_core.Dense(5) logits = output_layer(ones) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print sess.run(ones) print sess.run(logits)
相关文章推荐
- Tensorflow的一些基本用法
- java中 FileWriter类 和 FileReader类的一些基本用法
- 有关JavaScript的一些基本用法
- TensorFlow的基本用法
- Tensorflow一些常用基本概念与函数(1)
- Java集合框架的一些了解和基本用法
- WEB标准开发中的一些基本用法(连载中...)
- 关于DSP的一些基本知识和用法(资料来源于合众达网页)
- MongoDB基本介绍及一些用法
- struts2标签的一些基本用法
- Tensorflow一些常用基本概念与函数(3)
- ajax入门的一些基本用法
- c#中string的一些基本用法
- 关于setTimeout跟setinterval的一些基本的用法/小常识
- AJAX.JQ一些基本用法
- TensorFlow入门(一)基本用法
- CStdioFile 一些基本用法
- WEB标准开发中的一些基本用法
- 在C#应用程序中,对comboBox这个控件的一些基本用法(2009-06-10)
- tensorflow的一些基本函数