tensorflow expand_dims和squeeze
有时我们会碰到升维或降维的需求,比如现在有一个图像样本,形状是 [height, width, channels],我们需要把它输入到已经训练好的模型中做分类,而模型定义的输入变量是一个batch,即形状为 [batch_size, height, width, channels],这时就需要升维了。tensorflow提供了一个方便的升维函数:expand_dims,参数定义如下:
tf.expand_dims(input, axis=None, name=None, dim=None)
参数说明:
input:待升维的tensor
axis:插入新维度的索引位置
name:输出tensor名称
dim: 一般不用
import tensorflow as tf sess = tf.Session() t = tf.constant([1, 2, 3], dtype=tf.int32) t.get_shape()
# TensorShape([Dimension(3)]) tf.expand_dims(t, 0).get_shape()
# TensorShape([Dimension(1), Dimension(3)]) tf.expand_dims(t, 1).get_shape()
# TensorShape([Dimension(3), Dimension(1)])
squeeze正好执行相反的操作:删除大小是1的维度
tf.squeeze(input, squeeze_dims=None, name=None)
input: 待降维的张量
sequeeze_dims: list[int]类型,表示需要删除的维度索引。默认为[],即删除所以大小为1的维度
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
Or, to remove specific size 1 dimensions: # 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
在处理tensor的时候合理使用这两个函数,能极大的提高效率。例如处理输入样本、执行向量与矩阵的点乘等情况。
参考:https://blog.csdn.net/qq_31780525/article/details/72280284
tensorflow expand_dims和squeeze的更多相关文章
- tensorflow之tf.squeeze()
tf.squeeze()函数的作用是从tensor中删除所有大小(szie)是1的维度. 给定丈量输入, 此操作返回的是相同类型的张量, 并删除所有尺寸为1的维度.如果不想删除所有尺寸为1的维度, 可 ...
- Tensorflow从0到1(3)之实战传统机器算法
计算图中的操作 import numpy as np import tensorflow as tf sess = tf.Session() x_vals = np.array([1., 3., 5. ...
- TensorFlow机器学习实战指南之第二章
一.计算图中的操作 在这个例子中,我们将结合前面所学的知识,传入一个列表到计算图中的操作,并打印返回值: 声明张量和占位符.这里,创建一个numpy数组,传入计算图操作: import tensorf ...
- TF常用知识
命名空间及变量共享 # coding=utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt; ...
- tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask
1. tf.split(3, group, input) # 拆分函数 3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...
- tensorflow 笔记14:tf.expand_dims和tf.squeeze函数
tf.expand_dims和tf.squeeze函数 一.tf.expand_dims() Function tf.expand_dims(input, axis=None, name=None, ...
- tf.expand_dims和tf.squeeze函数
from http://blog.csdn.net/qq_31780525/article/details/72280284 tf.expand_dims() Function tf.expand_d ...
- (转)Image Segmentation with Tensorflow using CNNs and Conditional Random Fields
Daniil's blog Machine Learning and Computer Vision artisan. About/ Blog/ Image Segmentation with Ten ...
- TensorFlow和最近发布的slim
笔者将和大家分享一个结合了TensorFlow和最近发布的slim库的小应用,来实现图像分类.图像标注以及图像分割的任务,围绕着slim展开,包括其理论知识和应用场景. 之前自己尝试过许多其它的库,比 ...
随机推荐
- Webpack之optimization.splitChunks代码分割插件的配置
SplitChunkPlugin插件配置参数详解 对引入的库代码(例如:lodash.jQuery等)进行代码的分割进行优化 若配置时只写chunks:"all",其余则为默认配置 ...
- mac item2的快捷键
https://juejin.im/entry/58fac23fb123db4449071c99 听说这个工具可以解决,iterm2的整句翻译的问题.一致找不到免费的破解版本. Myna for Go ...
- AVR单片机教程——示波器
本文隶属于AVR单片机教程系列. 在用DAC做了一个稍大的项目之后,我们来拿ADC开开刀.在本讲中,我们将了解0.96寸OLED屏,移植著名的U8g2库到我们的开发板上,学习在屏幕上画直线的算法, ...
- Charles证书下载报Name lookup failed
背景 1.charles https抓包需要在手机上安装证书. 2.近期疫情影响,通过openVPN远程办公. 问题现象 按照网上教程配置好charles proxy配置,证书安装的时候,iPhone ...
- findContours()函数
函数原型 findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int m ...
- 折腾vue--环境搭建(一)
1.安装nodejs nodejs下载地址:https://nodejs.org/en/ 2.检测nodejs //检测nodejs版本 node -v //检测npm npm –v 3.安装vue ...
- UESTC 1324 卿学姐与公主 分块板子
#include<iostream> #include<cmath> using namespace std; ; //表示当前数在哪一块里面 int belong[maxn] ...
- 快速读写模板(int)
一.快速读入模板(int) inline int read(int x){ char ch=getchar(); int x=0,f=1; while(ch>='9'||ch<='0'){ ...
- C# 截取屏幕
/// <summary> /// 截取屏幕 /// </summary> /// <param name="x">起点X坐标</para ...
- AQS源码分析总结
AQS是并发编程的一个最基本组件,是一个抽象同步器. 网上有很多详细介绍AQS的博文,在这里我就不仔细介绍了,主要写一些重要的内容. AQS中重要的几个属性: //同步队列的头节点 private t ...