def sample(model, data, dev, nsamples=100, use_max=False): while True: cmd = input('Do you want to sample text from the model [y/n]') if cmd == 'n': return else: seed = input('Please input some seeding text, e.g., #include <c: ') inputs = [] for c in seed: x = np.zeros((1, data.vocab_size), dtype=np.float32) x[0, data.char_to_idx[c]] = 1 tx = tensor.from_numpy(x) tx.to_device(dev) inputs.append(tx) model.reset_states(dev) outputs = model(inputs) y = tensor.softmax(outputs[-1]) sys.stdout.write(seed) for i in range(nsamples): prob = tensor.to_numpy(y)[0] if use_max: cur = np.argmax(prob) else: cur = np.random.choice(data.vocab_size, 1, p=prob)[0] sys.stdout.write(data.idx_to_char[cur]) x = np.zeros((1, data.vocab_size), dtype=np.float32) x[0, cur] = 1 tx = tensor.from_numpy(x) tx.to_device(dev) outputs = model([tx]) y = tensor.softmax(outputs[-1])
def predict(self, queries: List[str]): print("Get queries") res = [] queries = [self._preprocess(ele) for ele in queries] for input_ids in queries: x = tensor.Tensor(device=self.dev, data=input_ids) out = [] for i in range(self.length): y = self._model.forward(x) y = autograd.reshape(y, y.shape[-2:])[-1, :] y = tensor.softmax(y) y = tensor.to_numpy(y)[0] y = np.argsort(y)[-1] out.append(y) y = np.array([y]).reshape([1, 1, -1]).astype(np.float32) y = tensor.Tensor(device=self.dev, data=y) x = tensor.concatenate([x, y], 2) result = self._postprocess(out) res.append(result) return res
img = preprocess(img) # sg_ir = sonnx.prepare(onnx_model) # run without graph # y = sg_ir.run([img]) logging.info("model compling...") dev = device.create_cuda_gpu() x = tensor.PlaceHolder(img.shape, device=dev) model = MyModel(onnx_model) model.compile([x], is_train=False, use_graph=True, sequential=True) # verifty the test # from utils import load_dataset # inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'model', 'test_shufflenetv2', # 'model.onnx')) # x_batch = tensor.Tensor(device=dev, data=inputs[0]) # outputs = sg_ir.run([x_batch]) # for ref_o, o in zip(ref_outputs, outputs): # np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) logging.info("model running...") x = tensor.Tensor(device=dev, data=img) y = model.forward(x) logging.info("postprocessing...") y = tensor.softmax(y) scores = tensor.to_numpy(y) scores = np.squeeze(scores) a = np.argsort(scores)[::-1] for i in a[0:5]: logging.info('class=%s ; probability=%f' % (labels[i], scores[i]))
def sample(model_path, nsamples=100, seed_text='', do_sample=True): with open(model_path, 'rb') as fd: d = pickle.load(fd) rnn_w = tensor.from_numpy(d['rnn_w']) idx_to_char = d['idx_to_char'] char_to_idx = d['char_to_idx'] vocab_size = len(idx_to_char) dense_w = tensor.from_numpy(d['dense_w']) dense_b = tensor.from_numpy(d['dense_b']) hidden_size = d['hidden_size'] num_stacks = d['num_stacks'] dropout = d['dropout'] cuda = device.create_cuda_gpu() rnn = layer.LSTM(name='lstm', hidden_size=hidden_size, num_stacks=num_stacks, dropout=dropout, input_sample_shape=(len(idx_to_char),)) rnn.to_device(cuda) rnn.param_values()[0].copy_data(rnn_w) dense = layer.Dense('dense', vocab_size, input_sample_shape=(hidden_size,)) dense.to_device(cuda) dense.param_values()[0].copy_data(dense_w) dense.param_values()[1].copy_data(dense_b) hx = tensor.Tensor((num_stacks, 1, hidden_size), cuda) cx = tensor.Tensor((num_stacks, 1, hidden_size), cuda) hx.set_value(0.0) cx.set_value(0.0) if len(seed_text) > 0: for c in seed_text: x = np.zeros((1, vocab_size), dtype=np.float32) x[0, char_to_idx[c]] = 1 tx = tensor.from_numpy(x) tx.to_device(cuda) inputs = [tx, hx, cx] outputs = rnn.forward(False, inputs) y = dense.forward(False, outputs[0]) y = tensor.softmax(y) hx = outputs[1] cx = outputs[2] sys.stdout.write(seed_text) else: y = tensor.Tensor((1, vocab_size), cuda) y.set_value(1.0 / vocab_size) for i in range(nsamples): y.to_host() prob = tensor.to_numpy(y)[0] if do_sample: cur = np.random.choice(vocab_size, 1, p=prob)[0] else: cur = np.argmax(prob) sys.stdout.write(idx_to_char[cur]) x = np.zeros((1, vocab_size), dtype=np.float32) x[0, cur] = 1 tx = tensor.from_numpy(x) tx.to_device(cuda) inputs = [tx, hx, cx] outputs = rnn.forward(False, inputs) y = dense.forward(False, outputs[0]) y = tensor.softmax(y) hx = outputs[1] cx = outputs[2] print('')
def sample(model_path, nsamples=100, seed_text='', do_sample=True): with open(model_path, 'rb') as fd: d = pickle.load(fd) rnn_w = tensor.from_numpy(d['rnn_w']) idx_to_char = d['idx_to_char'] char_to_idx = d['char_to_idx'] vocab_size = len(idx_to_char) dense_w = tensor.from_numpy(d['dense_w']) dense_b = tensor.from_numpy(d['dense_b']) hidden_size = d['hidden_size'] num_stacks = d['num_stacks'] dropout = d['dropout'] cuda = device.create_cuda_gpu() rnn = layer.LSTM(name='lstm', hidden_size=hidden_size, num_stacks=num_stacks, dropout=dropout, input_sample_shape=(len(idx_to_char), )) rnn.to_device(cuda) rnn.param_values()[0].copy_data(rnn_w) dense = layer.Dense('dense', vocab_size, input_sample_shape=(hidden_size, )) dense.to_device(cuda) dense.param_values()[0].copy_data(dense_w) dense.param_values()[1].copy_data(dense_b) hx = tensor.Tensor((num_stacks, 1, hidden_size), cuda) cx = tensor.Tensor((num_stacks, 1, hidden_size), cuda) hx.set_value(0.0) cx.set_value(0.0) if len(seed_text) > 0: for c in seed_text: x = np.zeros((1, vocab_size), dtype=np.float32) x[0, char_to_idx[c]] = 1 tx = tensor.from_numpy(x) tx.to_device(cuda) inputs = [tx, hx, cx] outputs = rnn.forward(False, inputs) y = dense.forward(False, outputs[0]) y = tensor.softmax(y) hx = outputs[1] cx = outputs[2] sys.stdout.write(seed_text) else: y = tensor.Tensor((1, vocab_size), cuda) y.set_value(1.0 / vocab_size) for i in range(nsamples): y.to_host() prob = tensor.to_numpy(y)[0] if do_sample: cur = np.random.choice(vocab_size, 1, p=prob)[0] else: cur = np.argmax(prob) sys.stdout.write(idx_to_char[cur]) x = np.zeros((1, vocab_size), dtype=np.float32) x[0, cur] = 1 tx = tensor.from_numpy(x) tx.to_device(cuda) inputs = [tx, hx, cx] outputs = rnn.forward(False, inputs) y = dense.forward(False, outputs[0]) y = tensor.softmax(y) hx = outputs[1] cx = outputs[2] print('')