teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

s

2018/10/23 12:33

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -80,7 +80,7 @@
80
80
  print('total chars:', len(chars))
81
81
 
82
82
  # 文字をID変換
83
- char_to_id = dict((c, i) for i, c in enumerate(chars))
83
+ char_indices = dict((c, i) for i, c in enumerate(chars))
84
84
 
85
85
  # IDから文字へ変換
86
86
  indices_char = dict((i, c) for i, c in enumerate(chars))

1

d

2018/10/23 12:32

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -42,4 +42,103 @@
42
42
  ```
43
43
 
44
44
  これでエラーはなくなり、一応動くようになります。
45
- が、学習自体はうまくいっていないようです。自分は自然言語処理は門外漢なため、学習ができない原因やそもそもやろうとしているアプローチが正しいのかについては、すみませんが、アドバイスできません。
45
+ が、学習自体はうまくいっていないようです。自分は自然言語処理は門外漢なため、学習ができない原因やそもそもやろうとしているアプローチが正しいのかについては、すみませんが、アドバイスできません。
46
+
47
+ ## 追記
48
+
49
+ ```test.txt
50
+ 朝霧 の 中 に 九段 の ともし 哉
51
+ あたたか な 雨 が 降る なり 枯葎
52
+ 菜の花 や は つと 明るき 町 は づれ
53
+ 秋風 や 伊予 へ 流る る 汐 の 音
54
+ 長閑 さ や 障子 の 穴 に 海 見え て
55
+ 若鮎 の 二 手 に なりて 上り けり
56
+ 行く 秋 を す つく と 鹿 の 立ち に けり
57
+ 我 声 の 風 に なり けり 茸狩
58
+ 毎年 よ 彼岸の入り に 寒い の は
59
+ ```
60
+
61
+ ```python
62
+ import numpy as np
63
+ import codecs
64
+ from keras.layers import Activation, Dense, Input
65
+ from keras.models import Model
66
+
67
+ #データの読み込み
68
+ with open(r'test.txt', encoding='utf-8') as f:
69
+ poems = f.read().splitlines()
70
+ text = poems[0] # 1個目のデータ
71
+ print(text)
72
+
73
+ # コーパスの長さ
74
+ print('corpus length:', len(text))
75
+
76
+ # 文字数を数えるため、textをソート
77
+ chars = sorted(list(set(text)))
78
+
79
+ # 全文字数の表示
80
+ print('total chars:', len(chars))
81
+
82
+ # 文字をID変換
83
+ char_to_id = dict((c, i) for i, c in enumerate(chars))
84
+
85
+ # IDから文字へ変換
86
+ indices_char = dict((i, c) for i, c in enumerate(chars))
87
+
88
+ #テキストを17文字ずつ読み込む
89
+ maxlen = 17
90
+ #サンプルバッチ数
91
+ step = 3
92
+ sentences = []
93
+ next_chars = []
94
+ for i in range(0, len(text) - maxlen, step):
95
+ sentences.append(text[i: i + maxlen])
96
+ next_chars.append(text[i + maxlen])
97
+ #学習する文字数を表示
98
+ print('Sequences:', sentences)
99
+ print('next_chars:', next_chars)
100
+
101
+ #ベクトル化する
102
+ print('Vectorization...')
103
+ x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
104
+ y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
105
+ for i, sentence in enumerate(sentences):
106
+ for t, char in enumerate(sentence):
107
+ x[i, t, char_indices[char]] = 1
108
+ y[i, char_indices[next_chars[i]]] = 1
109
+
110
+ #モデルを構築する工程に入る
111
+ print('Build model...')
112
+ #encoderの次元
113
+ encoding_dim = 128
114
+ #入力用の変数
115
+ input_word = Input(shape=(maxlen, len(chars)))
116
+ #入力された語がencodeされたものを格納する
117
+ encoded = Dense(128, activation='relu')(input_word)
118
+ encoded = Dense(64, activation='relu')(encoded)
119
+ encoded = Dense(32, activation='relu')(encoded)
120
+ #潜在変数(実質的な主成分分析)
121
+ latent = Dense(8, activation='relu')(encoded)
122
+ #encodeされたデータを再構成
123
+ decoded = Dense(32, activation='relu')(latent)
124
+ decoded = Dense(64, activation='relu')(decoded)
125
+ decoded = Dense(12, activation='relu')(encoded)
126
+ autoencoder = Model(input=input_word, output=decoded)
127
+ # #Adamで最適化、loss関数をcategorical_crossentropy
128
+ autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
129
+ autoencoder.summary()
130
+
131
+ print(x.shape)
132
+ # #autoencoderの実行
133
+ autoencoder.fit(x, x,
134
+ epochs=1000,
135
+ batch_size=256,
136
+ shuffle=False)
137
+
138
+ #モデルの構造を保存
139
+ model_json = autoencoder.to_json()
140
+ with open('keras_AE.json', 'w') as json_file:
141
+ json_file.write(model_json)
142
+ #学習済みモデルの重みを保存
143
+ autoencoder.save_weights('AE.h5')
144
+ ```