import tensorflow as tf


  # In case the shape is immutable.
  shape = list(shape)
  # In case shape represents a vector, e.g. [None, [2, 2, 2]]
  if shape[0] is None:
    shape[0] = np.ones(len(shape[1]), dtype=int)
  # In case shape represents a vector, e.g. [[2, 2, 2], None]
  if shape[1] is None:
    shape[1] = np.ones(len(shape[0]), dtype=int)
  shape = np.array(shape)
  tt_rank = np.array(tt_rank)
  _validate_input_parameters(is_tensor=False, shape=shape, tt_rank=tt_rank)
  n_in = np.prod(shape[0])
  lamb = 2.0 / n_in

  with tf.name_scope(name):
    return random_matrix(shape, tt_rank=tt_rank, stddev=np.sqrt(lamb),
                         dtype=dtype)


def lecun_initializer(shape, tt_rank=2, dtype=tf.float32,
                      name='t3f_lecun_initializer'):
  """Constructs a random TT matrix with entrywise variance 1.0 / n_in

  Args:
    shape: 2d array, shape[0] is the shape of the matrix row-index,
      shape[1] is the shape of the column index.
      shape[0] and shape[1] should have the same number of elements (d)
      Also supports omitting one of the dimensions for vectors, e.g.
        lecun_initializer([[2, 2, 2], None])
