def equal_compute(attrs, inputs, output_type, target): assert len(inputs) == 2 return [topi.equal(inputs[0], inputs[1])]
new_h = h for i in range(num_timesteps): inp = topi.concatenate([xs[i], new_h], 1) g = topi.tanh(topi.matmul(inp, weights[0]) + weights[1]) j = topi.sigmoid(topi.matmul(inp, weights[2]) + weights[3]) f = topi.sigmoid(topi.matmul(inp, weights[4]) + weights[5]) o = topi.sigmoid(topi.matmul(inp, weights[6]) + weights[7]) new_s = new_s * f + g * j new_h = topi.tanh(new_s) * o logits = topi.matmul(new_h, weights[8]) + weights[9] # compute accuracy pred = topi.nn.softmax(logits) correct_pred = topi.equal(topi.argmax(y, 1), topi.argmax(pred, 1)) accuracy = topi.sum(correct_pred.astype('float32')) / batch_size # Define loss and optimizer loss = topi.sum(-topi.sum(y * topi.nn.log_softmax(logits), axis=1)) / batch_size head = topi.full((1, ), 'float32', 1.0) gradients = list(tvm.differentiate(topi.reshape(loss, (1, )), weights, head)) new_weights = [w - lr * g for (w, g) in zip(weights, gradients)] # Define model sched = tvm.create_schedule([loss.op, accuracy.op] + [x.op for x in new_weights]) parallel_schedule(sched) train_model = tvm.build(sched,
def equal(x, y): return topi.equal(x, y).astype("int8")