tensorflow使用张量时的一些注意点tf.concat、tf.reshape、tf.stack
2018-01-12 22:36
309 查看
有一段时间没用tensorflow了,现在跑实验还是存在一些坑了,主要是关于张量计算的问题。tensorflow升级1.0版本后与以前的版本并不兼容,可能出现各种奇奇怪怪的问题。
1 tf.concat函数
tensorflow1.0以前函数用法:tf.concat(concat_dim, values, name=’concat’),第一个参数为连接的维度,可以将几个向量按指定维度连接起来。如:
tf.concat的作用主要是将向量按指定维连起来,其余维度不变;而1.0版本以后,函数的用法变成:
位置变了,需要注意。
2 tf.stack函数
用法:stack(values, axis=0, name=”stack”):
“”“Stacks a list of rank-
tf.stack将一组R维张量变为R+1维张量。注意:tf.pack已经变成了tf.stack
3.tf.reshape
用法:reshape(tensor, shape, name=None):主要通过改变张量形状,可以从高维变低维,也可以从低维变高维;
1 tf.concat函数
tensorflow1.0以前函数用法:tf.concat(concat_dim, values, name=’concat’),第一个参数为连接的维度,可以将几个向量按指定维度连接起来。如:
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] #按照第0维连接 tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] #按照第1维连接 tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
tf.concat的作用主要是将向量按指定维连起来,其余维度不变;而1.0版本以后,函数的用法变成:
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] #按照第0维连接 tf.concat( [t1, t2],0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] #按照第1维连接 tf.concat([t1, t2],1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
位置变了,需要注意。
2 tf.stack函数
用法:stack(values, axis=0, name=”stack”):
“”“Stacks a list of rank-
Rtensors into one rank-
(R+1)tensor.
x = tf.constant([1, 4]) y = tf.constant([2, 5]) z = tf.constant([3, 6]) tf.stack([x,y,z]) ==> [[1,4],[2,5],[3,6]] tf.stack([x,y,z],axis=0) ==> [[1,4],[2,5],[3,6]] tf.stack([x,y,z],axis=1) ==> [[1, 2, 3], [4, 5, 6]]
tf.stack将一组R维张量变为R+1维张量。注意:tf.pack已经变成了tf.stack
3.tf.reshape
用法:reshape(tensor, shape, name=None):主要通过改变张量形状,可以从高维变低维,也可以从低维变高维;
a = tf.Variable(initial_value=[[1,2,3],[4,5,6]]) ==> shape:[2,3] b = tf.Variable(initial_value=[[[1,2,3],[4,5,6]],[[7,8,9],[1,0,2]]]) ==> shape:[2,2,3] a_1 = tf.reshape(a,[2,1,1,3]) ==> [[[[1,2,3]]],[[[4,5,6]]]] a_2 = tf.reshape(a,[2,1,3]) ==> [[[1,2,3]],[[4,5,6]]] b_1 = tf.reshape(b,[2,2,1,3]) ==> [[[[1,2,3]],[[4,5,6]]],[[[7,8,9]],[[1,0,2]]]] new_1 = tf.concat([b_1,a_1],1) new_2 = tf.reshape(tf.concat([b,a_2],1),[2,3,1,3]) """ new_1: [[[[1 2 3]] [[4 5 6]] [[1 2 3]]] [[[7 8 9]] [[1 0 2]] [[4 5 6]]]] new_2; [[[[1 2 3]] [[4 5 6]] [[1 2 3]]] [[[7 8 9]] [[1 0 2]] [[4 5 6]]]]
相关文章推荐
- tensorflow:使用tfrecords时的注意事项
- TensorFlow 介绍 tf.concat 的使用方法
- 180224 tensorflow之tf.reshape()与tf.concat()应用举例
- TensorFlow Lite demo——就是为嵌入式设备而存在的,底层调用NDK神经网络API,注意其使用的tf model需要转换下,同时提供java和C++ API,无法使用tflite的见后
- jstl 使用中的一些关键注意地方
- Vue使用的一些注意要点
- 关于U盾使用的一些注意事项
- Tensorflow中使用tfrecord方式读取数据
- [备忘]WCF中使用MessageContract的一些注意点
- css属性使用中的一些注意事项(实战经验)
- 声明方法java实际开发中泛型使用需要注意的一些问题
- Zepto 使用中的一些注意点
- python 使用plt的一些注意事项
- Labview使用DAQ数据采集的一些注意问题
- Zepto 使用中的一些注意点
- 针对raml的一些使用技巧和raml-jaxrs-maven-plugin插件的使用注意
- Block使用中的一些要注意的地方
- 使用supervisor的一些注意事项
- Android dp方式的屏幕适配工具使用(一些疑惑和注意事项)
- 使用Junit和mockito写单测的一些注意点