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 = [<tf.Tensor.'bert/encoder/layer_0/attention/self/strided_slice:0',shape = (),dtype=int32>,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中的第一个值,所以返回的值为

<tf.Tensor 'strided_slice:0' shape=() dtype = int32>

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

sh = [<tf.Tensor 'strided_slice:0' shape=() dtype=int32>,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)
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值