質問編集履歴
11
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
下記のコードで、3万9000件の文章データを入れているはずが、一文しか入っていないということになっています。
|
2
2
|
|
3
|
+
|
4
|
+
|
3
5
|
> Using TensorFlow backend.
|
4
6
|
|
5
7
|
朝霧 の 中 に 九段 の ともし 哉
|
@@ -14,27 +16,37 @@
|
|
14
16
|
|
15
17
|
Build model...
|
16
18
|
|
19
|
+
|
20
|
+
|
21
|
+
原因を推測される方は何卒、宜しくお願いいたします。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
追記:
|
26
|
+
|
27
|
+
textをpoemsに変えてみました。
|
28
|
+
|
29
|
+
すると、ValueError: Error when checking target: expected dense_7 to have shape (2, 12) but got array with shape (2, 39065)となりました。
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
文章が表示される原因が、
|
34
|
+
|
35
|
+
print('Sequences:', sentences)
|
36
|
+
|
37
|
+
であることがわかりました。
|
38
|
+
|
39
|
+
|
40
|
+
|
17
41
|
/home/yudai/Desktop/src/keras_AE.py:54: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., outputs=Tensor("de...)`
|
18
42
|
|
19
43
|
autoencoder = Model(input=input_word, output=decoded)
|
20
44
|
|
21
|
-
|
22
|
-
|
23
|
-
原因を推測される方は何卒、宜しくお願いいたします。
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
45
|
+
は、
|
28
|
-
|
29
|
-
|
46
|
+
|
30
|
-
|
31
|
-
すると、ValueError: Error when checking target: expected dense_7 to have shape (2, 12) but got array with shape (2, 39065)となりました。
|
32
|
-
|
33
|
-
また、文章が表示される原因が、
|
34
|
-
|
35
|
-
|
47
|
+
autoencoder = Model(inputs=input_word, outputs=decoded)
|
36
|
-
|
48
|
+
|
37
|
-
で
|
49
|
+
で解消されることがわかりました。
|
38
50
|
|
39
51
|
|
40
52
|
|
10
文法訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -162,10 +162,6 @@
|
|
162
162
|
|
163
163
|
sentences.append(text[i: i + maxlen])
|
164
164
|
|
165
|
-
#学習する文字数を表示
|
166
|
-
|
167
|
-
print('Sequences:', sentences)
|
168
|
-
|
169
165
|
#ベクトル化する
|
170
166
|
|
171
167
|
print('Vectorization...')
|
9
文法訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -210,7 +210,7 @@
|
|
210
210
|
|
211
211
|
decoded = Dense(12, activation='relu')(encoded)
|
212
212
|
|
213
|
-
autoencoder = Model(input=input_word, output=decoded)
|
213
|
+
autoencoder = Model(inputs=input_word, outputs=decoded)
|
214
214
|
|
215
215
|
# #Adamで最適化、loss関数をcategorical_crossentropy
|
216
216
|
|
8
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,6 +24,20 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
+
追記:
|
28
|
+
|
29
|
+
textをpoemsに変えてみました。
|
30
|
+
|
31
|
+
すると、ValueError: Error when checking target: expected dense_7 to have shape (2, 12) but got array with shape (2, 39065)となりました。
|
32
|
+
|
33
|
+
また、文章が表示される原因が、
|
34
|
+
|
35
|
+
print('Sequences:', sentences)
|
36
|
+
|
37
|
+
であることがわかりました。
|
38
|
+
|
39
|
+
|
40
|
+
|
27
41
|
poem.txt
|
28
42
|
|
29
43
|
```
|
7
文法訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,228 +24,214 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
|
27
|
+
poem.txt
|
28
|
+
|
29
|
+
```
|
28
30
|
|
29
31
|
朝霧 の 中 に 九段 の ともし 哉
|
30
32
|
|
33
|
+
あたたか な 雨 が 降る なり 枯葎
|
34
|
+
|
35
|
+
菜の花 や は つと 明るき 町 は づれ
|
36
|
+
|
37
|
+
秋風 や 伊予 へ 流る る 汐 の 音
|
38
|
+
|
39
|
+
長閑 さ や 障子 の 穴 に 海 見え て
|
40
|
+
|
41
|
+
若鮎 の 二 手 に なりて 上り けり
|
42
|
+
|
43
|
+
行く 秋 を す つく と 鹿 の 立ち に けり
|
44
|
+
|
31
|
-
|
45
|
+
我 声 の 風 に なり けり 茸狩
|
46
|
+
|
32
|
-
|
47
|
+
毎年 よ 彼岸の入り に 寒い の は
|
48
|
+
|
49
|
+
我宿 は 女 ばかり の あつ さ 哉
|
50
|
+
|
33
|
-
|
51
|
+
妻 より は 妾 の 多し 門 涼み
|
34
|
-
|
52
|
+
|
35
|
-
|
53
|
+
みちのく へ 涼み に 行く や 下駄 は い て
|
54
|
+
|
36
|
-
|
55
|
+
夕立 や 殺生石 の あたり より
|
56
|
+
|
37
|
-
|
57
|
+
稲妻 や 生血 したたる つるし 熊
|
58
|
+
|
38
|
-
|
59
|
+
薪 を わる いもうと 一人 冬 籠
|
60
|
+
|
39
|
-
|
61
|
+
絶えず 人 いこ ふ 夏野 の 石 一つ
|
62
|
+
|
40
|
-
|
63
|
+
赤蜻蛉 筑波 に 雲 もなか り けり
|
64
|
+
|
65
|
+
何となく 奈良 なつかし や 古 暦
|
66
|
+
|
67
|
+
春 や 昔 十 五 万 石 の 城下 哉
|
68
|
+
|
69
|
+
六月 を 奇麗 な 風 の 吹く こと よ
|
70
|
+
|
71
|
+
夏 瘦 の 骨 に とどまる 命 か な
|
72
|
+
|
73
|
+
行く 我 に とどまる 汝 に 秋 二つ
|
74
|
+
|
75
|
+
柿 く へ ば 鐘 が 鳴る なり 法隆寺
|
76
|
+
|
77
|
+
漱石 が 来 て 虚子 が 来 て 大 三十日
|
78
|
+
|
41
|
-
|
79
|
+
枯薄 ここら よ 昔 不破の関
|
80
|
+
|
81
|
+
元日 の 人通り と は なり に けり
|
82
|
+
|
83
|
+
春風 に こぼれ て 赤 し 歯磨粉
|
84
|
+
|
85
|
+
春 の 夜 や 屏風 の 陰 に 物 の 息
|
42
86
|
|
43
87
|
```
|
44
88
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
89
|
+
|
90
|
+
|
91
|
+
```python
|
92
|
+
|
93
|
+
import numpy as np
|
94
|
+
|
95
|
+
import codecs
|
96
|
+
|
97
|
+
from keras.layers import Activation, Dense, Input
|
98
|
+
|
99
|
+
from keras.models import Model
|
100
|
+
|
101
|
+
import sys
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
#データの読み込み
|
106
|
+
|
107
|
+
with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
|
108
|
+
|
109
|
+
poems = f.read().splitlines()
|
110
|
+
|
111
|
+
text = poems[0] # 1個目のデータ
|
112
|
+
|
113
|
+
print(text)
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# コーパスの長さ
|
118
|
+
|
119
|
+
print('corpus length:', len(text))
|
120
|
+
|
121
|
+
# 文字数を数えるため、textをソート
|
122
|
+
|
123
|
+
chars = sorted(list(set(text)))
|
124
|
+
|
125
|
+
# 全文字数の表示
|
126
|
+
|
127
|
+
print('total chars:', len(chars))
|
128
|
+
|
129
|
+
# 文字をID変換
|
130
|
+
|
131
|
+
char_indices = dict((c, i) for i, c in enumerate(chars))
|
132
|
+
|
133
|
+
# IDから文字へ変換
|
134
|
+
|
135
|
+
indices_char = dict((i, c) for i, c in enumerate(chars))
|
136
|
+
|
137
|
+
#テキストを17文字ずつ読み込む
|
138
|
+
|
139
|
+
maxlen = 2
|
140
|
+
|
141
|
+
#サンプルバッチ数
|
142
|
+
|
143
|
+
step = 2
|
144
|
+
|
145
|
+
sentences = []
|
146
|
+
|
147
|
+
for i in range(0, len(text) - maxlen, step):
|
148
|
+
|
149
|
+
sentences.append(text[i: i + maxlen])
|
150
|
+
|
151
|
+
#学習する文字数を表示
|
152
|
+
|
153
|
+
print('Sequences:', sentences)
|
154
|
+
|
155
|
+
#ベクトル化する
|
156
|
+
|
157
|
+
print('Vectorization...')
|
158
|
+
|
159
|
+
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
|
160
|
+
|
161
|
+
for i, sentence in enumerate(sentences):
|
162
|
+
|
163
|
+
for t, char in enumerate(sentence):
|
164
|
+
|
165
|
+
x[i, t, char_indices[char]] = 1
|
166
|
+
|
167
|
+
#モデルを構築する工程に入る
|
168
|
+
|
169
|
+
print('Build model...')
|
170
|
+
|
171
|
+
#encoderの次元
|
172
|
+
|
173
|
+
encoding_dim = 128
|
174
|
+
|
175
|
+
#入力用の変数
|
176
|
+
|
177
|
+
input_word = Input(shape=(maxlen, len(chars)))
|
178
|
+
|
179
|
+
#入力された語がencodeされたものを格納する
|
180
|
+
|
181
|
+
encoded = Dense(128, activation='relu')(input_word)
|
182
|
+
|
183
|
+
encoded = Dense(64, activation='relu')(encoded)
|
184
|
+
|
185
|
+
encoded = Dense(32, activation='relu')(encoded)
|
186
|
+
|
187
|
+
#潜在変数(実質的な主成分分析)
|
188
|
+
|
189
|
+
latent = Dense(8, activation='relu')(encoded)
|
190
|
+
|
191
|
+
#encodeされたデータを再構成
|
192
|
+
|
193
|
+
decoded = Dense(32, activation='relu')(latent)
|
194
|
+
|
195
|
+
decoded = Dense(64, activation='relu')(decoded)
|
196
|
+
|
197
|
+
decoded = Dense(12, activation='relu')(encoded)
|
198
|
+
|
199
|
+
autoencoder = Model(input=input_word, output=decoded)
|
200
|
+
|
201
|
+
# #Adamで最適化、loss関数をcategorical_crossentropy
|
202
|
+
|
203
|
+
autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
|
204
|
+
|
205
|
+
#モデルの構造を見る
|
206
|
+
|
207
|
+
autoencoder.summary()
|
208
|
+
|
209
|
+
#アレイサイズの確認
|
210
|
+
|
211
|
+
print(x.shape)
|
212
|
+
|
213
|
+
#autoencoderの実行
|
214
|
+
|
215
|
+
autoencoder.fit(x, x,
|
216
|
+
|
217
|
+
epochs=50,
|
218
|
+
|
219
|
+
batch_size=3,
|
220
|
+
|
221
|
+
shuffle=False)
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
for i in range(17):
|
226
|
+
|
227
|
+
x_haiku = np.zeros((1, maxlen, len(chars)))
|
228
|
+
|
229
|
+
for t, char in enumerate(sentence):
|
230
|
+
|
231
|
+
x_haiku[0,char_indices[char]] = 1.
|
232
|
+
|
233
|
+
sentence = sentence[:-1]
|
234
|
+
|
235
|
+
print(char)
|
100
236
|
|
101
237
|
```
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
```python
|
106
|
-
|
107
|
-
import numpy as np
|
108
|
-
|
109
|
-
import codecs
|
110
|
-
|
111
|
-
from keras.layers import Activation, Dense, Input
|
112
|
-
|
113
|
-
from keras.models import Model
|
114
|
-
|
115
|
-
import sys
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#データの読み込み
|
120
|
-
|
121
|
-
with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
|
122
|
-
|
123
|
-
poems = f.read().splitlines()
|
124
|
-
|
125
|
-
text = poems[0] # 1個目のデータ
|
126
|
-
|
127
|
-
print(text)
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
# コーパスの長さ
|
132
|
-
|
133
|
-
print('corpus length:', len(text))
|
134
|
-
|
135
|
-
# 文字数を数えるため、textをソート
|
136
|
-
|
137
|
-
chars = sorted(list(set(text)))
|
138
|
-
|
139
|
-
# 全文字数の表示
|
140
|
-
|
141
|
-
print('total chars:', len(chars))
|
142
|
-
|
143
|
-
# 文字をID変換
|
144
|
-
|
145
|
-
char_indices = dict((c, i) for i, c in enumerate(chars))
|
146
|
-
|
147
|
-
# IDから文字へ変換
|
148
|
-
|
149
|
-
indices_char = dict((i, c) for i, c in enumerate(chars))
|
150
|
-
|
151
|
-
#テキストを17文字ずつ読み込む
|
152
|
-
|
153
|
-
maxlen = 2
|
154
|
-
|
155
|
-
#サンプルバッチ数
|
156
|
-
|
157
|
-
step = 2
|
158
|
-
|
159
|
-
sentences = []
|
160
|
-
|
161
|
-
for i in range(0, len(text) - maxlen, step):
|
162
|
-
|
163
|
-
sentences.append(text[i: i + maxlen])
|
164
|
-
|
165
|
-
#学習する文字数を表示
|
166
|
-
|
167
|
-
print('Sequences:', sentences)
|
168
|
-
|
169
|
-
#ベクトル化する
|
170
|
-
|
171
|
-
print('Vectorization...')
|
172
|
-
|
173
|
-
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
|
174
|
-
|
175
|
-
for i, sentence in enumerate(sentences):
|
176
|
-
|
177
|
-
for t, char in enumerate(sentence):
|
178
|
-
|
179
|
-
x[i, t, char_indices[char]] = 1
|
180
|
-
|
181
|
-
#モデルを構築する工程に入る
|
182
|
-
|
183
|
-
print('Build model...')
|
184
|
-
|
185
|
-
#encoderの次元
|
186
|
-
|
187
|
-
encoding_dim = 128
|
188
|
-
|
189
|
-
#入力用の変数
|
190
|
-
|
191
|
-
input_word = Input(shape=(maxlen, len(chars)))
|
192
|
-
|
193
|
-
#入力された語がencodeされたものを格納する
|
194
|
-
|
195
|
-
encoded = Dense(128, activation='relu')(input_word)
|
196
|
-
|
197
|
-
encoded = Dense(64, activation='relu')(encoded)
|
198
|
-
|
199
|
-
encoded = Dense(32, activation='relu')(encoded)
|
200
|
-
|
201
|
-
#潜在変数(実質的な主成分分析)
|
202
|
-
|
203
|
-
latent = Dense(8, activation='relu')(encoded)
|
204
|
-
|
205
|
-
#encodeされたデータを再構成
|
206
|
-
|
207
|
-
decoded = Dense(32, activation='relu')(latent)
|
208
|
-
|
209
|
-
decoded = Dense(64, activation='relu')(decoded)
|
210
|
-
|
211
|
-
decoded = Dense(12, activation='relu')(encoded)
|
212
|
-
|
213
|
-
autoencoder = Model(input=input_word, output=decoded)
|
214
|
-
|
215
|
-
# #Adamで最適化、loss関数をcategorical_crossentropy
|
216
|
-
|
217
|
-
autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
|
218
|
-
|
219
|
-
#モデルの構造を見る
|
220
|
-
|
221
|
-
autoencoder.summary()
|
222
|
-
|
223
|
-
#アレイサイズの確認
|
224
|
-
|
225
|
-
print(x.shape)
|
226
|
-
|
227
|
-
#autoencoderの実行
|
228
|
-
|
229
|
-
autoencoder.fit(x, x,
|
230
|
-
|
231
|
-
epochs=50,
|
232
|
-
|
233
|
-
batch_size=3,
|
234
|
-
|
235
|
-
shuffle=False)
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
for i in range(17):
|
240
|
-
|
241
|
-
x_haiku = np.zeros((1, maxlen, len(chars)))
|
242
|
-
|
243
|
-
for t, char in enumerate(sentence):
|
244
|
-
|
245
|
-
x_haiku[0,char_indices[char]] = 1.
|
246
|
-
|
247
|
-
sentence = sentence[:-1]
|
248
|
-
|
249
|
-
print(char)
|
250
|
-
|
251
|
-
```
|
6
環境詳細を記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,22 @@
|
|
1
|
-
下記のコードで、3万9000件の文章データを入れているはずが、
|
1
|
+
下記のコードで、3万9000件の文章データを入れているはずが、一文しか入っていないということになっています。
|
2
|
+
|
3
|
+
> Using TensorFlow backend.
|
4
|
+
|
5
|
+
朝霧 の 中 に 九段 の ともし 哉
|
6
|
+
|
7
|
+
corpus length: 20
|
8
|
+
|
9
|
+
total chars: 12
|
10
|
+
|
11
|
+
Sequences: ['朝霧', ' の', ' 中', ' に', ' 九', '段 ', 'の ', 'とも', 'し ']
|
12
|
+
|
13
|
+
Vectorization...
|
14
|
+
|
15
|
+
Build model...
|
16
|
+
|
17
|
+
/home/yudai/Desktop/src/keras_AE.py:54: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., outputs=Tensor("de...)`
|
18
|
+
|
19
|
+
autoencoder = Model(input=input_word, output=decoded)
|
2
20
|
|
3
21
|
|
4
22
|
|
5
タイトルの改善
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
文章が読み込まれません
|
test
CHANGED
@@ -1,48 +1,22 @@
|
|
1
|
+
下記のコードで、3万9000件の文章データを入れているはずが、12しか入っていないということになっています。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
|
5
|
+
原因を推測される方は何卒、宜しくお願いいたします。
|
2
|
-
|
6
|
+
|
7
|
+
|
8
|
+
|
3
|
-
|
9
|
+
Using TensorFlow backend.
|
10
|
+
|
4
|
-
|
11
|
+
朝霧 の 中 に 九段 の ともし 哉
|
12
|
+
|
13
|
+
corpus length: 20
|
14
|
+
|
15
|
+
total chars: 12
|
16
|
+
|
5
|
-
|
17
|
+
Sequences: ['朝霧', ' の', ' 中', ' に', ' 九', '段 ', 'の ', 'とも', 'し ']
|
6
|
-
|
7
|
-
|
18
|
+
|
8
|
-
|
9
|
-
|
19
|
+
|
10
|
-
|
11
|
-
str(type(x)) + '`. '
|
12
|
-
|
13
|
-
ValueError: Unexpectedly found an instance of type `<class 'int'>`. Expected a symbolic tensor instance.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
> During handling of the above exception, another exception occurred:
|
18
|
-
|
19
|
-
>
|
20
|
-
|
21
|
-
> Traceback (most recent call last):
|
22
|
-
|
23
|
-
> File "/home/yudai/Desktop/keras_AE.py", line 94, in <module>
|
24
|
-
|
25
|
-
> model = autoencoder(len(word2id))
|
26
|
-
|
27
|
-
> File "/home/yudai/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 440, in __call__
|
28
|
-
|
29
|
-
> self.assert_input_compatibility(inputs)
|
30
|
-
|
31
|
-
> File "/home/yudai/.local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 285, in assert_input_compatibility
|
32
|
-
|
33
|
-
> str(inputs) + '. All inputs to the layer '
|
34
|
-
|
35
|
-
> ValueError: Layer model_1 was called with an input that isn't a symbolic tensor. Received type: <class 'int'>. Full input: [4]. All inputs to the layer should be tensors.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
と出ます。
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
レイヤに実際に入力を与えていないようです。
|
44
|
-
|
45
|
-
[GitHub](https://stackoverflow.com/questions/44852153/layer-called-with-an-input-that-isnt-a-symbolic-tensor-keras)
|
46
20
|
|
47
21
|
|
48
22
|
|
@@ -50,15 +24,61 @@
|
|
50
24
|
|
51
25
|
```
|
52
26
|
|
53
|
-
朝霧 の 中 に 九段 の ともし 哉
|
27
|
+
朝霧 の 中 に 九段 の ともし 哉
|
54
|
-
|
28
|
+
|
55
|
-
あたたか な 雨 が 降る なり 枯葎
|
29
|
+
あたたか な 雨 が 降る なり 枯葎
|
56
|
-
|
30
|
+
|
57
|
-
菜の花 や は つと 明るき 町 は づれ
|
31
|
+
菜の花 や は つと 明るき 町 は づれ
|
58
|
-
|
32
|
+
|
59
|
-
秋風 や 伊予 へ 流る る 汐 の 音
|
33
|
+
秋風 や 伊予 へ 流る る 汐 の 音
|
60
|
-
|
34
|
+
|
61
|
-
長閑 さ や 障子 の 穴 に 海 見え て
|
35
|
+
長閑 さ や 障子 の 穴 に 海 見え て
|
36
|
+
|
37
|
+
若鮎 の 二 手 に なりて 上り けり
|
38
|
+
|
39
|
+
行く 秋 を す つく と 鹿 の 立ち に けり
|
40
|
+
|
41
|
+
我 声 の 風 に なり けり 茸狩
|
42
|
+
|
43
|
+
毎年 よ 彼岸の入り に 寒い の は
|
44
|
+
|
45
|
+
我宿 は 女 ばかり の あつ さ 哉
|
46
|
+
|
47
|
+
妻 より は 妾 の 多し 門 涼み
|
48
|
+
|
49
|
+
みちのく へ 涼み に 行く や 下駄 は い て
|
50
|
+
|
51
|
+
夕立 や 殺生石 の あたり より
|
52
|
+
|
53
|
+
稲妻 や 生血 したたる つるし 熊
|
54
|
+
|
55
|
+
薪 を わる いもうと 一人 冬 籠
|
56
|
+
|
57
|
+
絶えず 人 いこ ふ 夏野 の 石 一つ
|
58
|
+
|
59
|
+
赤蜻蛉 筑波 に 雲 もなか り けり
|
60
|
+
|
61
|
+
何となく 奈良 なつかし や 古 暦
|
62
|
+
|
63
|
+
春 や 昔 十 五 万 石 の 城下 哉
|
64
|
+
|
65
|
+
六月 を 奇麗 な 風 の 吹く こと よ
|
66
|
+
|
67
|
+
夏 瘦 の 骨 に とどまる 命 か な
|
68
|
+
|
69
|
+
行く 我 に とどまる 汝 に 秋 二つ
|
70
|
+
|
71
|
+
柿 く へ ば 鐘 が 鳴る なり 法隆寺
|
72
|
+
|
73
|
+
漱石 が 来 て 虚子 が 来 て 大 三十日
|
74
|
+
|
75
|
+
枯薄 ここら よ 昔 不破の関
|
76
|
+
|
77
|
+
元日 の 人通り と は なり に けり
|
78
|
+
|
79
|
+
春風 に こぼれ て 赤 し 歯磨粉
|
80
|
+
|
81
|
+
春 の 夜 や 屏風 の 陰 に 物 の 息
|
62
82
|
|
63
83
|
```
|
64
84
|
|
@@ -66,8 +86,6 @@
|
|
66
86
|
|
67
87
|
```python
|
68
88
|
|
69
|
-
# coding:utf-8
|
70
|
-
|
71
89
|
import numpy as np
|
72
90
|
|
73
91
|
import codecs
|
@@ -76,24 +94,22 @@
|
|
76
94
|
|
77
95
|
from keras.models import Model
|
78
96
|
|
97
|
+
import sys
|
98
|
+
|
79
99
|
|
80
100
|
|
81
101
|
#データの読み込み
|
82
102
|
|
83
|
-
with open(r'/home/
|
103
|
+
with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
|
84
|
-
|
104
|
+
|
85
|
-
poems = f.readline()
|
105
|
+
poems = f.read().splitlines()
|
86
|
-
|
87
|
-
while poems:
|
88
|
-
|
89
|
-
print (poems)
|
90
|
-
|
91
|
-
poems = f.readline()
|
92
106
|
|
93
107
|
text = poems[0] # 1個目のデータ
|
94
108
|
|
95
109
|
print(text)
|
96
110
|
|
111
|
+
|
112
|
+
|
97
113
|
# コーパスの長さ
|
98
114
|
|
99
115
|
print('corpus length:', len(text))
|
@@ -120,7 +136,7 @@
|
|
120
136
|
|
121
137
|
#サンプルバッチ数
|
122
138
|
|
123
|
-
step =
|
139
|
+
step = 2
|
124
140
|
|
125
141
|
sentences = []
|
126
142
|
|
@@ -132,8 +148,6 @@
|
|
132
148
|
|
133
149
|
print('Sequences:', sentences)
|
134
150
|
|
135
|
-
print('next_chars:', next_chars)
|
136
|
-
|
137
151
|
#ベクトル化する
|
138
152
|
|
139
153
|
print('Vectorization...')
|
@@ -184,19 +198,21 @@
|
|
184
198
|
|
185
199
|
autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
|
186
200
|
|
201
|
+
#モデルの構造を見る
|
202
|
+
|
187
203
|
autoencoder.summary()
|
188
204
|
|
189
|
-
|
205
|
+
#アレイサイズの確認
|
190
206
|
|
191
207
|
print(x.shape)
|
192
208
|
|
193
|
-
#
|
209
|
+
#autoencoderの実行
|
194
210
|
|
195
211
|
autoencoder.fit(x, x,
|
196
212
|
|
197
|
-
epochs=50
|
213
|
+
epochs=50,
|
198
|
-
|
214
|
+
|
199
|
-
batch_size=
|
215
|
+
batch_size=3,
|
200
216
|
|
201
217
|
shuffle=False)
|
202
218
|
|
@@ -208,12 +224,10 @@
|
|
208
224
|
|
209
225
|
for t, char in enumerate(sentence):
|
210
226
|
|
211
|
-
x_haiku[0,
|
227
|
+
x_haiku[0,char_indices[char]] = 1.
|
212
228
|
|
213
229
|
sentence = sentence[:-1]
|
214
230
|
|
215
231
|
print(char)
|
216
232
|
|
217
|
-
|
218
|
-
|
219
233
|
```
|
4
文法訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,14 +124,10 @@
|
|
124
124
|
|
125
125
|
sentences = []
|
126
126
|
|
127
|
-
next_chars = []
|
128
|
-
|
129
127
|
for i in range(0, len(text) - maxlen, step):
|
130
128
|
|
131
129
|
sentences.append(text[i: i + maxlen])
|
132
130
|
|
133
|
-
next_chars.append(text[i + maxlen])
|
134
|
-
|
135
131
|
#学習する文字数を表示
|
136
132
|
|
137
133
|
print('Sequences:', sentences)
|
@@ -144,16 +140,12 @@
|
|
144
140
|
|
145
141
|
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
|
146
142
|
|
147
|
-
y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
|
148
|
-
|
149
143
|
for i, sentence in enumerate(sentences):
|
150
144
|
|
151
145
|
for t, char in enumerate(sentence):
|
152
146
|
|
153
147
|
x[i, t, char_indices[char]] = 1
|
154
148
|
|
155
|
-
y[i, char_indices[next_chars[i]]] = 1
|
156
|
-
|
157
149
|
#モデルを構築する工程に入る
|
158
150
|
|
159
151
|
print('Build model...')
|
@@ -204,7 +196,7 @@
|
|
204
196
|
|
205
197
|
epochs=500,
|
206
198
|
|
207
|
-
batch_size=2
|
199
|
+
batch_size=12,
|
208
200
|
|
209
201
|
shuffle=False)
|
210
202
|
|
3
文法訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,7 +116,7 @@
|
|
116
116
|
|
117
117
|
#テキストを17文字ずつ読み込む
|
118
118
|
|
119
|
-
maxlen =
|
119
|
+
maxlen = 2
|
120
120
|
|
121
121
|
#サンプルバッチ数
|
122
122
|
|
@@ -208,4 +208,20 @@
|
|
208
208
|
|
209
209
|
shuffle=False)
|
210
210
|
|
211
|
+
|
212
|
+
|
213
|
+
for i in range(17):
|
214
|
+
|
215
|
+
x_haiku = np.zeros((1, maxlen, len(chars)))
|
216
|
+
|
217
|
+
for t, char in enumerate(sentence):
|
218
|
+
|
219
|
+
x_haiku[0, t, char_indices[char]] = 1.
|
220
|
+
|
221
|
+
sentence = sentence[:-1]
|
222
|
+
|
223
|
+
print(char)
|
224
|
+
|
225
|
+
|
226
|
+
|
211
227
|
```
|
2
読みやすくしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,126 +208,4 @@
|
|
208
208
|
|
209
209
|
shuffle=False)
|
210
210
|
|
211
|
-
|
212
|
-
|
213
|
-
#モデルの構造を保存
|
214
|
-
|
215
|
-
model_json = autoencoder.to_json()
|
216
|
-
|
217
|
-
with open('keras_AE.json', 'w') as json_file:
|
218
|
-
|
219
|
-
json_file.write(model_json)
|
220
|
-
|
221
|
-
#学習済みモデルの重みを保存
|
222
|
-
|
223
|
-
autoencoder.save_weights('AE.h5')
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
API_TOKEN = "xoxp-236860929750-418287614128-462130918069-71e23e344e08baad360681ed400c007e"
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
import json
|
232
|
-
|
233
|
-
from collections import OrderedDict
|
234
|
-
|
235
|
-
import MeCab
|
236
|
-
|
237
|
-
import codecs
|
238
|
-
|
239
|
-
from slackbot.bot import default_reply
|
240
|
-
|
241
|
-
from slackbot.bot import Bot
|
242
|
-
|
243
|
-
import numpy as np
|
244
|
-
|
245
|
-
import os
|
246
|
-
|
247
|
-
import io, sys
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
252
|
-
|
253
|
-
tagger = MeCab.Tagger('mecabrc')
|
254
|
-
|
255
|
-
#モデルの構造を読む
|
256
|
-
|
257
|
-
word2id = json.load(open("keras_AE.json", "r"))
|
258
|
-
|
259
|
-
model = autoencoder(len(word2id))
|
260
|
-
|
261
|
-
#モデルをロードする
|
262
|
-
|
263
|
-
loaded_model = model_from_json(word2id)
|
264
|
-
|
265
|
-
#重みを適用する
|
266
|
-
|
267
|
-
loded_model.load_weights('AE.h5')
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
@default_reply
|
272
|
-
|
273
|
-
def replay_message(message):
|
274
|
-
|
275
|
-
parsed_sentence = []
|
276
|
-
|
277
|
-
try:
|
278
|
-
|
279
|
-
for chunk in tagger.parse(message.body["text"].encode("utf-8")).splitlines()[:-1]:
|
280
|
-
|
281
|
-
(surface, feature) = chunk.decode("utf-8").split('\t')
|
282
|
-
|
283
|
-
parsed_sentence.append(surface)
|
284
|
-
|
285
|
-
parsed_sentence = ["<start>"] + parsed_sentence + ["<eos>"]
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
ids = []
|
290
|
-
|
291
|
-
for word in parsed_sentence:
|
292
|
-
|
293
|
-
if word in word2id:
|
294
|
-
|
295
|
-
id = word2id[word]
|
296
|
-
|
297
|
-
ids.append(id)
|
298
|
-
|
299
|
-
else:
|
300
|
-
|
301
|
-
ids.append(0)
|
302
|
-
|
303
|
-
ids_question = ids
|
304
|
-
|
305
|
-
sentence = "".join(model.generate_sentence(ids_question, dictionary=id2word)).encode("utf-8")
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
sentence = sentence.replace("◯", "HAIJIN")
|
310
|
-
|
311
|
-
message.reply(sentence)
|
312
|
-
|
313
|
-
except Exception as e:
|
314
|
-
|
315
|
-
print (e)
|
316
|
-
|
317
|
-
message.reply("解析できなかったのでもう一度おねがいします。")
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
def main():
|
322
|
-
|
323
|
-
bot = Bot()
|
324
|
-
|
325
|
-
bot.run()
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
if __name__ == "__main__":
|
330
|
-
|
331
|
-
main()
|
332
|
-
|
333
211
|
```
|
1
詳細の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -38,9 +38,11 @@
|
|
38
38
|
|
39
39
|
と出ます。
|
40
40
|
|
41
|
-
|
41
|
+
|
42
|
-
|
42
|
+
|
43
|
-
|
43
|
+
レイヤに実際に入力を与えていないようです。
|
44
|
+
|
45
|
+
[GitHub](https://stackoverflow.com/questions/44852153/layer-called-with-an-input-that-isnt-a-symbolic-tensor-keras)
|
44
46
|
|
45
47
|
|
46
48
|
|