Ternary algebras with $Z_2$ and $Z_3$ grading and generalized representations of the Lorentz group