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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。

Traceback (most recent call last): File "train.py", line 185, in <module> train() File "train.py", line 150, in train trainer.start() File "/home/nvidia/chenboln/4HDR-GAN-master/tensorkit/train.py", line 302, in start self._train_loop(self._sess) File "/home/nvidia/chenboln/4HDR-GAN-master/tensorkit/train.py", line 222, in _train_loop i(sess) File "train.py", line 132, in restore Restore().init(ckpt_dir=log_dir, ckpt_file=cf, optimistic=True).restore(sess) File "/home/nvidia/chenboln/4HDR-GAN-master/tensorkit/restore.py", line 39, in restore if self._restore_vars(sess): File "/home/nvidia/chenboln/4HDR-GAN-master/tensorkit/restore.py", line 58, in _restore_vars return self._optimistic_restore_model(sess) File "/home/nvidia/chenboln/4HDR-GAN-master/tensorkit/restore.py", line 69, in _optimistic_restore_model reader = tf.train.NewCheckpointReader(self.restore_ckpt_file) File "/home/nvidia/anaconda3/envs/pytorch1/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 636, in NewCheckpointReader return CheckpointReader(compat.as_bytes(filepattern)) File "/home/nvidia/anaconda3/envs/pytorch1/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 648, in __init__ this = _pywrap_tensorflow_internal.new_CheckpointReader(filename) tensorflow.python.framework.errors_impl.DataLossError: Unable to open table file ./logs/2025.03.13_16.11.57_56028_unetpps_sphere_sn_lsTP: Failed precondition: logs/2025.03.13_16.11.57_56028_unetpps_sphere_sn_lsTP; Is a directory: perhaps your file is in a different file format and you need to use a different restore operator?
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值