def test(self, data, label='Test'): N = int(math.ceil(len(data) / self.batch_size)) cost = 0 x = np.ndarray([self.batch_size, self.edim], dtype=np.float32) time = np.ndarray([self.batch_size, self.mem_size], dtype=np.int32) target = np.zeros([self.batch_size, self.nwords]) context = np.ndarray([self.batch_size, self.mem_size]) x.fill(self.init_hid) for t in xrange(self.mem_size): time[:, t].fill(t) if self.show: from util import ProgressBar bar = ProgressBar(label, max=N) m = self.mem_size for idx in xrange(N): if self.show: bar.next() target.fill(0) for b in xrange(self.batch_size): target[b][data[m]] = 1 context[b] = data[m - self.mem_size:m] m += 1 if m >= len(data): m = self.mem_size loss = self.sess.run([self.loss], feed_dict={self.input: x, self.time: time, self.target: target, self.context: context}) cost += np.sum(loss) if self.show: bar.finish() return cost / N / self.batch_size
def train(self, data): N = int(math.ceil(len(data) / self.batch_size)) cost = 0 x = np.ndarray([self.batch_size, self.edim], dtype=np.float32) time = np.ndarray([self.batch_size, self.mem_size], dtype=np.int32) target = np.zeros([self.batch_size, self.nwords]) context = np.ndarray([self.batch_size, self.mem_size]) x.fill(self.init_hid) for t in xrange(self.mem_size): time[:, t].fill(t) if self.show: from util import ProgressBar bar = ProgressBar('Train', max=N) for idx in xrange(N): if self.show: bar.next() target.fill(0) for b in xrange(self.batch_size): m = random.randrange(self.mem_size, len(data)) target[b][data[m]] = 1 context[b] = data[m - self.mem_size:m] _, loss, self.step = self.sess.run([self.optim, self.loss, self.global_step], feed_dict={ self.input: x, self.time: time, self.target: target, self.context: context}) cost += np.sum(loss) if self.show: bar.finish() return cost / N / self.batch_size