Tensorflow学习笔记(一)
来源:互联网 发布:淘宝发送链接怎么发 编辑:程序博客网 时间:2024/05/17 22:46
batch_size = tf.size(labels)labels = tf.expand_dims(labels, 1)indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)concated = tf.concat(1, [indices, labels])onehot_labels = tf.sparse_to_dense( concated, tf.stack([batch_size, NUM_CLASSES]), 1.0, 0.0)
以上这段代码将类标识转换为onehot型的类标识。
假设batch_size大小为5,NUM_CLASSES为10。
初始的labels=[3,5,2,9,6]
则第二行代码后labels的shape变为(5,1)
labels=[[3],[5],[2],[9],[6]]
第三行产生的indices的shape也为(5,1)
indices=[[0],[1],[2],[3],[4]]
第四行产生的concated
concated=[[0,3],[1,5],[2,2],[3,9],[4,6]]
最后的onehot_labels
onehot_labels=[[0,0,0,1,0,0,0,0,0,0], [0,0,0,0,0,1,0,0,0,0], [0,0,1,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,1] [0,0,0,0,0,0,1,0,0,0]]
函数解释
tf.expand_dims(Tensor, dim)
在张量Tensor的第dim维插入一个维度。
sess = tf.InteractiveSession()labels = [1,2,3]x = tf.expand_dims(labels, 0)print(sess.run(x))x = tf.expand_dims(labels, 1)print(sess.run(x))#>>>[[1 2 3]]#>>>[[1]# [2]# [3]]
tf.concat(concat_dim, values, name=’concat’)
除去name参数用以指定该操作的name,与方法有关的一共两个参数:
第一个参数concat_dim:必须是一个数,表明在哪一维上连接
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat(0, [t1, t2]) == > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value, name=None)
除去name参数用以指定该操作的name,与方法有关的一共四个参数:
第一个参数sparse_indices:稀疏矩阵中那些个别元素对应的索引值。有三种情况:
sparse_indices是个数,那么它只能指定一维矩阵的某一个元素 sparse_indices是个向量,那么它可以指定一维矩阵的多个元素 sparse_indices是个矩阵,那么它可以指定二维矩阵的多个元素
第二个参数output_shape:输出的稀疏矩阵的shape
第三个参数sparse_values:个别元素的值。分为两种情况:
sparse_values是个数:所有索引指定的位置都用这个数 sparse_values是个向量:输出矩阵的某一行向量里某一行对应的数(所以这里向量的长度应该和输出矩阵的行数对应,不然报错)
第四个参数default_value:未指定元素的默认值,一般如果是稀疏矩阵的话就是0了
阅读全文
0 0
- TensorFlow学习笔记(一):TensorFlow安装
- tensorflow学习笔记(一):tensorflow安装
- TensorFlow学习笔记(一)
- tensorflow学习笔记(一)
- TensorFlow学习笔记(一)
- TensorFlow学习笔记(一)
- Tensorflow学习笔记(一)
- tensorflow学习笔记(一)
- TensorFlow学习笔记(一)
- TensorFlow学习笔记(一)
- Tensorflow学习笔记(一)
- TensorFlow学习笔记(一)
- TensorFlow学习笔记(一)
- TensorFlow学习笔记(一)
- tensorflow学习笔记一
- TensorFlow学习笔记(一):快速安装与使用TensorFlow
- TensorFlow学习笔记(一补):使用Anaconda安装TensorFlow
- TensorFlow学习笔记(一):快速安装与使用TensorFlow
- JAVAWEB 面试题
- 58. Length of Last Word
- 算法题--小易喜欢的单词
- grails3的elasticsearch插件问题
- Vue Study
- Tensorflow学习笔记(一)
- Linux学习笔记之进程管理
- immutable日常操作之深入API
- C++开发-绘制正弦曲线
- UICollectionView使用过程中的常见错误
- React渲染问题研究以及Immutable的应用
- 一道简单的递归题
- 函数的定义与调用&&参数数组
- 【NY520】最大素因子