， 如有侵权请联系删除，仅限于学术交流，请勿商用。如有谬误，请联系指出。

TensorFlow中的张量具有静态形状属性，该属性在图形构造期间确定。静态形状可能未指定。例如，我们可以定义一个形状张量[None，128]：

```import tensorflow as tf
a = tf.placeholder(tf.float32, [None, 128])```

`static_shape = a.shape.as_list()  # returns [None, 128]`

`dynamic_shape = tf.shape(a)`

```a.set_shape([32, 128])  # static shape of a is [32, 128]
a.set_shape([None, 128])  # first dimension of a is determined dynamically```

`a =  tf.reshape(a, [32, 128])`

```def get_shape(tensor):
static_shape = tensor.shape.as_list()
dynamic_shape = tf.unstack(tf.shape(tensor))
dims = [s[1] if s[0] is None else s[0]
for s in zip(static_shape, dynamic_shape)]
return dims```

```b = tf.placeholder(tf.float32, [None, 10, 32])
shape = get_shape(b)
b = tf.reshape(b, [shape[0], shape[1] * shape[2]])```

```import tensorflow as tf
import numpy as np
def reshape(tensor, dims_list):
shape = get_shape(tensor)
dims_prod = []
for dims in dims_list:
if isinstance(dims, int):
dims_prod.append(shape[dims])
elif all([isinstance(shape[d], int) for d in dims]):
dims_prod.append(np.prod([shape[d] for d in dims]))
else:
dims_prod.append(tf.prod([shape[d] for d in dims]))
tensor = tf.reshape(tensor, dims_prod)
return tensor```

```b = tf.placeholder(tf.float32, [None, 10, 32])
b = reshape(b, [0, [1, 2]])```