Cannot convert a symbolic Tensor to a numpy array错误的解决

最近写代码的时候发生了一件奇怪的错误

 NotImplementedError: Cannot convert a symbolic Tensor (bert/encoder/layer_0/attention/self/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

这里的源代码内容是这样的

sh = self.get_shape_list(from_tensor)
#data = sh[:2]
mask = tf.ones(sh[:2],dtype=tf.int32)

其中原先的sh内容为:

sh = [,128,768]

查看对应的get_shape_list的相应源码

@staticmethod
def get_shape_list(tensor):
    """ Tries to return the static shape as a list
    falling back to dynamic shape per dimension. """
    static_shape, dyn_shape = tensor.shape.as_list(), tf.shape(tensor)
    def shape_dim(ndx):
        return dyn_shape[ndx] if static_shape[ndx] is None else static_shape[ndx]

这里面本身调用的内容为

static_shape,dyn_shape = tensor.shape_aslist(),tf.shape(tensor)

此时这里调用出来的内容为

static_shape = [None,128,768]
dyn_shape = Tensor("bert/encoder/layer_0/attention/self/Shape:0",shape=(3,),dtype=int32)

然后进行调用shape_dim函数

def shape_dim(ndx):
    return dyn_shape[ndx] if static_shape[ndx] is None else static_shape[ndx]

这里的操作是分析对应的static_shape数组内容,对于第一个数值为None的时候调用dyn_shape中的第一个值,所以返回的值为


后面两个对应的值不为None,所以调用对应的128和768,因此最后返回的数组为

sh = [,128,768]

然后此时将前两维度作为ones数组的形状调用进入会发生报错

mask = tf.ones(sh[:2],dtype=tf.int32)

将sh的前两维数组放入进去之后,会报以下的错误

 NotImplementedError: Cannot convert a symbolic Tensor (bert/encoder/layer_0/attention/self/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

这个时候我想到另辟蹊径,直接放入对应的dyn_shape的tensor类型的数组,直接解决了这个对应的问题
dyn_shape的对应数值为

dyn_shape = Tensor("bert/encoder/layer_0/attention/self/Shape:0",shape=(3,),dtype=int32)

你可能感兴趣的:(bert源码解读)