Example #1
0
 def copyTargetQNetwork(self):
     self.session.run(self.copyTargetQNetworkOperation)
     tt_cores = []
     for i in range(self.W_conv2.ndims()):
         tt_cores.append(
             tf.assign(self.W_conv2T._tt_cores[i],
                       self.W_conv2._tt_cores[i]))
     self.W_conv2T = TensorTrain(tt_cores,
                                 self.W_conv2.get_raw_shape(),
                                 self.W_conv2.get_tt_ranks(),
                                 convert_to_tensor=False)
     tt_cores = []
     for i in range(self.W_conv3.ndims()):
         tt_cores.append(
             tf.assign(self.W_conv3T._tt_cores[i],
                       self.W_conv3._tt_cores[i]))
     self.W_conv3T = TensorTrain(tt_cores,
                                 self.W_conv3.get_raw_shape(),
                                 self.W_conv3.get_tt_ranks(),
                                 convert_to_tensor=False)
     tt_cores = []
     for i in range(self.W_fc1.ndims()):
         tt_cores.append(
             tf.assign(self.W_fc1T._tt_cores[i], self.W_fc1._tt_cores[i]))
     self.W_fc1T = TensorTrain(tt_cores,
                               self.W_fc1.get_raw_shape(),
                               self.W_fc1.get_tt_ranks(),
                               convert_to_tensor=False)
Example #2
0
 def ttmul(self, tt_matrix_a, tt_matrix_b):
     result_cores = []
     # TODO: name the operation and the resulting tensor.
     a_shape = np.array([s.as_list() for s in tt_matrix_a.get_raw_shape()])
     a_ranks = np.array(tt_matrix_a.get_tt_ranks().as_list())
     b_shape = np.array([s.as_list() for s in tt_matrix_b.get_raw_shape()])
     b_ranks = np.array(tt_matrix_b.get_tt_ranks().as_list())
     for core_idx in range(ndims):
         a_core = tt_matrix_a._tt_cores[core_idx]
         b_core = tt_matrix_b._tt_cores[core_idx]
         curr_res_core = tf.einsum('aijb,cjkd->acikbd', a_core, b_core)
         res_left_rank = a_ranks[core_idx] * b_ranks[core_idx]
         res_right_rank = a_ranks[core_idx + 1] * b_ranks[core_idx + 1]
         left_mode = a_shape[0][core_idx]
         right_mode = b_shape[1][core_idx]
         core_shape = (res_left_rank, left_mode, right_mode, res_right_rank)
         curr_res_core = tf.reshape(curr_res_core, core_shape)
         result_cores.append(curr_res_core)
     res_shape = (tt_matrix_a.get_raw_shape()[0],
                  tt_matrix_b.get_raw_shape()[1])
     static_a_ranks = tt_matrix_a.get_tt_ranks()
     static_b_ranks = tt_matrix_b.get_tt_ranks()
     out_ranks = [
         a_r * b_r for a_r, b_r in zip(static_a_ranks, static_b_ranks)
     ]
     return TensorTrain(result_cores, res_shape, out_ranks)
Example #3
0
 def transpose(self, tt_matrix):
     transposed_tt_cores = []
     for core_idx in range(tt_matrix.ndims()):
         curr_core = tt_matrix._tt_cores[core_idx]
         transposed_tt_cores.append(tf.transpose(curr_core, (0, 2, 1, 3)))
     tt_matrix_shape = tt_matrix.get_raw_shape()
     transposed_shape = tt_matrix_shape[1], tt_matrix_shape[0]
     return TensorTrain(transposed_tt_cores, transposed_shape,
                        tt_matrix.get_tt_ranks())
def sym_sum_sinus_tensor(d, bounds=[0, 1], discretization=10):
    space = np.linspace(bounds[0], bounds[1], discretization, endpoint=False)
    sinx, cosx = np.sin(space), np.cos(space)

    first_core = np.vstack([sinx, cosx]).T[np.newaxis, ...]
    last_core = np.vstack([cosx, sinx])[..., np.newaxis]
    middle_core = np.hstack([np.vstack([cosx, sinx]),
                             np.vstack([-sinx, cosx])])
    middle_core = np.reshape(middle_core, (2, 2, -1)).transpose((0, 2, 1))

    return TensorTrain.from_cores([first_core] + [middle_core.copy() for _ in xrange(d - 2)] + [last_core])
Example #5
0
 def ttvariable(self,
                name,
                initializer=None,
                collections=None,
                validate_shape=True):
     variable_cores = []
     # Create new variable.
     with tf.variable_scope(name):
         num_dims = initializer.ndims()
         for i in range(num_dims):
             curr_core_var = tf.Variable(initializer._tt_cores[i],
                                         collections=collections,
                                         name='core_%d' % i)
             variable_cores.append(curr_core_var)
     v = TensorTrain(variable_cores,
                     initializer.get_raw_shape(),
                     initializer.get_tt_ranks(),
                     convert_to_tensor=False)
     tf.add_to_collection('TensorTrainVariables', v)
     return v
Example #6
0
 def ttweight_variable(self, shape, rank):
     shape = np.array(shape)
     tt_rank = np.array(rank)
     num_dim = shape[0].size
     if tt_rank.size == 1:
         tt_rank = tt_rank * np.ones(num_dim - 1)
         tt_rank = np.concatenate([[1], tt_rank, [1]])
     tt_rank = tt_rank.astype(int)
     #var=np.prod(tt_rank)
     #cr_exponent=-1/(2*num_dim)
     #var=np.prod(tt_rank**cr_exponent)
     #stddev=np.sqrt(2/(np.prod(shape[0])+np.prod(shape[1])))
     #core_stddev=stddev**(1/num_dim)*var
     tt_cores = [None] * num_dim
     for i in range(num_dim):
         curr_core_shape = (shape[1][i] * tt_rank[i + 1],
                            tt_rank[i] * shape[0][i])
         tt_cores[i] = tf.random_normal(curr_core_shape,
                                        mean=0,
                                        stddev=0.01)
     initial = TensorTrain(tt_cores, shape, tt_rank)
     return self.ttvariable('Weight', initializer=initial)
 def to_tt_matrix(self, h, max_rank):
     raw_shape = h.shape.as_list()[0]
     if raw_shape == 32:
         shape = [[2, 2, 2, 2, 2, 1], [5, 5, 4, 4, 4, 4]]
     else:
         shape = [[1, 1, 1, 1, 1, 1], [5, 5, 4, 4, 4, 4]]
     shape = np.array(shape)
     tens = tf.reshape(h, shape.flatten())
     d = len(shape[0])
     # transpose_idx = 0, d, 1, d+1 ...
     transpose_idx = np.arange(2 * d).reshape(2, d).T.flatten()
     transpose_idx = transpose_idx.astype(int)
     tens = tf.transpose(tens, transpose_idx)
     new_shape = np.prod(shape, axis=0)
     tens = tf.reshape(tens, new_shape)
     max_rank = (max_rank * np.ones(d + 1)).astype(np.int32)
     ranks = [1] * (d + 1)
     tt_cores = []
     for core_idx in range(d - 1):
         curr_mode = new_shape[core_idx]
         rows = ranks[core_idx] * curr_mode
         tens = tf.reshape(tens, [rows, -1])
         columns = tens.get_shape()[1].value
         s, u, v = tf.svd(tens, full_matrices=False)
         if max_rank[core_idx + 1] == 1:
             ranks[core_idx + 1] = 1
         else:
             ranks[core_idx + 1] = min(max_rank[core_idx + 1], rows,
                                       columns)
         u = u[:, 0:ranks[core_idx + 1]]
         s = s[0:ranks[core_idx + 1]]
         v = v[:, 0:ranks[core_idx + 1]]
         core_shape = (ranks[core_idx], shape[0, core_idx],
                       shape[1, core_idx], ranks[core_idx + 1])
         tt_cores.append(tf.reshape(u, core_shape))
         tens = tf.matmul(tf.diag(s), tf.transpose(v))
     core_shape = (ranks[d - 1], shape[0, -1], shape[1, -1], ranks[d])
     tt_cores.append(tf.reshape(tens, core_shape))
     return TensorTrain(tt_cores, shape, ranks)