回答編集履歴

2

s

2018/10/23 12:33

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -162,7 +162,7 @@
162
162
 
163
163
  # 文字をID変換
164
164
 
165
- char_to_id = dict((c, i) for i, c in enumerate(chars))
165
+ char_indices = dict((c, i) for i, c in enumerate(chars))
166
166
 
167
167
 
168
168
 

1

d

2018/10/23 12:32

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -87,3 +87,201 @@
87
87
  これでエラーはなくなり、一応動くようになります。
88
88
 
89
89
  が、学習自体はうまくいっていないようです。自分は自然言語処理は門外漢なため、学習ができない原因やそもそもやろうとしているアプローチが正しいのかについては、すみませんが、アドバイスできません。
90
+
91
+
92
+
93
+ ## 追記
94
+
95
+
96
+
97
+ ```test.txt
98
+
99
+ 朝霧 の 中 に 九段 の ともし 哉
100
+
101
+ あたたか な 雨 が 降る なり 枯葎
102
+
103
+ 菜の花 や は つと 明るき 町 は づれ
104
+
105
+ 秋風 や 伊予 へ 流る る 汐 の 音
106
+
107
+ 長閑 さ や 障子 の 穴 に 海 見え て
108
+
109
+ 若鮎 の 二 手 に なりて 上り けり
110
+
111
+ 行く 秋 を す つく と 鹿 の 立ち に けり
112
+
113
+ 我 声 の 風 に なり けり 茸狩
114
+
115
+ 毎年 よ 彼岸の入り に 寒い の は
116
+
117
+ ```
118
+
119
+
120
+
121
+ ```python
122
+
123
+ import numpy as np
124
+
125
+ import codecs
126
+
127
+ from keras.layers import Activation, Dense, Input
128
+
129
+ from keras.models import Model
130
+
131
+
132
+
133
+ #データの読み込み
134
+
135
+ with open(r'test.txt', encoding='utf-8') as f:
136
+
137
+ poems = f.read().splitlines()
138
+
139
+ text = poems[0] # 1個目のデータ
140
+
141
+ print(text)
142
+
143
+
144
+
145
+ # コーパスの長さ
146
+
147
+ print('corpus length:', len(text))
148
+
149
+
150
+
151
+ # 文字数を数えるため、textをソート
152
+
153
+ chars = sorted(list(set(text)))
154
+
155
+
156
+
157
+ # 全文字数の表示
158
+
159
+ print('total chars:', len(chars))
160
+
161
+
162
+
163
+ # 文字をID変換
164
+
165
+ char_to_id = dict((c, i) for i, c in enumerate(chars))
166
+
167
+
168
+
169
+ # IDから文字へ変換
170
+
171
+ indices_char = dict((i, c) for i, c in enumerate(chars))
172
+
173
+
174
+
175
+ #テキストを17文字ずつ読み込む
176
+
177
+ maxlen = 17
178
+
179
+ #サンプルバッチ数
180
+
181
+ step = 3
182
+
183
+ sentences = []
184
+
185
+ next_chars = []
186
+
187
+ for i in range(0, len(text) - maxlen, step):
188
+
189
+ sentences.append(text[i: i + maxlen])
190
+
191
+ next_chars.append(text[i + maxlen])
192
+
193
+ #学習する文字数を表示
194
+
195
+ print('Sequences:', sentences)
196
+
197
+ print('next_chars:', next_chars)
198
+
199
+
200
+
201
+ #ベクトル化する
202
+
203
+ print('Vectorization...')
204
+
205
+ x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
206
+
207
+ y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
208
+
209
+ for i, sentence in enumerate(sentences):
210
+
211
+ for t, char in enumerate(sentence):
212
+
213
+ x[i, t, char_indices[char]] = 1
214
+
215
+ y[i, char_indices[next_chars[i]]] = 1
216
+
217
+
218
+
219
+ #モデルを構築する工程に入る
220
+
221
+ print('Build model...')
222
+
223
+ #encoderの次元
224
+
225
+ encoding_dim = 128
226
+
227
+ #入力用の変数
228
+
229
+ input_word = Input(shape=(maxlen, len(chars)))
230
+
231
+ #入力された語がencodeされたものを格納する
232
+
233
+ encoded = Dense(128, activation='relu')(input_word)
234
+
235
+ encoded = Dense(64, activation='relu')(encoded)
236
+
237
+ encoded = Dense(32, activation='relu')(encoded)
238
+
239
+ #潜在変数(実質的な主成分分析)
240
+
241
+ latent = Dense(8, activation='relu')(encoded)
242
+
243
+ #encodeされたデータを再構成
244
+
245
+ decoded = Dense(32, activation='relu')(latent)
246
+
247
+ decoded = Dense(64, activation='relu')(decoded)
248
+
249
+ decoded = Dense(12, activation='relu')(encoded)
250
+
251
+ autoencoder = Model(input=input_word, output=decoded)
252
+
253
+ # #Adamで最適化、loss関数をcategorical_crossentropy
254
+
255
+ autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
256
+
257
+ autoencoder.summary()
258
+
259
+
260
+
261
+ print(x.shape)
262
+
263
+ # #autoencoderの実行
264
+
265
+ autoencoder.fit(x, x,
266
+
267
+ epochs=1000,
268
+
269
+ batch_size=256,
270
+
271
+ shuffle=False)
272
+
273
+
274
+
275
+ #モデルの構造を保存
276
+
277
+ model_json = autoencoder.to_json()
278
+
279
+ with open('keras_AE.json', 'w') as json_file:
280
+
281
+ json_file.write(model_json)
282
+
283
+ #学習済みモデルの重みを保存
284
+
285
+ autoencoder.save_weights('AE.h5')
286
+
287
+ ```