有一段时间没用tensorflow了,现在跑实验还是存在一些坑了,主要是关于张量计算的问题。tensorflow升级1.0版本后与以前的版本并不兼容,可能出现各种奇奇怪怪的问题。
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-R tensors 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中的reshape(tensor,[1,-1])和reshape(tensor,[-1,1])
和python 中的reshape用法应该一样
import tensorflow as tf a = [[1,2],[3,4],[5,6]] tf.reshape(a,[-1,1]) Out[13]: <tf.Tensor 'Reshape_4:0' shape=(6, 1) dtype=int32> tf.reshape(tf.reshape(a,[-1,1]),[1,-1]) Out[14]: <tf.Tensor 'Reshape_6:0' shape=(1, 6) dtype=int32>
tf.reshape(tensor,[-1,1])将张量变为一维列向量
tf.reshape(tensor,[1,-1])将张量变为一维行向量
以上这篇浅谈tensorflow使用张量时的一些注意点tf.concat,tf.reshape,tf.stack就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]