summaryrefslogtreecommitdiff
path: root/train.py
diff options
context:
space:
mode:
authorAlex Auvolat <alex.auvolat@ens.fr>2015-06-15 13:57:37 -0400
committerAlex Auvolat <alex.auvolat@ens.fr>2015-06-15 13:57:37 -0400
commit211c2272c544ab0bbf7b87b374736a71c790ac8e (patch)
tree802557e488eff0362c7c5928c1c3d4ec402fa54d /train.py
parentbbbf8861503b6102bbd52af3dbd4041cd3560562 (diff)
downloadtext-rnn-211c2272c544ab0bbf7b87b374736a71c790ac8e.tar.gz
text-rnn-211c2272c544ab0bbf7b87b374736a71c790ac8e.zip
Put GenText in separate file
Diffstat (limited to 'train.py')
-rwxr-xr-xtrain.py60
1 files changed, 5 insertions, 55 deletions
diff --git a/train.py b/train.py
index a8c246a..ddd1d0c 100755
--- a/train.py
+++ b/train.py
@@ -22,7 +22,7 @@ from blocks.model import Model
from blocks.algorithms import GradientDescent
import datastream
-# from apply_model import Apply
+import gentext
logging.basicConfig(level='INFO')
logger = logging.getLogger(__name__)
@@ -36,57 +36,6 @@ if __name__ == "__main__":
model_name = sys.argv[1]
config = importlib.import_module('%s' % model_name)
-
-class GenText(SimpleExtension):
- def __init__(self, model, init_text, max_bytes, **kwargs):
- super(GenText, self).__init__(**kwargs)
-
- self.init_text = init_text
- self.max_bytes = max_bytes
-
- out = model.out[:, -1, :] / numpy.float32(config.sample_temperature)
- prob = tensor.nnet.softmax(out)
-
- cg = ComputationGraph([prob])
- assert(len(cg.inputs) == 1)
- assert(cg.inputs[0].name == 'bytes')
-
- state_vars = [theano.shared(v[0:1, :].zeros_like().eval(), v.name+'-gen')
- for v, _ in model.states]
- givens = [(v, x) for (v, _), x in zip(model.states, state_vars)]
- updates= [(x, upd) for x, (_, upd) in zip(state_vars, model.states)]
-
- self.f = theano.function(inputs=cg.inputs, outputs=[prob],
- givens=givens, updates=updates)
- self.reset_states = theano.function(inputs=[], outputs=[],
- updates=[(v, v.zeros_like()) for v in state_vars])
-
- def do(self, which_callback, *args):
-
- print "Sample:"
- print "-------"
-
- self.reset_states()
-
- v = numpy.array([ord(i) for i in self.init_text],
- dtype='int16')[None, :]
- prob, = self.f(v)
-
- sys.stdout.write(self.init_text)
- while v.shape[1] < self.max_bytes:
- prob = prob / 1.00001
- pred = numpy.random.multinomial(1, prob[0, :]).nonzero()[0][0]
-
- v = numpy.concatenate([v, pred[None, None]], axis=1)
- sys.stdout.write(chr(int(pred)))
- sys.stdout.flush()
-
- prob, = self.f(pred[None, None])
- print
- print "-------"
- print
-
-
class ResetStates(SimpleExtension):
def __init__(self, state_vars, **kwargs):
super(ResetStates, self).__init__(**kwargs)
@@ -142,9 +91,10 @@ def train_model(m, train_stream, dump_path=None):
server_url='http://eos21:4201/',
every_n_epochs=1, after_epoch=False),
- GenText(m, '\nalex\ttu crois ?\n', config.sample_len,
- every_n_epochs=config.sample_freq,
- after_epoch=False, before_training=True),
+ gentext.GenText(m, '\nalex\ttu crois ?\n',
+ config.sample_len, config.sample_temperature,
+ every_n_epochs=config.sample_freq,
+ after_epoch=False, before_training=True),
ResetStates([v for v, _ in m.states], after_epoch=True)
]
)