tf.concat&tf.gather&tf.gather

您所在的位置:网站首页 tensorflow常用函数笔记 tf.concat&tf.gather&tf.gather

tf.concat&tf.gather&tf.gather

2024-07-16 17:37:22| 来源: 网络整理| 查看: 265

Tensorflow常用函数笔记 tf.concat

把一组向量从某一维上拼接起来,很向numpy中的Concatenate,官网例子:

t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] # tensor t3 with shape [2, 3] # tensor t4 with shape [2, 3] tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3] tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6]123456789

其实,如果是list类型的话也是可以的,只要是形似Tensor,最后tf.concat返回的还是Tensor类型

tf.gather

类似于数组的索引,可以把向量中某些索引值提取出来,得到新的向量,适用于要提取的索引为不连续的情况。这个函数似乎只适合在一维的情况下使用。

import tensorflow as tf a = tf.Variable([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]]) index_a = tf.Variable([0,2]) b = tf.Variable([1,2,3,4,5,6,7,8,9,10]) index_b = tf.Variable([2,4,6,8]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(tf.gather(a, index_a))) print(sess.run(tf.gather(b, index_b))) # [[ 1 2 3 4 5] # [11 12 13 14 15]] # [3 5 7 9] 123456789101112131415161718 tf.gather_nd

同上,但允许在多维上进行索引,例子只展示了一种很简单的用法,更复杂的用法可见官网。

import tensorflow as tf a = tf.Variable([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]]) index_a = tf.Variable([[0,2], [0,4], [2,2]]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(tf.gather_nd(a, index_a))) # [ 3 5 13]12345678910 tf.greater

判断函数。首先张量x和张量y的尺寸要相同,输出的tf.greater(x, y)也是一个和x,y尺寸相同的张量。如果x的某个元素比y中对应位置的元素大,则tf.greater(x, y)对应位置返回True,否则返回False。与此类似的函数还有tf.greater_equal。

import tensorflow as tf x = tf.Variable([[1,2,3], [6,7,8], [11,12,13]]) y = tf.Variable([[0,1,2], [5,6,7], [10,11,12]]) x1 = tf.Variable([[1,2,3], [6,7,8], [11,12,13]]) y1 = tf.Variable([[10,1,2], [15,6,7], [10,21,12]]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(tf.greater(x, y))) print(sess.run(tf.greater(x1, y1))) # [[ True True True] # [ True True True] # [ True True True]] # [[False True True] # [False True True] # [ True False True]] 123456789101112131415161718192021 tf.cast

转换数据类型。

a = tf.constant([0, 2, 0, 4, 2, 2], dtype='int32') print(a) # b = tf.cast(a, 'float32') print(b) # 1234567 tf.expand_dims & tf.squeeze

增加 / 压缩张量的维度。

a = tf.constant([0, 2, 0, 4, 2, 2], dtype='int32') print(a) # b = tf.expand_dims(a, 0) print(b) # print(tf.squeeze(b, 0)) # 12345678910


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭