質問編集履歴

13

文法訂正

2018/10/26 11:05

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -150,7 +150,7 @@
150
150
 
151
151
  shuffle=False)
152
152
 
153
- bunsyo = autoencoder.predict(self, x, batch_size=10, verbose=1, steps=None)
153
+ bunsyo = autoencoder.predict(x[1:17], batch_size=10, verbose=1, steps=None)
154
154
 
155
155
  print(bunsyo)
156
156
 

12

追記

2018/10/26 11:05

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -1,14 +1,28 @@
1
- autoencoderで、学習した結果(decoded_3)を出力したいのですが、
1
+ autoencoderで、学習した結果を出力したいのですが、
2
2
 
3
3
 
4
4
 
5
5
  ```python
6
6
 
7
- 1/1 [==============================] - 0s 163ms/step - loss: 16.1181
7
+ [[[0. 0. 0. 0. 0.24245892 0.
8
8
 
9
- (1, 2, 5)
9
+ 0. 0. 0. ]
10
10
 
11
+ [0. 0. 0. 0. 0. 0.
12
+
13
+ 0. 0. 0.15186222]]
14
+
15
+
16
+
11
- Tensor("dense_7/Relu:0", shape=(?, 2, 5), dtype=float32)
17
+ [[0. 0. 0. 0. 0. 0.
18
+
19
+ 0. 0.27800256 0. ]
20
+
21
+ [0. 0.14638866 0. 0. 0. 0.
22
+
23
+ 0. 0. 0. ]]]
24
+
25
+
12
26
 
13
27
  ```
14
28
 
@@ -136,6 +150,8 @@
136
150
 
137
151
  shuffle=False)
138
152
 
139
- autoencoder.predict(self, x, batch_size=10, verbose=1, steps=None)
153
+ bunsyo = autoencoder.predict(self, x, batch_size=10, verbose=1, steps=None)
154
+
155
+ print(bunsyo)
140
156
 
141
157
  ```

11

文法訂正

2018/10/26 10:56

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -136,8 +136,6 @@
136
136
 
137
137
  shuffle=False)
138
138
 
139
-
140
-
141
- print(decoded_3)
139
+ autoencoder.predict(self, x, batch_size=10, verbose=1, steps=None)
142
140
 
143
141
  ```

10

文法訂正

2018/10/26 10:37

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -138,6 +138,6 @@
138
138
 
139
139
 
140
140
 
141
- print()
141
+ print(decoded_3)
142
142
 
143
143
  ```

9

タイトルの改善

2018/10/26 06:25

投稿

yep
yep

スコア45

test CHANGED
@@ -1 +1 @@
1
- pythonname 'outputs' is not defined
1
+ 学習したデータを可視化きません
test CHANGED
@@ -1,22 +1,20 @@
1
- autoencoderで、学習した結果(outputs)を出力したいのですが、
1
+ autoencoderで、学習した結果(decoded_3)を出力したいのですが、
2
2
 
3
3
 
4
4
 
5
5
  ```python
6
6
 
7
- print(outputs)
7
+ 1/1 [==============================] - 0s 163ms/step - loss: 16.1181
8
8
 
9
+ (1, 2, 5)
10
+
9
- NameError: name 'outputs' is not defined
11
+ Tensor("dense_7/Relu:0", shape=(?, 2, 5), dtype=float32)
10
12
 
11
13
  ```
12
14
 
15
+ となってしまいます。
13
16
 
14
17
 
15
- np.zeroのx_haikuを出力すると、34行の[0,0,0...0,0,0]のような配列が出力されました。
16
-
17
- そこでenumerateで配列を文字に復元し、文字が出力されるはずですが、17文字が出力されません。
18
-
19
- 複数の文字を出力したいと考えています。
20
18
 
21
19
 
22
20
 
@@ -46,45 +44,13 @@
46
44
 
47
45
 
48
46
 
49
- 追記:
50
-
51
- ValueError: Error when checking target: expected dense_7 to have shape (2, 12) but got array with shape (2, 5)
52
-
53
- と出ていましたが、
54
-
55
-
56
-
57
- ```ここに言語を入力
58
-
59
- decoded = Dense(12, activation='relu')(encoded)
60
-
61
- ```
62
-
63
-
64
-
65
- このエラーは、データセットの行数によって変化するようです。
66
-
67
- 例えば、上記のpoem.txtでは、
68
-
69
-
70
-
71
- ```ここに言語を入力
72
-
73
- decoded = Dense(5, activation='relu')(encoded)
74
-
75
- ```
76
-
77
-
78
-
79
- に変更する必要があるようです。
80
-
81
-
82
-
83
47
  ```python
84
48
 
85
49
  import numpy as np
86
50
 
87
51
  import codecs
52
+
53
+ import keras
88
54
 
89
55
  from keras.layers import Activation, Dense, Input
90
56
 
@@ -94,35 +60,25 @@
94
60
 
95
61
 
96
62
 
97
- #データの読み込み
98
-
99
- with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
63
+ with open(r'./Desktop/haiku.txt', encoding='utf-8') as f:
100
64
 
101
65
  poems = f.readlines()
102
66
 
103
67
  for p in poems:
104
68
 
105
- s = p.rstrip() # 改行を除去
69
+ s = p.rstrip()
106
70
 
107
71
  s = s.split(' ')
108
72
 
109
73
 
110
74
 
111
- # 文字をID変換
75
+ chars = sorted(list(set(poems)))
112
76
 
113
77
  char_indices = dict((c, i) for i, c in enumerate(chars))
114
78
 
115
- # IDから文字へ変換
79
+ maxlen = 2
116
80
 
117
- indices_char = dict((i, c) for i, c in enumerate(chars))
118
-
119
- #テキストを2文字ずつ読み込む
120
-
121
- maxlen = 4
122
-
123
- #サンプルバッチ数
124
-
125
- step = 2
81
+ step = 3
126
82
 
127
83
  sentences = []
128
84
 
@@ -132,7 +88,11 @@
132
88
 
133
89
 
134
90
 
135
- #ベクトル化する
91
+ input_word = Input(shape=(maxlen, len(chars)))
92
+
93
+ indices_char = dict((i, c) for i, c in enumerate(chars))
94
+
95
+
136
96
 
137
97
  x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
138
98
 
@@ -144,54 +104,40 @@
144
104
 
145
105
 
146
106
 
147
- #encoderの次元
148
-
149
107
  encoding_dim = 128
150
-
151
- #入力用の変数
152
108
 
153
109
  input_word = Input(shape=(maxlen, len(chars)))
154
110
 
155
- #入力された語がencodeされたものを格納する
111
+ encoded_1 = Dense(128, activation='relu')(input_word)
156
112
 
157
- encoded = Dense(128, activation='relu')(input_word)
113
+ encoded_2 = Dense(64, activation='relu')(encoded_1)
158
114
 
159
- encoded = Dense(64, activation='relu')(encoded)
115
+ encoded_3 = Dense(32, activation='relu')(encoded_2)
160
116
 
161
- encoded = Dense(32, activation='relu')(encoded)
117
+ latent = Dense(8, activation='relu')(encoded_3)
162
118
 
163
- latent = Dense(8, activation='relu')(encoded)
119
+ decoded_1 = Dense(32, activation='relu')(latent)
164
120
 
165
- #encodeされたデータを再構成
121
+ decoded_2 = Dense(64, activation='relu')(decoded_1)
166
122
 
167
- decoded = Dense(32, activation='relu')(latent)
123
+ decoded_3 = Dense(5, activation='relu')(encoded_2)
168
124
 
169
- decoded = Dense(64, activation='relu')(decoded)
170
-
171
- decoded = Dense(39065, activation='relu')(encoded)
172
-
173
- autoencoder = Model(inputs=input_word, outputs=decoded)
125
+ autoencoder = Model(inputs=input_word, outputs=decoded_3)
174
-
175
- # #Adamで最適化、loss関数をcategorical_crossentropy
176
126
 
177
127
  autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
178
-
179
- #アレイサイズの確認
180
-
181
- print(x.shape)
182
128
 
183
129
 
184
130
 
185
131
  autoencoder.fit(x, x,
186
132
 
187
- epochs=50,
133
+ epochs=1,
188
134
 
189
- batch_size=3,
135
+ batch_size=150,
190
136
 
191
137
  shuffle=False)
192
138
 
193
139
 
194
140
 
195
- print(outputs)
141
+ print()
196
142
 
197
143
  ```

8

タイトルの改善

2018/10/26 06:24

投稿

yep
yep

スコア45

test CHANGED
@@ -1 +1 @@
1
- name 'outputs' is not defined
1
+ pythonでname 'outputs' is not defined
test CHANGED
File without changes

7

タイトルの改善

2018/10/26 05:41

投稿

yep
yep

スコア45

test CHANGED
@@ -1 +1 @@
1
- 複数の文字を出力したい
1
+ name 'outputs' is not defined
test CHANGED
@@ -1,20 +1,12 @@
1
- autoencoderで、学習した結果を出力したいのですが、
1
+ autoencoderで、学習した結果(outputs)を出力したいのですが、
2
2
 
3
3
 
4
4
 
5
5
  ```python
6
6
 
7
- for i in range(17):
7
+ print(outputs)
8
8
 
9
- x_haiku = np.zeros((1, maxlen, len(chars)))
10
-
11
- for t, char in enumerate(sentence):
9
+ NameError: name 'outputs' is not defined
12
-
13
- x_haiku[0,char_indices[char]] = 1.
14
-
15
- sentence = sentence[:-1]
16
-
17
- print(char)
18
10
 
19
11
  ```
20
12
 
@@ -200,16 +192,6 @@
200
192
 
201
193
 
202
194
 
203
- for i in range(17):
204
-
205
- x_haiku = np.zeros((1, maxlen, len(chars)))
206
-
207
- for t, char in enumerate(sentence):
208
-
209
- x_haiku[0,char_indices[char]] = 1.
210
-
211
- sentence = sentence[:-1]
212
-
213
- print(char)
195
+ print(outputs)
214
196
 
215
197
  ```

6

文法訂正

2018/10/26 05:41

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
  #テキストを2文字ずつ読み込む
128
128
 
129
- maxlen = 2
129
+ maxlen = 4
130
130
 
131
131
  #サンプルバッチ数
132
132
 

5

追記

2018/10/26 04:39

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,7 @@
56
56
 
57
57
  追記:
58
58
 
59
- ValueError: Error when checking target: expected dense_7 to have shape (2, 12) but got array with shape (2, 39065)
59
+ ValueError: Error when checking target: expected dense_7 to have shape (2, 12) but got array with shape (2, 5)
60
60
 
61
61
  と出ていましたが、
62
62
 
@@ -68,150 +68,144 @@
68
68
 
69
69
  ```
70
70
 
71
+
72
+
73
+ このエラーは、データセットの行数によって変化するようです。
74
+
71
-
75
+ 例えば、上記のpoem.txtでは、
76
+
77
+
72
78
 
73
79
  ```ここに言語を入力
74
80
 
81
+ decoded = Dense(5, activation='relu')(encoded)
82
+
83
+ ```
84
+
85
+
86
+
87
+ に変更する必要があるようです。
88
+
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.readlines()
110
+
111
+ for p in poems:
112
+
113
+ s = p.rstrip() # 改行を除去
114
+
115
+ s = s.split(' ')
116
+
117
+
118
+
119
+ # 文字をID変換
120
+
121
+ char_indices = dict((c, i) for i, c in enumerate(chars))
122
+
123
+ # IDから文字へ変換
124
+
125
+ indices_char = dict((i, c) for i, c in enumerate(chars))
126
+
127
+ #テキストを2文字ずつ読み込む
128
+
129
+ maxlen = 2
130
+
131
+ #サンプルバッチ数
132
+
133
+ step = 2
134
+
135
+ sentences = []
136
+
137
+ for i in range(0, len(poems) - maxlen, step):
138
+
139
+ sentences.append(poems[i: i + maxlen])
140
+
141
+
142
+
143
+ #ベクトル化する
144
+
145
+ x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
146
+
147
+ for i, sentence in enumerate(sentences):
148
+
149
+ for t, char in enumerate(sentence):
150
+
151
+ x[i, t, char_indices[char]] = 10
152
+
153
+
154
+
155
+ #encoderの次元
156
+
157
+ encoding_dim = 128
158
+
159
+ #入力用の変数
160
+
161
+ input_word = Input(shape=(maxlen, len(chars)))
162
+
163
+ #入力された語がencodeされたものを格納する
164
+
165
+ encoded = Dense(128, activation='relu')(input_word)
166
+
167
+ encoded = Dense(64, activation='relu')(encoded)
168
+
169
+ encoded = Dense(32, activation='relu')(encoded)
170
+
171
+ latent = Dense(8, activation='relu')(encoded)
172
+
173
+ #encodeされたデータを再構成
174
+
175
+ decoded = Dense(32, activation='relu')(latent)
176
+
177
+ decoded = Dense(64, activation='relu')(decoded)
178
+
75
179
  decoded = Dense(39065, activation='relu')(encoded)
76
180
 
77
- ```
78
-
79
- このエラーは、データセットの行数によって変化するようです。
80
-
81
- 例えば、上記のpoem.txtでは、
82
-
83
-
84
-
85
- ```ここに言語を入力
86
-
87
- decoded = Dense(5, activation='relu')(encoded)
181
+ autoencoder = Model(inputs=input_word, outputs=decoded)
88
-
89
- ```
182
+
90
-
91
-
92
-
93
- に変更する必要があるようです。
94
-
95
-
96
-
97
- ```python
98
-
99
- import numpy as np
100
-
101
- import codecs
102
-
103
- from keras.layers import Activation, Dense, Input
183
+ # #Adamで最適化、loss関数をcategorical_crossentropy
104
-
105
- from keras.models import Model
184
+
106
-
107
- import sys
108
-
109
-
110
-
111
- #データの読み込み
112
-
113
- with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
185
+ autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
186
+
114
-
187
+ #アレイサイズの確認
188
+
115
- poems = f.readlines()
189
+ print(x.shape)
190
+
191
+
192
+
116
-
193
+ autoencoder.fit(x, x,
194
+
195
+ epochs=50,
196
+
197
+ batch_size=3,
198
+
199
+ shuffle=False)
200
+
201
+
202
+
117
- for p in poems:
203
+ for i in range(17):
118
-
119
- s = p.rstrip() # 改行を除去
204
+
120
-
121
- s = s.split(' ')
122
-
123
-
124
-
125
- # 文字をID変換
126
-
127
- char_indices = dict((c, i) for i, c in enumerate(chars))
128
-
129
- # IDから文字へ変換
130
-
131
- indices_char = dict((i, c) for i, c in enumerate(chars))
132
-
133
- #テキストを2文字ずつ読み込む
134
-
135
- maxlen = 2
136
-
137
- #サンプルバッチ数
138
-
139
- step = 2
140
-
141
- sentences = []
142
-
143
- for i in range(0, len(poems) - maxlen, step):
144
-
145
- sentences.append(poems[i: i + maxlen])
146
-
147
-
148
-
149
- #ベクトル化する
150
-
151
- x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
205
+ x_haiku = np.zeros((1, maxlen, len(chars)))
152
-
153
- for i, sentence in enumerate(sentences):
154
206
 
155
207
  for t, char in enumerate(sentence):
156
208
 
157
- x[i, t, char_indices[char]] = 10
158
-
159
-
160
-
161
- #encoderの次元
162
-
163
- encoding_dim = 128
164
-
165
- #入力用の変数
166
-
167
- input_word = Input(shape=(maxlen, len(chars)))
168
-
169
- #入力された語がencodeされたものを格納する
170
-
171
- encoded = Dense(128, activation='relu')(input_word)
172
-
173
- encoded = Dense(64, activation='relu')(encoded)
174
-
175
- encoded = Dense(32, activation='relu')(encoded)
176
-
177
- latent = Dense(8, activation='relu')(encoded)
178
-
179
- #encodeされたデータを再構成
180
-
181
- decoded = Dense(32, activation='relu')(latent)
182
-
183
- decoded = Dense(64, activation='relu')(decoded)
184
-
185
- decoded = Dense(39065, activation='relu')(encoded)
186
-
187
- autoencoder = Model(inputs=input_word, outputs=decoded)
188
-
189
- # #Adamで最適化、loss関数をcategorical_crossentropy
190
-
191
- autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
192
-
193
- #アレイサイズの確認
194
-
195
- print(x.shape)
196
-
197
-
198
-
199
- autoencoder.fit(x, x,
200
-
201
- epochs=50,
202
-
203
- batch_size=3,
204
-
205
- shuffle=False)
206
-
207
-
208
-
209
- for i in range(17):
210
-
211
- x_haiku = np.zeros((1, maxlen, len(chars)))
212
-
213
- for t, char in enumerate(sentence):
214
-
215
209
  x_haiku[0,char_indices[char]] = 1.
216
210
 
217
211
  sentence = sentence[:-1]

4

文法訂正

2018/10/26 03:14

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -84,134 +84,134 @@
84
84
 
85
85
  ```ここに言語を入力
86
86
 
87
+ decoded = Dense(5, activation='relu')(encoded)
88
+
89
+ ```
90
+
91
+
92
+
93
+ に変更する必要があるようです。
94
+
95
+
96
+
97
+ ```python
98
+
99
+ import numpy as np
100
+
101
+ import codecs
102
+
103
+ from keras.layers import Activation, Dense, Input
104
+
105
+ from keras.models import Model
106
+
107
+ import sys
108
+
109
+
110
+
111
+ #データの読み込み
112
+
113
+ with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
114
+
115
+ poems = f.readlines()
116
+
117
+ for p in poems:
118
+
119
+ s = p.rstrip() # 改行を除去
120
+
121
+ s = s.split(' ')
122
+
123
+
124
+
125
+ # 文字をID変換
126
+
127
+ char_indices = dict((c, i) for i, c in enumerate(chars))
128
+
129
+ # IDから文字へ変換
130
+
131
+ indices_char = dict((i, c) for i, c in enumerate(chars))
132
+
133
+ #テキストを2文字ずつ読み込む
134
+
135
+ maxlen = 2
136
+
137
+ #サンプルバッチ数
138
+
139
+ step = 2
140
+
141
+ sentences = []
142
+
143
+ for i in range(0, len(poems) - maxlen, step):
144
+
145
+ sentences.append(poems[i: i + maxlen])
146
+
147
+
148
+
149
+ #ベクトル化する
150
+
151
+ x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
152
+
153
+ for i, sentence in enumerate(sentences):
154
+
155
+ for t, char in enumerate(sentence):
156
+
157
+ x[i, t, char_indices[char]] = 10
158
+
159
+
160
+
161
+ #encoderの次元
162
+
163
+ encoding_dim = 128
164
+
165
+ #入力用の変数
166
+
167
+ input_word = Input(shape=(maxlen, len(chars)))
168
+
169
+ #入力された語がencodeされたものを格納する
170
+
171
+ encoded = Dense(128, activation='relu')(input_word)
172
+
173
+ encoded = Dense(64, activation='relu')(encoded)
174
+
175
+ encoded = Dense(32, activation='relu')(encoded)
176
+
177
+ latent = Dense(8, activation='relu')(encoded)
178
+
179
+ #encodeされたデータを再構成
180
+
181
+ decoded = Dense(32, activation='relu')(latent)
182
+
183
+ decoded = Dense(64, activation='relu')(decoded)
184
+
87
185
  decoded = Dense(39065, activation='relu')(encoded)
88
186
 
89
- ```
90
-
91
-
92
-
93
- に変更する必要があるようです。
94
-
95
-
96
-
97
- ```python
98
-
99
- import numpy as np
100
-
101
- import codecs
102
-
103
- from keras.layers import Activation, Dense, Input
187
+ autoencoder = Model(inputs=input_word, outputs=decoded)
104
-
188
+
105
- from keras.models import Model
189
+ # #Adamで最適化、loss関数をcategorical_crossentropy
106
-
107
- import sys
190
+
108
-
109
-
110
-
111
- #データの読み込み
112
-
113
- with open(r'/home/hoge/Desktop/data/haiku.txt', encoding='utf-8') as f:
191
+ autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
192
+
114
-
193
+ #アレイサイズの確認
194
+
115
- poems = f.readlines()
195
+ print(x.shape)
196
+
197
+
198
+
116
-
199
+ autoencoder.fit(x, x,
200
+
201
+ epochs=50,
202
+
203
+ batch_size=3,
204
+
205
+ shuffle=False)
206
+
207
+
208
+
117
- for p in poems:
209
+ for i in range(17):
118
-
119
- s = p.rstrip() # 改行を除去
210
+
120
-
121
- s = s.split(' ')
122
-
123
-
124
-
125
- # 文字をID変換
126
-
127
- char_indices = dict((c, i) for i, c in enumerate(chars))
128
-
129
- # IDから文字へ変換
130
-
131
- indices_char = dict((i, c) for i, c in enumerate(chars))
132
-
133
- #テキストを2文字ずつ読み込む
134
-
135
- maxlen = 2
136
-
137
- #サンプルバッチ数
138
-
139
- step = 2
140
-
141
- sentences = []
142
-
143
- for i in range(0, len(poems) - maxlen, step):
144
-
145
- sentences.append(poems[i: i + maxlen])
146
-
147
-
148
-
149
- #ベクトル化する
150
-
151
- x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
211
+ x_haiku = np.zeros((1, maxlen, len(chars)))
152
-
153
- for i, sentence in enumerate(sentences):
154
212
 
155
213
  for t, char in enumerate(sentence):
156
214
 
157
- x[i, t, char_indices[char]] = 10
158
-
159
-
160
-
161
- #encoderの次元
162
-
163
- encoding_dim = 128
164
-
165
- #入力用の変数
166
-
167
- input_word = Input(shape=(maxlen, len(chars)))
168
-
169
- #入力された語がencodeされたものを格納する
170
-
171
- encoded = Dense(128, activation='relu')(input_word)
172
-
173
- encoded = Dense(64, activation='relu')(encoded)
174
-
175
- encoded = Dense(32, activation='relu')(encoded)
176
-
177
- latent = Dense(8, activation='relu')(encoded)
178
-
179
- #encodeされたデータを再構成
180
-
181
- decoded = Dense(32, activation='relu')(latent)
182
-
183
- decoded = Dense(64, activation='relu')(decoded)
184
-
185
- decoded = Dense(39065, activation='relu')(encoded)
186
-
187
- autoencoder = Model(inputs=input_word, outputs=decoded)
188
-
189
- # #Adamで最適化、loss関数をcategorical_crossentropy
190
-
191
- autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
192
-
193
- #アレイサイズの確認
194
-
195
- print(x.shape)
196
-
197
-
198
-
199
- autoencoder.fit(x, x,
200
-
201
- epochs=50,
202
-
203
- batch_size=3,
204
-
205
- shuffle=False)
206
-
207
-
208
-
209
- for i in range(17):
210
-
211
- x_haiku = np.zeros((1, maxlen, len(chars)))
212
-
213
- for t, char in enumerate(sentence):
214
-
215
215
  x_haiku[0,char_indices[char]] = 1.
216
216
 
217
217
  sentence = sentence[:-1]

3

追記

2018/10/26 03:13

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -76,9 +76,21 @@
76
76
 
77
77
  ```
78
78
 
79
-
79
+ このエラーは、データセットの行数によって変化するようです。
80
+
80
-
81
+ 例えば、上記のpoem.txtでは、
82
+
83
+
84
+
85
+ ```ここに言語を入力
86
+
87
+ decoded = Dense(39065, activation='relu')(encoded)
88
+
89
+ ```
90
+
91
+
92
+
81
- に変更すると、エラー解消されました
93
+ に変更する必要あるようです
82
94
 
83
95
 
84
96
 

2

コードを必要最小限に読みやすくしました

2018/10/26 03:12

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -108,17 +108,7 @@
108
108
 
109
109
  s = s.split(' ')
110
110
 
111
- # コーパスの長さ
111
+
112
-
113
- print('corpus length:', len(poems))
114
-
115
- # 文字数を数えるため、textをソート
116
-
117
- chars = sorted(list(set(poems)))
118
-
119
- # 全文字数の表示
120
-
121
- print('total chars:', len(chars))
122
112
 
123
113
  # 文字をID変換
124
114
 
@@ -128,7 +118,7 @@
128
118
 
129
119
  indices_char = dict((i, c) for i, c in enumerate(chars))
130
120
 
131
- #テキストを17文字ずつ読み込む
121
+ #テキストを2文字ずつ読み込む
132
122
 
133
123
  maxlen = 2
134
124
 
@@ -142,14 +132,10 @@
142
132
 
143
133
  sentences.append(poems[i: i + maxlen])
144
134
 
145
- #学習する文字数を表示
146
-
147
135
 
148
136
 
149
137
  #ベクトル化する
150
138
 
151
- print('Vectorization...')
152
-
153
139
  x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
154
140
 
155
141
  for i, sentence in enumerate(sentences):
@@ -158,9 +144,7 @@
158
144
 
159
145
  x[i, t, char_indices[char]] = 10
160
146
 
161
- #モデルを構築する工程に入る
147
+
162
-
163
- print('Build model...')
164
148
 
165
149
  #encoderの次元
166
150
 
@@ -178,8 +162,6 @@
178
162
 
179
163
  encoded = Dense(32, activation='relu')(encoded)
180
164
 
181
- #潜在変数(実質的な主成分分析)
182
-
183
165
  latent = Dense(8, activation='relu')(encoded)
184
166
 
185
167
  #encodeされたデータを再構成
@@ -196,15 +178,11 @@
196
178
 
197
179
  autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy')
198
180
 
199
- #モデルの構造を見る
200
-
201
- autoencoder.summary()
202
-
203
181
  #アレイサイズの確認
204
182
 
205
183
  print(x.shape)
206
184
 
207
- #autoencoderの実行
185
+
208
186
 
209
187
  autoencoder.fit(x, x,
210
188
 
@@ -214,17 +192,7 @@
214
192
 
215
193
  shuffle=False)
216
194
 
217
- #モデルの構造を保存
195
+
218
-
219
- model_json = autoencoder.to_json()
220
-
221
- with open('keras_AE.json', 'w') as json_file:
222
-
223
- json_file.write(model_json)
224
-
225
- #学習済みモデルの重みを保存
226
-
227
- autoencoder.save_weights('AE.h5')
228
196
 
229
197
  for i in range(17):
230
198
 

1

追記

2018/10/26 02:39

投稿

yep
yep

スコア45

test CHANGED
File without changes
test CHANGED
@@ -34,17 +34,19 @@
34
34
 
35
35
  poem.txtは、
36
36
 
37
-
37
+ ```ここに言語を入力
38
-
38
+
39
- 朝霧 の 中 に 九段 の ともし 哉
39
+ 朝霧 の 中 に 九段 の ともし 哉
40
-
40
+
41
- あたたか な 雨 が 降る なり 枯葎
41
+ あたたか な 雨 が 降る なり 枯葎
42
-
42
+
43
- 菜の花 や は つと 明るき 町 は づれ
43
+ 菜の花 や は つと 明るき 町 は づれ
44
-
44
+
45
- 秋風 や 伊予 へ 流る る 汐 の 音
45
+ 秋風 や 伊予 へ 流る る 汐 の 音
46
-
46
+
47
- 長閑 さ や 障子 の 穴 に 海 見え て
47
+ 長閑 さ や 障子 の 穴 に 海 見え て
48
+
49
+ ```
48
50
 
49
51
 
50
52
 
@@ -60,13 +62,21 @@
60
62
 
61
63
 
62
64
 
65
+ ```ここに言語を入力
66
+
63
- decoded = Dense(12, activation='relu')(encoded)
67
+ decoded = Dense(12, activation='relu')(encoded)
68
+
69
+ ```
64
70
 
65
71
 
66
72
 
67
-
73
+ ```ここに言語を入力
68
-
74
+
69
- decoded = Dense(39065, activation='relu')(encoded)
75
+ decoded = Dense(39065, activation='relu')(encoded)
76
+
77
+ ```
78
+
79
+
70
80
 
71
81
  に変更すると、エラーが解消されました。
72
82