質問編集履歴
3
ソースの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,13 +116,13 @@
|
|
116
116
|
|
117
117
|
```
|
118
118
|
|
119
|
-
```ここに言語を入力
|
120
119
|
|
121
120
|
|
122
121
|
|
123
122
|
|
124
123
|
|
125
124
|
|
125
|
+
```ここに言語を入力
|
126
126
|
|
127
127
|
from __future__ import absolute_import
|
128
128
|
|
@@ -194,12 +194,4 @@
|
|
194
194
|
|
195
195
|
softmax_loss_function = None
|
196
196
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
197
|
+
```
|
2
ソースの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -118,25 +118,9 @@
|
|
118
118
|
|
119
119
|
```ここに言語を入力
|
120
120
|
|
121
|
-
|
121
|
+
|
122
|
-
|
123
|
-
|
122
|
+
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
123
|
+
|
128
|
-
|
129
|
-
# you may not use this file except in compliance with the License.
|
130
|
-
|
131
|
-
# You may obtain a copy of the License at
|
132
|
-
|
133
|
-
#
|
134
|
-
|
135
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
"""Sequence-to-sequence model with an attention mechanism."""
|
140
124
|
|
141
125
|
|
142
126
|
|
@@ -212,95 +196,7 @@
|
|
212
196
|
|
213
197
|
|
214
198
|
|
215
|
-
|
199
|
+
|
216
|
-
|
217
|
-
with tf.device("/cpu:0"):
|
218
|
-
|
219
|
-
w = tf.get_variable("proj_w", [size, self.target_vocab_size])
|
220
|
-
|
221
|
-
w_t = tf.transpose(w)
|
222
|
-
|
223
|
-
b = tf.get_variable("proj_b", [self.target_vocab_size])
|
224
|
-
|
225
|
-
output_projection = (w, b)
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
def sampled_loss(inputs, labels):
|
232
|
-
|
233
|
-
with tf.device("/cpu:0"):
|
234
|
-
|
235
|
-
labels = tf.reshape(labels, [-1, 1])
|
236
|
-
|
237
|
-
return tf.nn.sampled_softmax_loss(w_t, b, inputs, labels, num_samples,
|
238
|
-
|
239
|
-
self.target_vocab_size)
|
240
|
-
|
241
|
-
softmax_loss_function = sampled_loss
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
single_cell = rnn_cell.GRUCell(size)
|
248
|
-
|
249
|
-
if use_lstm:
|
250
|
-
|
251
|
-
single_cell = rnn_cell.BasicLSTMCell(size)
|
252
|
-
|
253
|
-
cell = single_cell
|
254
|
-
|
255
|
-
if num_layers > 1:
|
256
|
-
|
257
|
-
cell = rnn_cell.MultiRNNCell([single_cell] * num_layers)
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
def seq2seq_f(encoder_inputs, decoder_inputs, do_decode):
|
264
|
-
|
265
|
-
return seq2seq.embedding_attention_seq2seq(
|
266
|
-
|
267
|
-
encoder_inputs, decoder_inputs, cell, source_vocab_size,
|
268
|
-
|
269
|
-
target_vocab_size, output_projection=output_projection,
|
270
|
-
|
271
|
-
feed_previous=do_decode)
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
self.encoder_inputs = []
|
276
|
-
|
277
|
-
self.decoder_inputs = []
|
278
|
-
|
279
|
-
self.target_weights = []
|
280
|
-
|
281
|
-
for i in xrange(buckets[-1][0]):
|
282
|
-
|
283
|
-
self.encoder_inputs.append(tf.placeholder(tf.int32, shape=[None],
|
284
|
-
|
285
|
-
name="encoder{0}".format(i)))
|
286
|
-
|
287
|
-
for i in xrange(buckets[-1][1] + 1):
|
288
|
-
|
289
|
-
self.decoder_inputs.append(tf.placeholder(tf.int32, shape=[None],
|
290
|
-
|
291
|
-
name="decoder{0}".format(i)))
|
292
|
-
|
293
|
-
self.target_weights.append(tf.placeholder(tf.float32, shape=[None],
|
294
|
-
|
295
|
-
name="weight{0}".format(i)))
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
targets = [self.decoder_inputs[i + 1]
|
302
|
-
|
303
|
-
for i in xrange(len(self.decoder_inputs) - 1)]
|
304
200
|
|
305
201
|
|
306
202
|
|
1
ソースの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -21,3 +21,289 @@
|
|
21
21
|
すべて、予測してくれるとは思ってはいないのですが、ここでエラーが出て進まないのですが、解決方法などを教えていただけると幸いです。
|
22
22
|
|
23
23
|
よろしくお願いいたします。
|
24
|
+
|
25
|
+
```ここに言語を入力
|
26
|
+
|
27
|
+
from __future__ import absolute_import
|
28
|
+
|
29
|
+
from __future__ import division
|
30
|
+
|
31
|
+
from __future__ import print_function
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
import MeCab
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
import math
|
40
|
+
|
41
|
+
import os
|
42
|
+
|
43
|
+
import random
|
44
|
+
|
45
|
+
import sys
|
46
|
+
|
47
|
+
import time
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
import tensorflow.python.platform
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
import numpy as np
|
58
|
+
|
59
|
+
from six.moves import xrange
|
60
|
+
|
61
|
+
import tensorflow as tf
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
import data_utils
|
66
|
+
|
67
|
+
from tensorflow.models.rnn.translate import seq2seq_model
|
68
|
+
|
69
|
+
from tensorflow.python.platform import gfile
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
tf.app.flags.DEFINE_float("learning_rate", 0.5, "Learning rate.")
|
76
|
+
|
77
|
+
tf.app.flags.DEFINE_float("learning_rate_decay_factor", 0.99,
|
78
|
+
|
79
|
+
"Learning rate decays by this much.")
|
80
|
+
|
81
|
+
tf.app.flags.DEFINE_float("max_gradient_norm", 5.0,
|
82
|
+
|
83
|
+
"Clip gradients to this norm.")
|
84
|
+
|
85
|
+
tf.app.flags.DEFINE_integer("batch_size", 4,
|
86
|
+
|
87
|
+
"Batch size to use during training.")
|
88
|
+
|
89
|
+
tf.app.flags.DEFINE_integer("size", 256, "Size of each model layer.")
|
90
|
+
|
91
|
+
tf.app.flags.DEFINE_integer("num_layers", 2, "Number of layers in the model.")
|
92
|
+
|
93
|
+
tf.app.flags.DEFINE_integer("in_vocab_size", 12500, "input vocabulary size.")
|
94
|
+
|
95
|
+
tf.app.flags.DEFINE_integer("out_vocab_size", 12500, "output vocabulary size.")
|
96
|
+
|
97
|
+
tf.app.flags.DEFINE_string("data_dir", "./datas", "Data directory")
|
98
|
+
|
99
|
+
tf.app.flags.DEFINE_string("train_dir", "./datas", "Training directory.")
|
100
|
+
|
101
|
+
tf.app.flags.DEFINE_integer("max_train_data_size", 0,
|
102
|
+
|
103
|
+
"Limit on the size of training data (0: no limit).")
|
104
|
+
|
105
|
+
tf.app.flags.DEFINE_integer("steps_per_checkpoint", 100,
|
106
|
+
|
107
|
+
"How many training steps to do per checkpoint.")
|
108
|
+
|
109
|
+
tf.app.flags.DEFINE_boolean("decode", False,
|
110
|
+
|
111
|
+
"Set to True for interactive decoding.")
|
112
|
+
|
113
|
+
tf.app.flags.DEFINE_boolean("self_test", False,
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
```
|
118
|
+
|
119
|
+
```ここに言語を入力
|
120
|
+
|
121
|
+
#!/usr/bin/env python
|
122
|
+
|
123
|
+
# -*- coding: utf-8 -*-
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
128
|
+
|
129
|
+
# you may not use this file except in compliance with the License.
|
130
|
+
|
131
|
+
# You may obtain a copy of the License at
|
132
|
+
|
133
|
+
#
|
134
|
+
|
135
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
"""Sequence-to-sequence model with an attention mechanism."""
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
from __future__ import absolute_import
|
144
|
+
|
145
|
+
from __future__ import division
|
146
|
+
|
147
|
+
from __future__ import print_function
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
import random
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
import numpy as np
|
156
|
+
|
157
|
+
from six.moves import xrange
|
158
|
+
|
159
|
+
import tensorflow as tf
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
from tensorflow.models.rnn import rnn_cell
|
164
|
+
|
165
|
+
from tensorflow.models.rnn import seq2seq
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
import data_utils
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
class Seq2SeqModel(object):
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def __init__(self, source_vocab_size, target_vocab_size, buckets, size,
|
180
|
+
|
181
|
+
num_layers, max_gradient_norm, batch_size, learning_rate,
|
182
|
+
|
183
|
+
learning_rate_decay_factor, use_lstm=False,
|
184
|
+
|
185
|
+
num_samples=512, forward_only=False):
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
self.source_vocab_size = source_vocab_size
|
190
|
+
|
191
|
+
self.target_vocab_size = target_vocab_size
|
192
|
+
|
193
|
+
self.buckets = buckets
|
194
|
+
|
195
|
+
self.batch_size = batch_size
|
196
|
+
|
197
|
+
self.learning_rate = tf.Variable(float(learning_rate), trainable=False)
|
198
|
+
|
199
|
+
self.learning_rate_decay_op = self.learning_rate.assign(
|
200
|
+
|
201
|
+
self.learning_rate * learning_rate_decay_factor)
|
202
|
+
|
203
|
+
self.global_step = tf.Variable(0, trainable=False)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
output_projection = None
|
210
|
+
|
211
|
+
softmax_loss_function = None
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
if num_samples > 0 and num_samples < self.target_vocab_size:
|
216
|
+
|
217
|
+
with tf.device("/cpu:0"):
|
218
|
+
|
219
|
+
w = tf.get_variable("proj_w", [size, self.target_vocab_size])
|
220
|
+
|
221
|
+
w_t = tf.transpose(w)
|
222
|
+
|
223
|
+
b = tf.get_variable("proj_b", [self.target_vocab_size])
|
224
|
+
|
225
|
+
output_projection = (w, b)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
def sampled_loss(inputs, labels):
|
232
|
+
|
233
|
+
with tf.device("/cpu:0"):
|
234
|
+
|
235
|
+
labels = tf.reshape(labels, [-1, 1])
|
236
|
+
|
237
|
+
return tf.nn.sampled_softmax_loss(w_t, b, inputs, labels, num_samples,
|
238
|
+
|
239
|
+
self.target_vocab_size)
|
240
|
+
|
241
|
+
softmax_loss_function = sampled_loss
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
single_cell = rnn_cell.GRUCell(size)
|
248
|
+
|
249
|
+
if use_lstm:
|
250
|
+
|
251
|
+
single_cell = rnn_cell.BasicLSTMCell(size)
|
252
|
+
|
253
|
+
cell = single_cell
|
254
|
+
|
255
|
+
if num_layers > 1:
|
256
|
+
|
257
|
+
cell = rnn_cell.MultiRNNCell([single_cell] * num_layers)
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
def seq2seq_f(encoder_inputs, decoder_inputs, do_decode):
|
264
|
+
|
265
|
+
return seq2seq.embedding_attention_seq2seq(
|
266
|
+
|
267
|
+
encoder_inputs, decoder_inputs, cell, source_vocab_size,
|
268
|
+
|
269
|
+
target_vocab_size, output_projection=output_projection,
|
270
|
+
|
271
|
+
feed_previous=do_decode)
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
self.encoder_inputs = []
|
276
|
+
|
277
|
+
self.decoder_inputs = []
|
278
|
+
|
279
|
+
self.target_weights = []
|
280
|
+
|
281
|
+
for i in xrange(buckets[-1][0]):
|
282
|
+
|
283
|
+
self.encoder_inputs.append(tf.placeholder(tf.int32, shape=[None],
|
284
|
+
|
285
|
+
name="encoder{0}".format(i)))
|
286
|
+
|
287
|
+
for i in xrange(buckets[-1][1] + 1):
|
288
|
+
|
289
|
+
self.decoder_inputs.append(tf.placeholder(tf.int32, shape=[None],
|
290
|
+
|
291
|
+
name="decoder{0}".format(i)))
|
292
|
+
|
293
|
+
self.target_weights.append(tf.placeholder(tf.float32, shape=[None],
|
294
|
+
|
295
|
+
name="weight{0}".format(i)))
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
targets = [self.decoder_inputs[i + 1]
|
302
|
+
|
303
|
+
for i in xrange(len(self.decoder_inputs) - 1)]
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
```
|