前提・実現したいこと
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
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/11 09:38