Learning algorithms for neural networks with ternary weights