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

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

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

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

Python 3.x

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

Python

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

Q&A

解決済

2回答

2715閲覧

Kerasで文章生成を行う際のメモリーエラーを回避したい

nuii_

総合スコア15

Keras

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

Python 3.x

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

Python

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

0グッド

0クリップ

投稿2018/10/10 21:15

前提・実現したいこと

KerasでLSTMを使い日本語文の生成を行いたいと考えています。
コードはKerasのサンプルコードを流用し、学習データに13万字程度の分かち書きをした日本語文章を使用します。
とりあえずメモリーエラーを回避して1回動かしたいので、解決策を知りたいです。

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

File "Sample.py", line 53, in <module> x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) MemoryError

該当のソースコード

Python

1# -*- coding: utf-8 -*- 2'''Example script to generate text from Nietzsche's writings. 3At least 20 epochs are required before the generated text 4starts sounding coherent. 5It is recommended to run this script on GPU, as recurrent 6networks are quite computationally intensive. 7If you try this script on new data, make sure your corpus 8has at least ~100k characters. ~1M is better. 9''' 10 11from __future__ import print_function 12from keras.callbacks import LambdaCallback 13from keras.models import Sequential 14from keras.layers import Dense 15from keras.layers import LSTM 16from keras.optimizers import RMSprop 17from keras.utils.data_utils import get_file 18import numpy as np 19import random 20import sys 21import io 22 23with io.open('Text.txt', 'r', encoding='utf-8') as f: 24 text = f.read() 25print('corpus length:', len(text)) 26 27 28chars = sorted(list(set(text))) 29print('total chars:', len(chars)) 30char_indices = dict((c, i) for i, c in enumerate(chars)) 31indices_char = dict((i, c) for i, c in enumerate(chars)) 32 33# cut the text in semi-redundant sequences of maxlen characters 34maxlen = 40 35step = 3 36sentences = [] 37next_chars = [] 38for i in range(0, len(text) - maxlen, step): 39 sentences.append(text[i: i + maxlen]) 40 next_chars.append(text[i + maxlen]) 41print('nb sequences:', len(sentences)) 42 43print('Vectorization...') 44x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) 45y = np.zeros((len(sentences), len(chars)), dtype=np.bool) 46for i, sentence in enumerate(sentences): 47 for t, char in enumerate(sentence): 48 x[i, t, char_indices[char]] = 1 49 y[i, char_indices[next_chars[i]]] = 1 50 51 52# build the model: a single LSTM 53print('Build model...') 54model = Sequential() 55model.add(LSTM(128, input_shape=(maxlen, len(chars)))) 56model.add(Dense(len(chars), activation='softmax')) 57 58optimizer = RMSprop(lr=0.01) 59model.compile(loss='categorical_crossentropy', optimizer=optimizer) 60 61 62def sample(preds, temperature=1.0): 63 # helper function to sample an index from a probability array 64 preds = np.asarray(preds).astype('float64') 65 preds = np.log(preds) / temperature 66 exp_preds = np.exp(preds) 67 preds = exp_preds / np.sum(exp_preds) 68 probas = np.random.multinomial(1, preds, 1) 69 return np.argmax(probas) 70 71 72def on_epoch_end(epoch, _): 73 # Function invoked at end of each epoch. Prints generated text. 74 print() 75 print('----- Generating text after Epoch: %d' % epoch) 76 77 start_index = random.randint(0, len(text) - maxlen - 1) 78 for diversity in [0.2, 0.5, 1.0, 1.2]: 79 print('----- diversity:', diversity) 80 81 generated = '' 82 sentence = text[start_index: start_index + maxlen] 83 generated += sentence 84 print('----- Generating with seed: "' + sentence + '"') 85 sys.stdout.write(generated) 86 87 for i in range(400): 88 x_pred = np.zeros((1, maxlen, len(chars))) 89 for t, char in enumerate(sentence): 90 x_pred[0, t, char_indices[char]] = 1. 91 92 preds = model.predict(x_pred, verbose=0)[0] 93 next_index = sample(preds, diversity) 94 next_char = indices_char[next_index] 95 96 generated += next_char 97 sentence = sentence[1:] + next_char 98 99 sys.stdout.write(next_char) 100 sys.stdout.flush() 101 print() 102 103print_callback = LambdaCallback(on_epoch_end=on_epoch_end) 104 105model.fit(x, y, 106 batch_size=128, 107 epochs=60, 108callbacks=[print_callback])

環境

Python 3.6.6
Anaconda 4.5.11
Windows 8.1
GPU版TensorFlow

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

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

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

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

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

guest

回答2

0

該当の文の前で

Python

1print(len(sentences), maxlen, len(chars))

とかして、自分がいったいどのぐらいの大きさの配列を作ろうとしているのか確認しましたか?

Python

1np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)

した時にそれがどのぐらいの大きさの配列になりそうなのか、考えましたか?

投稿2018/10/10 23:45

quickquip

総合スコア11038

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

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

nuii_

2018/10/11 09:38

配列が大きくなりすぎてしまうので学習データの見直しをしようと思います。 回答ありがとうございました。
guest

0

ベストアンサー

np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)のshapeが半端じゃなく大きいのかと。

日本語は英語(翻訳版みたいですね)と違って文字の種類が何万もありますから、そのまま動かすのは厳しいはずです。

とりあえずメモリーエラーを回避して1回動かしたい

英語のデータを使ってください。元のコードをそのまま動かすのが確実です。

それでも動かなければ、根本的にこういうことをするのにはスペックの低すぎるマシンを使っている可能性があります(必要メモリ量は確認していませんが)。

投稿2018/10/10 21:35

編集2018/10/10 21:35
hayataka2049

総合スコア30933

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

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

nuii_

2018/10/11 09:38

元コードでの実行を試したところ問題なく動いたため、学習データの見直しなどを行いたいと思います。 回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問