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

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

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

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

6821閲覧

python(2系と3系の違い?) biteとstr型に関するエラー(テキストファイル読み込み時) Keras word2vec(テキストファイル読み込み時)

takuyanai

総合スコア11

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/07/25 14:23

編集2017/07/25 15:45

###前提・実現したいこと
ここに質問したいことを詳細に書いてください
alice_in_wonderland.txtというテキストファイルを用いてword2vecを試したいのですが
エラーが出てしまうのですが原因がわかりません。

###発生している問題・エラーメッセージ

TypeError Traceback (most recent call last)
<ipython-input-15-9d4c438d877e> in <module>()
26 fin.close()
27
---> 28 sents = nltk.sent_tokenize(" ".join(lines))
29
30

TypeError: sequence item 0: expected str instance, bytes found

ここにご自身が実行したソースコードを書いてください

from __future__ import print_function from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.preprocessing.text import Tokenizer, one_hot from sklearn.metrics.pairwise import cosine_distances from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder import matplotlib.pyplot as plt import nltk import numpy as np import operator np.random.seed(42) BATCH_SIZE = 128 NUM_EPOCHS = 20 lines = [] fin = open("alice_in_wonderland.txt", "rb") for line in fin: line = line.strip().decode("ascii", "ignore").encode("utf-8") if len(line) == 0: continue lines.append(line) fin.close() sents = nltk.sent_tokenize(" ".join(lines)) tokenizer = Tokenizer(5000) # use top 5000 words only tokens = tokenizer.fit_on_texts(sents) vocab_size = len(tokenizer.word_counts) + 1 xs = [] ys = [] for sent in sents: embedding = one_hot(sent, vocab_size) triples = list(nltk.trigrams(embedding)) w_lefts = [x[0] for x in triples] w_centers = [x[1] for x in triples] w_rights = [x[2] for x in triples] xs.extend(w_centers) ys.extend(w_lefts) xs.extend(w_centers) ys.extend(w_rights) ohe = OneHotEncoder(n_values=vocab_size) X = ohe.fit_transform(np.array(xs).reshape(-1, 1)).todense() Y = ohe.fit_transform(np.array(ys).reshape(-1, 1)).todense() Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.3, random_state=42) print(Xtrain.shape, Xtest.shape, Ytrain.shape, Ytest.shape) model = Sequential() model.add(Dense(300, input_shape=(Xtrain.shape[1],))) model.add(Activation("relu")) model.add(Dropout(0.5)) model.add(Dense(Ytrain.shape[1])) model.add(Activation("softmax")) model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"]) history = model.fit(Xtrain, Ytrain, batch_size=BATCH_SIZE, epochs=NUM_EPOCHS, verbose=1, validation_data=(Xtest, Ytest)) plt.subplot(211) plt.title("accuracy") plt.plot(history.history["acc"], color="r", label="train") plt.plot(history.history["val_acc"], color="b", label="validation") plt.legend(loc="best") plt.subplot(212) plt.title("loss") plt.plot(history.history["loss"], color="r", label="train") plt.plot(history.history["val_loss"], color="b", label="validation") plt.legend(loc="best") plt.tight_layout() plt.show() score = model.evaluate(Xtest, Ytest, verbose=1) print("Test score: {:.3f}, accuracy: {:.3f}".format(score[0], score[1])) word2idx = tokenizer.word_index idx2word = {v:k for k, v in word2idx.items()} W, b = model.layers[0].get_weights() idx2emb = {} for word in word2idx.keys(): wid = word2idx[word] vec_in = ohe.fit_transform(np.array(wid)).todense() vec_emb = np.dot(vec_in, W) idx2emb[wid] = vec_emb for word in ["stupid", "alice", "succeeded"]: wid = word2idx[word] source_emb = idx2emb[wid] distances = [] for i in range(1, vocab_size): if i == wid: continue target_emb = idx2emb[i] distances.append(((wid, i), cosine_distances(source_emb, target_emb))) sorted_distances = sorted(distances, key=operator.itemgetter(1))[0:10] predictions = [idx2word[x[0][1]] for x in sorted_distances] print("{:s} => {:s}".format(word, ", ".join(predictions)))

###試したこと

###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報

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

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

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

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

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

quickquip

2017/07/25 14:58

どういう理由でこう書いたんですか? line = line.strip().decode("ascii", "ignore").encode("utf-8")
guest

回答1

0

ベストアンサー

追記欄で示されているリンク先にも書いていますが、原因はテキストファイルをrbバイナリモードで開いているためです。
その結果、line, linesの要素がbyte型になり、" ".join(lines)で提示エラーが発生しています。
以下のように、単純に読取(テキスト)モードrで開いてください。

Python

1# 略 2fin = open("alice_in_wonderland.txt", "r",encoding='ascii') # テキストモード, 文字コードはテキストファイルに合ったもので開く 3for line in fin: 4 line = line.strip() # decode, encodeは不要 5 if len(line) == 0: 6 continue 7 lines.append(line) 8fin.close() 9# 略

投稿2017/07/26 00:32

can110

総合スコア38256

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

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

takuyanai

2017/07/26 11:14

ありがとうございます!解決しました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問