在定义/编译输入形状为“None”的神经网络模型(例如,(None, 128, 128, 128, 1))时,可以实现 tf.reshape() 吗?

问题描述 投票:0回答:1

我正在张量流上构建深度学习模型,并需要在运行时重塑某个层的输入。 部分代码定义如下:

``` init = RandomNormal(stddev=0.02, seed=42)
``` shape_= [layer_in.shape[0], layer_in.shape[1], layer_in.shape[2], layer_in.shape[3]*layer_in.shape[4]]
``` shape_t = tf.convert_to_tensor(shape_)
``` layer_in = tf.reshape(layer_in, shape=shape_t)
``` g = Conv3D(n_filters, (4,4,4), strides=(2,2,2), padding='same', kernel_initializer=init)(layer_in)
``` g = LeakyReLU(alpha=0.2)(g)
``` return g

However, after running the code, it shows an error: 
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

When the error occurs, the shape of layer_in is (None, 128, 128, 128, 1) 
Also checked tf.reshape(), the size parameter has to be a tensor so it seems it doesn't work to run tf.reshape() in this case.
How can it be resolved? 
python tensorflow deep-learning tensorflow2.0 generative-adversarial-network
1个回答
0
投票

出现错误是因为layer_in.shape[0]为None,表示批量大小未知。 TensorFlow 需要 tf.reshape 中的形状参数为张量,但 None 无法转换。您可以考虑使用不同的选项来解决此问题:

解决方案:使用-1在运行时推断批次维度:

shape_ = [-1, layer_in.shape[1], layer_in.shape[2], layer_in.shape[3] * layer_in.shape[4]]

使用 tf.shape 进行动态尺寸:

对于完全动态的形状,请使用 tf.shape(layer_in) 在运行时检索尺寸:

shape_ = tf.shape(layer_in)
shape_ = tf.concat([shape_[:1], shape_[1:3], [shape_[3] * shape_[4]]], axis=0)

现在您可以使用构造的形状张量安全地重塑layer_in:

layer_in = tf.reshape(layer_in, shape=shape_)

最终版本应该是这样的:

init = RandomNormal(stddev=0.02, seed=42)
shape_ = [-1, layer_in.shape[1], layer_in.shape[2], layer_in.shape[3] * layer_in.shape[4]]  # Use -1 for batch dimension

layer_in = tf.reshape(layer_in, shape=shape_)
g = Conv3D(n_filters, (4, 4, 4), strides=(2, 2, 2), padding='same', kernel_initializer=init)(layer_in)
g = LeakyReLU(alpha=0.2)(g)

return g

如果您正在使用即时执行,则可能不需要显式地将形状转换为张量。 对于更复杂的动态重塑,请探索 tf.keras.layers.Reshape 层以获得清晰度和潜在的性能优势。

© www.soinside.com 2019 - 2024. All rights reserved.