Python Keras预测抛出';类型错误:ufunc';添加';未包含具有签名匹配类型dtype(';<;U4';)dtype(';<;U4';)dtype(';<;U4';)和';)的循环;

Python Keras预测抛出';类型错误:ufunc';添加';未包含具有签名匹配类型dtype(';<;U4';)dtype(';<;U4';)dtype(';<;U4';)和';)的循环;,python,keras,elmo,Python,Keras,Elmo,我构建了一个插槽填充(一种序列分类)模型,其结构为:custom ELMo embeddings layer-BiLSTM-CRF 它训练得很好。但根据预测,我得到: 'TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U4') dtype('<U4') dtype('<U4')'. 这是为了避免模型构建过程中出现错误,即:str没有属性ndim。如果不将测试

我构建了一个插槽填充(一种序列分类)模型,其结构为:custom ELMo embeddings layer-BiLSTM-CRF

它训练得很好。但根据预测,我得到:

'TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U4') dtype('<U4') dtype('<U4')'.
这是为了避免模型构建过程中出现错误,即:str没有属性ndim。如果不将测试数据转换为np.str,我会再次出现此错误

一位同事认为问题在于Keras深处的add方法(参见错误)。显然,这是一种处理无符号整数的特殊add方法,它不应该像现在这样引起问题

自定义层松散地基于

为了重现错误:我用代码和一些虚拟数据建立了一个github存储库

完全错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-f71c3fcdc6d2> in <module>
     16 print(type(X_train_sents[0]))
     17 print(type(X_test_sents[0]))
---> 18 test_pred = model.predict(X_test_sents, y_test)

~/.conda/envs/base_munroke/lib/python3.7/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
   1167                                             batch_size=batch_size,
   1168                                             verbose=verbose,
-> 1169                                             steps=steps)
   1170 
   1171     def train_on_batch(self, x, y,

~/.conda/envs/base_munroke/lib/python3.7/site-packages/keras/engine/training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps)
    280         # Sample-based predictions.
    281         outs = []
--> 282         batches = make_batches(num_samples, batch_size)
    283         index_array = np.arange(num_samples)
    284         for batch_index, (batch_start, batch_end) in enumerate(batches):

~/.conda/envs/base_munroke/lib/python3.7/site-packages/keras/engine/training_utils.py in make_batches(size, batch_size)
    367         A list of tuples of array indices.
    368     """
--> 369     num_batches = (size + batch_size - 1) // batch_size  # round up
    370     return [(i * batch_size, min(size, (i + 1) * batch_size))
    371             for i in range(num_batches)]

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U4') dtype('<U4') dtype('<U4')
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
16打印(类型(X列发送[0]))
17打印(类型(X测试[0]))
--->18测试预测=模型预测(X测试,y测试)
预测中的~/.conda/envs/base\u munroke/lib/python3.7/site-packages/keras/engine/training.py(self,x,批量大小,详细,步骤)
1167批次大小=批次大小,
1168 verbose=详细,
->1169步=步)
1170
1171 def系列在批次上(自身、x、y、,
预测循环中的~/.conda/envs/base\u munroke/lib/python3.7/site-packages/keras/engine/training\u arrays.py(模型、f、ins、批量大小、详细程度、步骤)
280#基于样本的预测。
281出局=[]
-->282批次=制造批次(样本数量、批次大小)
283索引数组=np.arange(样本数)
284对于枚举(批次)中的批次索引,(批次开始,批次结束):
生产批次中的~/.conda/envs/base\u munroke/lib/python3.7/site-packages/keras/engine/training\u utils.py(大小、批次大小)
367数组索引的元组列表。
368     """
-->369个批次=(大小+批次大小-1)//批次大小#四舍五入
370返回[(i*批量大小,最小值(大小,(i+1)*批量大小))
371适用于范围内的i(数量批次)]

TypeError:ufunc“add”不包含签名类型与dtype匹配的循环(“Update,我找到了解决方案:我将y集转换为np.str是错误的,例如

y\u-train=np.array(y\u-train,dtype=np.str)

我确实需要将y集转换为数组,但在该行中添加
dtype=np.str
只是一个疏忽


我希望我的痛苦能给别人带来一些帮助!:)

更新,我找到了解决方案:我把y集转换成np.str是错误的,例如

y\u-train=np.array(y\u-train,dtype=np.str)

我确实需要将y集转换为数组,但在该行中添加
dtype=np.str
只是一个疏忽

我希望我数小时的痛苦能给别人带来一些益处!:)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-f71c3fcdc6d2> in <module>
     16 print(type(X_train_sents[0]))
     17 print(type(X_test_sents[0]))
---> 18 test_pred = model.predict(X_test_sents, y_test)

~/.conda/envs/base_munroke/lib/python3.7/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
   1167                                             batch_size=batch_size,
   1168                                             verbose=verbose,
-> 1169                                             steps=steps)
   1170 
   1171     def train_on_batch(self, x, y,

~/.conda/envs/base_munroke/lib/python3.7/site-packages/keras/engine/training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps)
    280         # Sample-based predictions.
    281         outs = []
--> 282         batches = make_batches(num_samples, batch_size)
    283         index_array = np.arange(num_samples)
    284         for batch_index, (batch_start, batch_end) in enumerate(batches):

~/.conda/envs/base_munroke/lib/python3.7/site-packages/keras/engine/training_utils.py in make_batches(size, batch_size)
    367         A list of tuples of array indices.
    368     """
--> 369     num_batches = (size + batch_size - 1) // batch_size  # round up
    370     return [(i * batch_size, min(size, (i + 1) * batch_size))
    371             for i in range(num_batches)]

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U4') dtype('<U4') dtype('<U4')