tensorflow学习之路(4):tf.shape(xxx) vs. xxx.get_shape()

本文对比了tf.shape()和.get_shape()两种获取Tensor尺寸的方法。tf.shape()适用于tensor、list及array,返回int32类型的张量;.get_shape()仅适用于tensor,并返回tuple。通过示例说明了两者的使用场景及区别。

tf.shape(xxx) 和 xxx.get_shape()比较

  1. 相同点:都可以得到tensor xxx 的尺寸
  2. 不同点:tf.shape(xxx)中xxx数据的类型可以是tensor,list,array;而xxx.get_shape()中的xxx的数据类型必须是tensor,且返回的是一个tuple.可以通过xxx.get_shape().as_list()得到一个list。

例如:

x= tf.truncated_normal([32, 32, 3], dtype=tf.float32)

print(tf.shape(x))
print(x.get_shape())
print(x.get_shape().as_list())

输出:

Tensor("Shape:0", shape=(3,), dtype=int32)
(32, 32, 3)
[32, 32, 3]

注意:dtype=int32是tf.shape()这个op的输出类型,默认为tf.int32。

D:\Anaconda\envs\uuuunet\python.exe F:/shangke/UNet/UNet.py Using TensorFlow backend. 2025-11-06 20:07:58.414783: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll 2025-11-06 20:07:59.905524: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2025-11-06 20:07:59.908291: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll 2025-11-06 20:07:59.934409: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties: name: NVIDIA GeForce RTX 2080 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.545 pciBusID: 0000:01:00.0 2025-11-06 20:07:59.934644: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll 2025-11-06 20:07:59.937315: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll 2025-11-06 20:07:59.939831: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll 2025-11-06 20:07:59.940858: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll 2025-11-06 20:07:59.944721: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll 2025-11-06 20:07:59.947265: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll 2025-11-06 20:07:59.948591: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found 2025-11-06 20:07:59.948883: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1641] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... 2025-11-06 20:08:00.084896: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix: 2025-11-06 20:08:00.085054: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0 2025-11-06 20:08:00.085148: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N WARNING:tensorflow:From D:\Anaconda\envs\uuuunet\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers. WARNING:tensorflow:From D:\Anaconda\envs\uuuunet\lib\site-packages\keras\backend\tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead. F:/shangke/UNet/UNet.py:166: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., outputs=Tensor("co...)` model = Model(input= inputs,output = conv7) Model: "model_1" __________________________________________________________________________________________________ Layer (type) Output Shape Param # Connected to ================================================================================================== input_1 (InputLayer) (None, 128, 128, 1) 0 __________________________________________________________________________________________________ conv2d_1 (Conv2D) (None, 128, 128, 64) 640 input_1[0][0] __________________________________________________________________________________________________ dropout_1 (Dropout) (None, 128, 128, 64) 0 conv2d_1[0][0] __________________________________________________________________________________________________ conv2d_2 (Conv2D) (None, 128, 128, 64) 36928 dropout_1[0][0] __________________________________________________________________________________________________ max_pooling2d_1 (MaxPooling2D) (None, 64, 64, 64) 0 conv2d_2[0][0] __________________________________________________________________________________________________ conv2d_3 (Conv2D) (None, 64, 64, 128) 73856 max_pooling2d_1[0][0] __________________________________________________________________________________________________ dropout_2 (Dropout) (None, 64, 64, 128) 0 conv2d_3[0][0] __________________________________________________________________________________________________ conv2d_4 (Conv2D) (None, 64, 64, 128) 147584 dropout_2[0][0] __________________________________________________________________________________________________ max_pooling2d_2 (MaxPooling2D) (None, 32, 32, 128) 0 conv2d_4[0][0] __________________________________________________________________________________________________ conv2d_5 (Conv2D) (None, 32, 32, 256) 295168 max_pooling2d_2[0][0] __________________________________________________________________________________________________ dropout_3 (Dropout) (None, 32, 32, 256) 0 conv2d_5[0][0] __________________________________________________________________________________________________ conv2d_6 (Conv2D) (None, 32, 32, 256) 590080 dropout_3[0][0] __________________________________________________________________________________________________ max_pooling2d_3 (MaxPooling2D) (None, 16, 16, 256) 0 conv2d_6[0][0] __________________________________________________________________________________________________ conv2d_7 (Conv2D) (None, 16, 16, 512) 1180160 max_pooling2d_3[0][0] __________________________________________________________________________________________________ dropout_4 (Dropout) (None, 16, 16, 512) 0 conv2d_7[0][0] __________________________________________________________________________________________________ conv2d_8 (Conv2D) (None, 16, 16, 512) 2359808 dropout_4[0][0] __________________________________________________________________________________________________ max_pooling2d_4 (MaxPooling2D) (None, 8, 8, 512) 0 conv2d_8[0][0] __________________________________________________________________________________________________ conv2d_9 (Conv2D) (None, 8, 8, 1024) 4719616 max_pooling2d_4[0][0] __________________________________________________________________________________________________ dropout_5 (Dropout) (None, 8, 8, 1024) 0 conv2d_9[0][0] __________________________________________________________________________________________________ conv2d_10 (Conv2D) (None, 8, 8, 1024) 9438208 dropout_5[0][0] __________________________________________________________________________________________________ up_sampling2d_1 (UpSampling2D) (None, 16, 16, 1024) 0 conv2d_10[0][0] __________________________________________________________________________________________________ concatenate_1 (Concatenate) (None, 16, 16, 1536) 0 conv2d_8[0][0] up_sampling2d_1[0][0] __________________________________________________________________________________________________ conv2d_11 (Conv2D) (None, 16, 16, 512) 7078400 concatenate_1[0][0] __________________________________________________________________________________________________ dropout_6 (Dropout) (None, 16, 16, 512) 0 conv2d_11[0][0] __________________________________________________________________________________________________ conv2d_12 (Conv2D) (None, 16, 16, 512) 2359808 dropout_6[0][0] __________________________________________________________________________________________________ up_sampling2d_2 (UpSampling2D) (None, 32, 32, 512) 0 conv2d_12[0][0] __________________________________________________________________________________________________ concatenate_2 (Concatenate) (None, 32, 32, 768) 0 conv2d_6[0][0] up_sampling2d_2[0][0] __________________________________________________________________________________________________ conv2d_13 (Conv2D) (None, 32, 32, 256) 1769728 concatenate_2[0][0] __________________________________________________________________________________________________ dropout_7 (Dropout) (None, 32, 32, 256) 0 conv2d_13[0][0] __________________________________________________________________________________________________ conv2d_14 (Conv2D) (None, 32, 32, 256) 590080 dropout_7[0][0] __________________________________________________________________________________________________ up_sampling2d_3 (UpSampling2D) (None, 64, 64, 256) 0 conv2d_14[0][0] __________________________________________________________________________________________________ concatenate_3 (Concatenate) (None, 64, 64, 384) 0 conv2d_4[0][0] up_sampling2d_3[0][0] __________________________________________________________________________________________________ conv2d_15 (Conv2D) (None, 64, 64, 128) 442496 concatenate_3[0][0] __________________________________________________________________________________________________ dropout_8 (Dropout) (None, 64, 64, 128) 0 conv2d_15[0][0] __________________________________________________________________________________________________ conv2d_16 (Conv2D) (None, 64, 64, 128) 147584 dropout_8[0][0] __________________________________________________________________________________________________ up_sampling2d_4 (UpSampling2D) (None, 128, 128, 128 0 conv2d_16[0][0] __________________________________________________________________________________________________ concatenate_4 (Concatenate) (None, 128, 128, 192 0 conv2d_2[0][0] up_sampling2d_4[0][0] __________________________________________________________________________________________________ conv2d_17 (Conv2D) (None, 128, 128, 64) 110656 concatenate_4[0][0] __________________________________________________________________________________________________ dropout_9 (Dropout) (None, 128, 128, 64) 0 conv2d_17[0][0] __________________________________________________________________________________________________ conv2d_18 (Conv2D) (None, 128, 128, 64) 36928 dropout_9[0][0] __________________________________________________________________________________________________ conv2d_19 (Conv2D) (None, 128, 128, 8) 520 conv2d_18[0][0] ================================================================================================== Total params: 31,378,248 Trainable params: 31,378,248 Non-trainable params: 0 __________________________________________________________________________________________________ WARNING:tensorflow:From D:\Anaconda\envs\uuuunet\lib\site-packages\tensorflow_core\python\ops\math_grad.py:1424: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.where in 2.0, which has the same broadcast rule as np.where WARNING:tensorflow:From D:\Anaconda\envs\uuuunet\lib\site-packages\keras\backend\tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead. WARNING:tensorflow:From D:\Anaconda\envs\uuuunet\lib\site-packages\keras\callbacks\tensorboard_v1.py:200: The name tf.summary.merge_all is deprecated. Please use tf.compat.v1.summary.merge_all instead. WARNING:tensorflow:From D:\Anaconda\envs\uuuunet\lib\site-packages\keras\callbacks\tensorboard_v1.py:203: The name tf.summary.FileWriter is deprecated. Please use tf.compat.v1.summary.FileWriter instead. Found 130 images belonging to 1 classes. Found 1040 images belonging to 1 classes. Epoch 1/100 Found 130 images belonging to 1 classes. Found 1040 images belonging to 1 classes. 1/65 [..............................] - ETA: 9:24 - loss: 4.0354 - acc: 0.1175 2/65 [..............................] - ETA: 8:47 - loss: 3.6455 - acc: 0.1188 3/65 [>.............................] - ETA: 8:33 - loss: 3.3622 - acc: 0.1159 4/65 [>.............................] - ETA: 8:25 - loss: 3.1451 - acc: 0.1185 使用gpu了吗
最新发布
11-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值