write() argument must be str, not bytes を解決したい
興味のある論文の公開されているソースコードを実行しようとしたところwrite() argument must be str, not bytes のエラーに悩まされており,解決できそうにないため,お力を貸していただきたいです.
あまり,新しい論文ではないため,python2系で書かれていたコードをpython3で書き換えている途中です.
ぜひ,よろしくお願いします.
以下がそのコードです。
発生している問題・エラーメッセージ
TypeError Traceback (most recent call last) <ipython-input-20-4999cc12e722> in <module> 78 f = open('../data/FB15k_entity2idx.pkl', 'w') 79 g = open('../data/FB15k_idx2entity.pkl', 'w') ---> 80 cPickle.dump(entity2idx, f, -1) 81 cPickle.dump(idx2entity, g, -1) 82 f.close() TypeError: write() argument must be str, not bytes
該当のソースコード
python
1import os, sys 2#import cPickle 3import _pickle as cPickle 4 5import numpy as np 6import scipy.sparse as sp 7 8# Put the freebase15k data absolute path here 9datapath = "D:/Downloads/dataset/" 10assert datapath is not None 11 12if 'data' not in os.listdir('../'): 13 os.mkdir('../data') 14 15 16def parseline(line): 17 lhs, rel, rhs = line.split('\t') 18 lhs = lhs.split(' ') 19 rhs = rhs.split(' ') 20 rel = rel.split(' ') 21 return lhs, rel, rhs 22 23################################################# 24### Creation of the entities/indices dictionnaries 25 26np.random.seed(753) 27 28entleftlist = [] 29entrightlist = [] 30rellist = [] 31 32for datatyp in ['train']: 33 f = open(datapath + 'freebase_mtr100_mte100-%s.txt' % datatyp, 'r') 34 dat = f.readlines() 35 f.close() 36 for i in dat: 37 lhs, rel, rhs = parseline(i[:-1]) 38 entleftlist += [lhs[0]] 39 entrightlist += [rhs[0]] 40 rellist += [rel[0]] 41 42entleftset = np.sort(list(set(entleftlist) - set(entrightlist))) 43entsharedset = np.sort(list(set(entleftlist) & set(entrightlist))) 44entrightset = np.sort(list(set(entrightlist) - set(entleftlist))) 45relset = np.sort(list(set(rellist))) 46 47entity2idx = {} 48idx2entity = {} 49 50 51# we keep the entities specific to one side of the triplets contiguous 52idx = 0 53for i in entrightset: 54 entity2idx[i] = idx 55 idx2entity[idx] = i 56 idx += 1 57nbright = idx 58for i in entsharedset: 59 entity2idx[i] = idx 60 idx2entity[idx] = i 61 idx += 1 62nbshared = idx - nbright 63for i in entleftset: 64 entity2idx[i] = idx 65 idx2entity[idx] = i 66 idx += 1 67nbleft = idx - (nbshared + nbright) 68 69print ("# of only_left/shared/only_right entities: ", nbleft, '/', nbshared, '/', nbright) 70# add relations at the end of the dictionary 71for i in relset: 72 entity2idx[i] = idx 73 idx2entity[idx] = i 74 idx += 1 75nbrel = idx - (nbright + nbshared + nbleft) 76print ("Number of relations: ", nbrel) 77 78f = open('../data/FB15k_entity2idx.pkl', 'w') 79g = open('../data/FB15k_idx2entity.pkl', 'w') 80cPickle.dump(entity2idx, f, -1) 81cPickle.dump(idx2entity, g, -1) 82f.close() 83g.close() 84 85################################################# 86### Creation of the dataset files 87 88unseen_ents=[] 89remove_tst_ex=[] 90 91for datatyp in ['train', 'valid', 'test']: 92 print (datatyp) 93 f = open(datapath + 'freebase_mtr100_mte100-%s.txt' % datatyp, 'r') 94 dat = f.readlines() 95 f.close() 96 97 # Declare the dataset variables 98 inpl = sp.lil_matrix((np.max(entity2idx.values()) + 1, len(dat)), 99 dtype='float32') 100 inpr = sp.lil_matrix((np.max(entity2idx.values()) + 1, len(dat)), 101 dtype='float32') 102 inpo = sp.lil_matrix((np.max(entity2idx.values()) + 1, len(dat)), 103 dtype='float32') 104 # Fill the sparse matrices 105 ct = 0 106 for i in dat: 107 lhs, rel, rhs = parseline(i[:-1]) 108 if lhs[0] in entity2idx and rhs[0] in entity2idx and rel[0] in entity2idx: 109 inpl[entity2idx[lhs[0]], ct] = 1 110 inpr[entity2idx[rhs[0]], ct] = 1 111 inpo[entity2idx[rel[0]], ct] = 1 112 ct += 1 113 else: 114 if lhs[0] in entity2idx: 115 unseen_ents+=[lhs[0]] 116 if rel[0] in entity2idx: 117 unseen_ents+=[rel[0]] 118 if rhs[0] in entity2idx: 119 unseen_ents+=[rhs[0]] 120 remove_tst_ex+=[i[:-1]] 121 122 # Save the datasets 123 if 'data' not in os.listdir('../'): 124 os.mkdir('../data') 125 f = open('../data/FB15k-%s-lhs.pkl' % datatyp, 'w') 126 g = open('../data/FB15k-%s-rhs.pkl' % datatyp, 'w') 127 h = open('../data/FB15k-%s-rel.pkl' % datatyp, 'w') 128 cPickle.dump(inpl.tocsr(), f, -1) 129 cPickle.dump(inpr.tocsr(), g, -1) 130 cPickle.dump(inpo.tocsr(), h, -1) 131 f.close() 132 g.close() 133 h.close() 134 135unseen_ents=list(set(unseen_ents)) 136print (len(unseen_ents)) 137remove_tst_ex=list(set(remove_tst_ex)) 138print (len(remove_tst_ex)) 139 140for i in remove_tst_ex: 141 print (i)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/26 04:55 編集
2019/07/26 06:18
2019/07/26 07:05
2019/07/26 07:56
2019/07/26 10:03