raise TypeError(‘Inputs to a layer should be tensors. Got: %s‘ % (x,))

在使用Nezha Recognizer时,遇到TypeError,因为将非Tensor类型(财经新闻类别)误输入到模型。代码中model.predict()与model(x)的区别导致了输入规格不匹配。解决办法在于确保输入数据符合模型期望的Tensor格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

File "/home/server-train/data/nezha_recognizer.py", line 50, in predict_nezha
    scores = model(x)
  File "/root/anaconda3/envs/nlp/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1013, in __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
  File "/root/anaconda3/envs/nlp/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_spec.py", line 197, in assert_input_compatibility
    raise TypeError('Inputs to a layer should be tensors. Got: %s' % (x,))
TypeError: Inputs to a layer should be tensors. Got: 财经,金融,快讯,财经网,科技,汽车,资本,互联网,腾讯,杂志
Exception ignored in: <bound method _CheckpointRestoreCoordinatorDeleter.__del__ of <tensorflow.python.training.tracking.util._CheckpointRestoreCoordinatorDeleter object at 0x7f3a42786a20>>
Traceback (most recent call last):
  File "/root/anaconda3/envs/nlp/lib/python3.6/site-packages/tensorflow/python/training/tracking/util.py", line 162, in __del__
  File "/root/anaconda3/envs/nlp/lib/python3.6/site-packages/tensorflow/python/training/tracking/util.py", line 107, in node_names
TypeError: 'NoneType' object is not callable

原因:

调用模型应该用scores = model.predict(x)而不是用上面的model(x),这两个方法使用的类型不同,前者要求的是list(或ndarray  未再确认)后者要求是Tensor出现了上面的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值