tf.squeeze
https://github.com/tensorflow/docs/tree/r1.4/site/en/api_docs/api_docs/python/tf
site/en/api_docs/api_docs/python/tf/squeeze.md
squeeze(
input,
axis=None,
name=None,
squeeze_dims=None
)
Defined in tensorflow/python/ops/array_ops.py
.
See the guide: Tensor Transformations > Shapes and Shaping
Removes dimensions of size 1 from the shape of a tensor.
从张量形状中移除大小为 1 的维度。
Given a tensor input
, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don’t want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying axis
.
给定一个张量 input
,该操作返回一个与已经移除的所有大小为 1 的维度具有相同类型的张量。如果您不想删除所有大小为 1 的维度,则可以通过指定 axis
来删除特定的大小为 1 的维度。
For example:
注意是 shape。
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t)) # [2, 3]
Or, to remove specific size 1 dimensions:
或者,要删除特定的大小为 1 的维度:
注意是 shape。
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t, [2, 4])) # [1, 2, 3, 1]
1. Args
input
: ATensor
. Theinput
to squeeze.axis
: An optional list ofints
. Defaults to[]
. If specified, only squeezes the dimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. Must be in the range[-rank(input), rank(input))
. (如果指定,只能挤压列出的维度。维度索引从 0 开始。压缩非 1 的维度是错误的。必须在范围内[-rank(input), rank(input))
。)name
: A name for the operation (optional). (操作的名称 (可选))squeeze_dims
: Deprecated keyword argument that is now axis.
2. Returns
A Tensor
. Has the same type as input
.
Contains the same data as input
, but has one or more dimensions of size 1 removed.
包含与 input
相同的数据,但删除一个或多个尺寸为 1。
##3. Raises
ValueError
: When bothsqueeze_dims
andaxis
are specified.
squeeze [skwiːz]:v. 挤,紧握,勒索,压榨,使挤进,(非正式) 向...施加压力,(尤于金融或商业) 破坏 n. 挤压,榨出的液体,压榨,紧握,拥挤,佣金,(可获得的钱、工作岗位等的) 减少,(非正式) 女朋友,男朋友,拥挤的社交集会,压模,(非正式) 勒索的钱,(桥牌) 紧逼打法,(棒球) 抢分战术
4. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
matrix_data = tf.Variable(tf.random_normal(shape=(1, 3, 2)))
squeezed_data = tf.squeeze(matrix_data)
matrix_data_shape = tf.shape(matrix_data)
squeezed_data_shape = tf.shape(squeezed_data)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
squeezed_data_shape_, matrix_data_shape_, squeezed_data_, matrix_data_ = sess.run(
[squeezed_data_shape, matrix_data_shape, squeezed_data, matrix_data])
print("matrix_data:\n", matrix_data_)
print("squeezed_data:\n", squeezed_data_)
print("matrix_data_shape:\n", matrix_data_shape_)
print("squeezed_data_shape:\n", squeezed_data_shape_)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-15 17:47:22.155174: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-15 17:47:22.237386: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-15 17:47:22.237628: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.43GiB
2019-08-15 17:47:22.237639: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
matrix_data:
[[[ 0.52290684 -0.39370742]
[-1.131437 -0.29959333]
[-0.52568823 0.317227 ]]]
squeezed_data:
[[ 0.52290684 -0.39370742]
[-1.131437 -0.29959333]
[-0.52568823 0.317227 ]]
matrix_data_shape:
[1 3 2]
squeezed_data_shape:
[3 2]
Process finished with exit code 0