gpt4 book ai didi

TensorFlow 重复函数失败,出现 ValueError : None values not supported

转载 作者:行者123 更新时间:2023-12-04 02:38:35 63 4
gpt4 key购买 nike

我已经实现了以下自定义 Layer,它根据输入 x 的大小在调用时修改可学习参数 seed_vectors 的大小,使用函数repeat

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow import repeat
from tensorflow.keras.layers import LayerNormalization


class PoolingMultiHeadAttention(tf.keras.layers.Layer):

def __init__(self, d, k, h):
"""
Arguments:
d: an integer, input dimension.
k: an integer, number of seed vectors.
h: an integer, number of heads.
"""
super(PoolingMultiHeadAttention, self).__init__()
self.seed_vectors = self.add_weight(initializer='uniform',
shape=(1, k, d),
trainable=True)

def call(self, z):
"""
Arguments:
z: a float tensor with shape [b, n, d].
Returns:
a float tensor with shape [b, k, d]
"""
b = z.shape[0]
s = self.seed_vectors
s = repeat(s, (b), axis=0, name='rep') # shape [b, k, d]
return s*z


# Dimensionality test
z = tf.random.normal(shape=(10, 2, 9))
pma = PoolingMultiHeadAttention(d=9, k=2, h=3)
pma(z)

我已经在单元测试中测试了维度输入/输出,它工作正常,但不幸的是,如果我在模型中使用这个层,它会失败并出现错误:


<ipython-input-4-89023d123369>:110 call *
s = repeat(s, (b), axis=0, name='rep') # shape [b, k, d]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:5616 repeat **
return repeat_with_axis(input, repeats, axis, name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:5478 repeat_with_axis
repeats = convert_to_int_tensor(repeats, name="repeats")
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:5388 convert_to_int_tensor
tensor = ops.convert_to_tensor(tensor, name=name, preferred_dtype=dtype)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1341 convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:317 _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:258 constant
allow_broadcast=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:296 _constant_impl
allow_broadcast=allow_broadcast))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:439 make_tensor_proto
raise ValueError("None values not supported.")

ValueError: None values not supported.

此错误似乎与缺少输出(或输出为无)有关[我知道情况并非如此,因为我已经在急切模式下测试了该功能并且它有效]或出于某种原因反向传播没有使用此操作(重复)。我不知道有任何其他方法可以在运行时修改该参数的大小+(几乎)相同的代码使用 Pytorch 可以正常工作(https://github.com/TropComplique/set-transformer/blob/master/blocks.py)谢谢

最佳答案

修复应该非常简单:改用 b = tf.shape(z)[0]。说明:

问题是您正在尝试重复 b 次,这(我想)是可变的批量大小。当不以急切模式运行时,这由形状中的值 None 表示。因此,您试图重复导致崩溃的“无时间”。

重要的是 Tensor.shape 返回张量的 static 形状,即编译时已知的任何形状。如上所述,对于未知维度,这包括 None
tf.shape(tensor) 而是返回动态 形状,即仅在模型运行时才对其进行评估。此时,批处理大小当然是已知的(因为您将一些东西放入模型中),因此这将是一个可以放入 repeat 的具体值,而不是 None 我们在上面得到了。

关于TensorFlow 重复函数失败,出现 ValueError : None values not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60484479/

63 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com