免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6486 | 回复: 3

训练模型时python报错--(expected unicode, got str) [复制链接]

论坛徽章:
0
发表于 2019-06-03 15:57 |显示全部楼层
  1. for k in range(num_epochs):
  2.     # 给数据洗个牌
  3.     index_shuf = [i for i in range(len(questions_train))]
  4.     shuffle(index_shuf)
  5.     # 一一取出 问题,答案,和图片
  6.     questions_train = [questions_train[i] for i in index_shuf]
  7.     answers_train = [answers_train[i] for i in index_shuf]
  8.     images_train = [images_train[i] for i in index_shuf]
  9.     # batch分组
  10.     for qu_batch,an_batch,im_batch in zip(grouper(questions_train, batch_size, fillvalue=questions_train[-1]),
  11.                                         grouper(answers_train, batch_size, fillvalue=answers_train[-1]),
  12.                                         grouper(images_train, batch_size, fillvalue=images_train[-1])):
  13.         X_q_batch = get_questions_matrix_sum(qu_batch, nlp)
  14.         X_i_batch = get_images_matrix(im_batch, id_map, VGGfeatures)
  15.         X_batch = np.hstack((X_q_batch, X_i_batch))
  16.         Y_batch = get_answers_matrix(an_batch, labelencoder)
  17.         loss = model.train_on_batch(X_batch, Y_batch)
  18.         progbar.add(batch_size, values=[("train loss", loss)])
  19.     # 并且告诉模型,隔多久,存一次模型,比如这里,model_save_interval是10
  20.     if k%model_save_interval == 0:
  21.         model.save_weights(model_file_name + '_epoch_{:02d}.hdf5'.format(k))
  22. # 把最终的模型也存下来
  23. model.save_weights(model_file_name + '_epoch_{:02d}.hdf5'.format(k))
复制代码
  1. ---------------------------------------------------------------------------
  2. TypeError                                 Traceback (most recent call last)
  3. <ipython-input-16-f74b00680e52> in <module>()
  4.      15                                         grouper(answers_train, batch_size, fillvalue=answers_train[-1]),
  5.      16                                         grouper(images_train, batch_size, fillvalue=images_train[-1])):
  6. ---> 17         X_q_batch = get_questions_matrix_sum(qu_batch, nlp)
  7.      18         X_i_batch = get_images_matrix(im_batch, id_map, VGGfeatures)
  8.      19         X_batch = np.hstack((X_q_batch, X_i_batch))

  9. <ipython-input-9-175d4cdea086> in get_questions_matrix_sum(questions, nlp)
  10.      11     # assert not isinstance(questions, basestring)
  11.      12     nb_samples = len(questions)
  12. ---> 13     word_vec_dim = nlp(questions[0])[0].vector.shape[0]
  13.      14     questions_matrix = np.zeros((nb_samples, word_vec_dim))
  14.      15     for i in range(len(questions)):

  15. /anaconda2/lib/python2.7/site-packages/spacy/language.pyc in __call__(self, text, disable)
  16.     338             raise ValueError(Errors.E088.format(length=len(text),
  17.     339                                                 max_length=self.max_length))
  18. --> 340         doc = self.make_doc(text)
  19.     341         for name, proc in self.pipeline:
  20.     342             if name in disable:

  21. /anaconda2/lib/python2.7/site-packages/spacy/language.pyc in make_doc(self, text)
  22.     370
  23.     371     def make_doc(self, text):
  24. --> 372         return self.tokenizer(text)
  25.     373
  26.     374     def update(self, docs, golds, drop=0., sgd=None, losses=None):

  27. TypeError: Argument 'string' has incorrect type (expected unicode, got str)
复制代码
环境:anaconda python:2.7
这个代码之前在python2的环境下是可以正常运行的,求助。
TypeError: Argument 'string' has incorrect type (expected unicode, got str)

论坛徽章:
0
发表于 2019-06-04 09:43 |显示全部楼层
这不是说了吗,期待是unicode,传入的是str

论坛徽章:
0
发表于 2019-06-04 16:56 |显示全部楼层
回复 2# dahe_1984

我想请问一下具体应该怎么改?谢谢了。

论坛徽章:
0
发表于 2019-06-04 19:29 |显示全部楼层
把你的样本文件也贴上来,谁有耐心猜测你的样本是啥
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP