質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Chainer

Chainerは、国産の深層学習フレームワークです。あらゆるニューラルネットワークをPythonで柔軟に書くことができ、学習させることが可能。GPUをサポートしており、複数のGPUを用いた学習も直感的に記述できます。

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

337閲覧

encodeErroerについて

yukit5669

総合スコア15

Chainer

Chainerは、国産の深層学習フレームワークです。あらゆるニューラルネットワークをPythonで柔軟に書くことができ、学習させることが可能。GPUをサポートしており、複数のGPUを用いた学習も直感的に記述できます。

深層学習

深層学習は、多数のレイヤのニューラルネットワークによる機械学習手法。人工知能研究の一つでディープラーニングとも呼ばれています。コンピューター自体がデータの潜在的な特徴を汲み取り、効率的で的確な判断を実現することができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/02/11 10:35

編集2019/02/12 03:30

python

1 2import re 3 4def split_by_punct(segment): 5 """Splits str segment by punctuation, filters our empties and spaces.""" 6 return [s for s in re.split(r'\W+', segment) if s and not s.isspace()] 7 8def read_text(filename): 9 with open(filename) as f: 10 sentences = [] 11 for line in f: 12 line = line.strip() 13 words = ' '.join(split_by_punct(line)).strip() 14 sentences.append(words) 15 return ' '.join(sentences).strip() 16 17 18def load_file(data_file, split_idx): 19 train = [] 20 dev = [] 21 with open(data_file) as f: 22 for filename in f: 23 idx = int(filename.split('/')[-1].split('_')[0]) 24 words = read_text(filename.strip()) 25 if idx >= split_idx: 26 dev.append(words) 27 else: 28 train.append(words) 29 return train, dev 30 31 32def load_test_dataset(data_file): 33 test = [] 34 with open(data_file) as f: 35 for filename in f: 36 words = read_text(filename.strip()) 37 test.append(words) 38 return test 39 40def prepare_imdb(): 41 # this split is used at 42 # https://github.com/tensorflow/models/tree/master/research/adversarial_text 43 imdb_validation_pos_start_id = 10621 # total size: 12499 44 imdb_validation_neg_start_id = 10625 45 46 def fwrite_data(filename, sentences): 47 with open(filename, 'w') as f: 48 for words in sentences: 49 # line = ' '.join(words) 50 line = words 51 f.write(line.strip() + '\n') 52 f.close() 53 54 pos_train, pos_dev = load_file('imdb_train_pos_list.txt', 55 imdb_validation_pos_start_id) 56 neg_train, neg_dev = load_file('imdb_train_neg_list.txt', 57 imdb_validation_neg_start_id) 58 59 pos_test = load_test_dataset('imdb_test_pos_list.txt') 60 neg_test = load_test_dataset('imdb_test_neg_list.txt') 61 62 fwrite_data('imdb_pos_train.txt', pos_train) 63 fwrite_data('imdb_pos_dev.txt', pos_dev) 64 fwrite_data('imdb_neg_train.txt', neg_train) 65 fwrite_data('imdb_neg_dev.txt', neg_dev) 66 67 fwrite_data('imdb_pos_test.txt', pos_test) 68 fwrite_data('imdb_neg_test.txt', neg_test) 69 70 unlabled_lm_train, _ = load_file('imdb_unlabled_list.txt', 100000000) 71 72 fwrite_data('imdb_unlabled.txt', unlabled_lm_train) 73 print('Done') 74 75if __name__ == '__main__': 76 import sys 77 action = sys.argv[1] 78 if action == 'prepare_imdb': 79 prepare_imdb()

https://github.com/aonotas/adversarial_text

上記githubにあることをやろうとしています。

該当コードを実行したところ、
実行ファイル ./download.sh Prepare for IMDB Prepare script is running... Traceback (most recent call last): File "preprocess.py", line 79, in <module> prepare_imdb() File "preprocess.py", line 55, in prepare_imdb imdb_validation_pos_start_id) File "preprocess.py", line 24, in load_file words = read_text(filename.strip()) File "preprocess.py", line 11, in read_text for line in f: File "/Users/hogehoge/anaconda3/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 399: ordinal not in range(128)
がでました

環境は
OS Mac Mojave Python 3.6.5 :: Anaconda, Inc.
です

どのように修正すれば実行されるでしょうか

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

quickquip

2019/02/11 23:17

OS,Pythonはなにを使っているか、エラーが出たのはどこかを書く。トレースバックを省略しない。そもそもあなたかなにを実行したのかすら、他の人にはわからない。
yukit5669

2019/02/12 03:30 編集

わかりにくい記載をして申し訳ありませんでした OS Mac Mojave Python 3.6.5 :: Anaconda, Inc. ```実行ファイル ./download.sh Prepare for IMDB Prepare script is running... Traceback (most recent call last): File "preprocess.py", line 79, in <module> prepare_imdb() File "preprocess.py", line 55, in prepare_imdb imdb_validation_pos_start_id) File "preprocess.py", line 24, in load_file words = read_text(filename.strip()) File "preprocess.py", line 11, in read_text for line in f: File "/Users/hogehoge/anaconda3/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 399: ordinal not in range(128)```
guest

回答1

0

ベストアンサー

シェススクリプトの先頭でexport LC_ALL=Cしているんだから、ASCIIの範囲の文字しか受けつけなくなりますね。

https://github.com/aonotas/adversarial_text/network
https://github.com/tingkai-zhang/adversarial_text/commit/b0c8ec93f2040a7917fd96de35d21d7a099d3787

あたりに、直した人がいるように見えるので

https://github.com/tingkai-zhang/adversarial_text を使えばいいかもしれません。

投稿2019/02/12 04:22

quickquip

総合スコア11029

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yukit5669

2019/02/12 07:15

ありがとうございます! 自分の調査不足でした 本当にお助けいただきありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問