Example #1
0
    def train(self, x, y, epoch):
        n_train = y.shape[0]
        data_tensor_list = []
        for i in range(self.size):
            data_tensor_list.append([])
            for j in range(self.size):
                data_tensor_list[i].append([])
                for c in range(self.channel):
                    data_tensor_list[i][j].append(tn.Tensor(x[:,i,j,c,:],labels=["S","U"]))
        self.contracted[0] = data_tensor_list

        label_tensor = tn.Tensor(y,labels=["S","U"])

        for n in range(epoch):
            for l in range(self.num_layers):
                if self.layer_type[l] == -1:
                    for i, j in product(range(self.layer_size[l]), range(self.layer_size[l])):
                        self.update_single_wo_channel_tensor(label_tensor, n_train, l, i, j)
                        logger.debug(f"Done one wo tensor ({l},{i},{j}).")
                elif self.layer_type[l] == 0:
                    for i, j in product(range(self.layer_size[l]), range(self.layer_size[l])):
                        self.update_single_comb_channel_tensor(label_tensor, n_train, l, i, j)
                        logger.debug(f"Done one comb tensor ({l},{i},{j}).")
                else:
                    for i, j, c in product(range(self.layer_size[l]), range(self.layer_size[l]), range(self.channel)):
                        self.update_single_w_channel_tensor(label_tensor, n_train, l, i, j, c)
                        logger.debug(f"Done one w tensor ({l},{i},{j},{c}).")
            logger.debug(f'Epoch {n} completed.')
Example #2
0
    def predict(self, x):
        n_sample = x.shape[0]
        data_tensor_list = []
        for i in range(self.size):
            data_tensor_list.append([])
            for j in range(self.size):
                data_tensor_list[i].append([])
                for c in range(self.channel):
                    data_tensor_list[i][j].append(tn.Tensor(x[:,i,j,c,:],labels=["S","U"]))
        self.contracted[0] = data_tensor_list

        for l in range(self.num_layers):
            if self.layer_type[l] == 1:
                for i, j, c in product(range(self.layer_size[l]), range(self.layer_size[l]), range(self.channel)):
                    self.contracted[l+1][i][j][c] = contract_func0(self.ttn_tensor_list[l][i][j][c],
                                                                   self.contracted[l][2*i][2*j][c],
                                                                   self.contracted[l][2*i][2*j+1][c],
                                                                   self.contracted[l][2*i+1][2*j][c],
                                                                   self.contracted[l][2*i+1][2*j+1][c], n_sample)
            elif self.layer_type[l] == 0:
                for i, j in product(range(self.layer_size[l]), range(self.layer_size[l])):
                    self.contracted[l+1][i][j] = contract_func3(self.ttn_tensor_list[l][i][j], self.contracted[l][i][j], n_sample)
            else:
                for i, j in product(range(self.layer_size[l]), range(self.layer_size[l])):
                    self.contracted[l+1][i][j] = contract_func0(self.ttn_tensor_list[l][i][j], 
                                                                self.contracted[l][2*i][2*j],
                                                                self.contracted[l][2*i][2*j+1],
                                                                self.contracted[l][2*i+1][2*j],
                                                                self.contracted[l][2*i+1][2*j+1], n_sample)
        return self.contracted[self.num_layers][0][0].data
Example #3
0
def preprocess_test(images, labels, n_test, n_test_each, bond_data):
    import numpy as np
    import tncontract as tn
    from itertools import product
    from itertools import combinations

    image_group = get_image_group(images, labels)

    data_group = np.zeros(
        (16, 16, 10, image_group.shape[3], bond_data), dtype=np.float64)

    for i in range(bond_data):
        data_group[:, :, :, :, i] = (len([c for c in combinations(range(bond_data - 1), i)]) ** 0.5) * \
            np.cos((image_group) * (np.pi / 2)) ** (bond_data - (i + 1)) * np.sin(
            (image_group) * (np.pi / 2)) ** i

    test_data = np.zeros((16, 16, bond_data, n_test), dtype=np.float64)

    for k, l, m in product(range(bond_data), range(10), range(n_test_each)):
        test_data[:, :, k, l * n_test_each + m] = data_group[:, :, l, m, k]

    test_tensor = [[0 for col in range(16)] for row in range(16)]
    for i, j in product(range(16), range(16)):
        test_tensor[i][j] = tn.Tensor(
            test_data[i, j, :, :], labels=["up", "down"])

    return test_tensor
Example #4
0
def preprocess(images, labels, n_train, n_train_each, bond_label, bond_data, current_class):
    import numpy as np
    import tncontract as tn
    from itertools import product
    from itertools import combinations

    image_group = get_image_group(images, labels)

    data_group = np.zeros(
        (16, 16, 10, image_group.shape[3], bond_data), dtype=np.float64)

    for i in range(bond_data):
        data_group[:, :, :, :, i] = (len([c for c in combinations(range(bond_data - 1), i)]) ** 0.5) * \
            np.cos((image_group) * (np.pi / 2)) ** (bond_data - (i + 1)) * np.sin(
            (image_group) * (np.pi / 2)) ** i

    train_data = np.zeros((16, 16, bond_data, n_train), dtype=np.float64)
    label_data = np.zeros((n_train, bond_label), dtype=np.float64)

    for k, m in product(range(bond_data), range(n_train_each)):
        train_data[:, :, k, m] = data_group[:, :, current_class, m, k]

    cc = set([current_class])
    rest = set(range(0, 10)) - cc

    for k, l, m in product(range(bond_data), range(9), range(int(n_train_each / 9))):
        train_data[:, :, k, n_train_each + l *
                   int(n_train_each / 9) + m] = data_group[:, :, list(rest)[l], m, k]

    label_data[0:n_train_each] = [1, 0]
    label_data[n_train_each:n_train] = [0, 1]

    data_tensor = [[0 for col in range(16)] for row in range(16)]
    for i, j in product(range(16), range(16)):
        data_tensor[i][j] = tn.Tensor(
            train_data[i, j, :, :], labels=["up", "down"])

    label_tensor = tn.Tensor(label_data, labels=["up", "down"])
    return data_tensor, label_tensor
Example #5
0
def load_train_data(filename, n_train, n_train_each, bond_label, bond_data,
                    current_class):
    image = sio.loadmat(filename)
    image_group = image['Data_group']
    data_group = np.zeros((16, 16, 10, image_group.shape[3], bond_data),
                          dtype=np.float64)
    # 批量对像素进行特征映射
    for i in range(bond_data):
        data_group[:, :, :, :, i] = (len([c for c in combinations(range(bond_data - 1), i)]) ** 0.5) * \
            np.cos((image_group) * (np.pi / 2)) ** (bond_data - (i + 1)) * np.sin(
            (image_group) * (np.pi / 2)) ** i                   # dg[::::0]=cos((pi/2)x) dg[::::1]=sin((pi/2)x)

    train_data = np.zeros((16, 16, bond_data, n_train), dtype=np.float64)
    label_data = np.zeros((n_train, bond_label), dtype=np.float64)

    for k, m in product(range(bond_data), range(n_train_each)):
        train_data[:, :, k, m] = data_group[:, :, current_class, m, k]

    cc = set([current_class])  # 待去除的类别
    rest = set(range(0, 10)) - cc  # 剩下的类别

    for k, l, m in product(range(bond_data), range(9),
                           range(int(n_train_each / 9))):
        train_data[:, :, k, n_train_each + l * int(n_train_each / 9) +
                   m] = data_group[:, :, list(rest)[l], m, k]

    label_data[0:n_train_each] = [1, 0]
    label_data[n_train_each:n_train] = [0, 1]

    data_tensor = [[0 for col in range(16)] for row in range(16)]
    for i, j in product(range(16), range(16)):
        data_tensor[i][j] = tn.Tensor(train_data[i, j, :, :],
                                      labels=["up", "down"])

    label_tensor = tn.Tensor(label_data,
                             labels=["up",
                                     "down"])  # 自旋向上up[1,0],自旋向下down[0,1]
    return data_tensor, label_tensor
    def __init__(self, data, labels, bond_data, bond_inner, bond_label,
                 layer_units):
        self.flag_contract = np.zeros((5, 8, 8), dtype=np.float64)
        # initialize contraction results
        self.layer_units = layer_units

        # self.contracted = [[]] * len(self.layer_units)

        self.contracted = []

        self.contracted.append(data)
        for i in range(1, 5):
            self.contracted.append([[0 for _ in range(self.layer_units[i])]
                                    for _ in range(self.layer_units[i])])
        self.tn_layers = [0]

        self.bond_inner = bond_inner
        self.bond_label = bond_label
        self.bond_data = bond_data
        self.labels = labels
        self.n_train = data[0][0].shape[1]

        for i in range(1, 5):
            self.tn_layers.append([])
            for j in range(self.layer_units[i]):
                self.tn_layers[i].append([])
                for k in range(self.layer_units[i]):
                    if i == 1:
                        temp = np.random.random(
                            (self.bond_data, self.bond_data, self.bond_data,
                             self.bond_data, self.bond_inner))
                    elif i == 4:
                        temp = np.random.random(
                            (self.bond_inner, self.bond_inner, self.bond_inner,
                             self.bond_inner, self.bond_label))
                    else:
                        temp = np.random.random(
                            (self.bond_inner, self.bond_inner, self.bond_inner,
                             self.bond_inner, self.bond_inner))
                    self.tn_layers[i][j].append(
                        tn.Tensor(temp, labels=["1", "2", "3", "4", "up"]))
    def __init__(self, data, labels, bond_data, bond_inner, bond_label, layer_units):
        self.flag_contract = np.zeros((5, 8, 8), dtype=np.float64) # 从i=1层开始更新
        # initialize contraction results
        self.layer_units = layer_units

        # self.contracted = [[]] * len(self.layer_units)

        self.contracted = []

        self.contracted.append(data)
        # 给出input层数据data,其余层初始化为0
        for i in range(1, 5):
            self.contracted.append(
                [[0 for _ in range(self.layer_units[i])] for _ in range(self.layer_units[i])])
        self.tn_layers = [0]

        self.bond_inner = bond_inner
        self.bond_label = bond_label
        self.bond_data = bond_data
        self.labels = labels
        self.n_train = data[0][0].shape[1]
        # 初始化张量网络,i==1表示输入层,i==4表示输出层,i==其他表示中间层
        # 我终于想明白啦!!!原来它每一层是length*width的格式,输入层是16*16的图片,第一层是8*8的图片以此类推,直到1*1
        for i in range(1, 5):
            self.tn_layers.append([])
            for j in range(self.layer_units[i]):
                self.tn_layers[i].append([])
                for k in range(self.layer_units[i]):
                    if i == 1:
                        temp = np.random.random(
                            (self.bond_data, self.bond_data, self.bond_data, self.bond_data, self.bond_inner))
                    elif i == 4:    
                        temp = np.random.random(
                            (self.bond_inner, self.bond_inner, self.bond_inner, self.bond_inner, self.bond_label))
                    else:
                        temp = np.random.random(
                            (self.bond_inner, self.bond_inner, self.bond_inner, self.bond_inner, self.bond_inner))
                    # labels表示物理指标,后面不再赘述
                    self.tn_layers[i][j].append(
                        tn.Tensor(temp, labels=["1", "2", "3", "4", "up"]))
Example #8
0
def load_test_data(filename, n_test, n_test_each, bond_data):
    image = sio.loadmat(filename)
    image_group = image['Data_test_group']
    data_group = np.zeros((16, 16, 10, image_group.shape[3], bond_data),
                          dtype=np.float64)

    for i in range(bond_data):
        data_group[:, :, :, :, i] = (len([c for c in combinations(range(bond_data - 1), i)]) ** 0.5) * \
            np.cos((image_group) * (np.pi / 2)) ** (bond_data - (i + 1)) * np.sin(
            (image_group) * (np.pi / 2)) ** i

    test_data = np.zeros((16, 16, bond_data, n_test), dtype=np.float64)

    for k, l, m in product(range(bond_data), range(10), range(n_test_each)):
        test_data[:, :, k, l * n_test_each + m] = data_group[:, :, l, m, k]

    test_tensor = [[0 for col in range(16)] for row in range(16)]
    for i, j in product(range(16), range(16)):
        test_tensor[i][j] = tn.Tensor(test_data[i, j, :, :],
                                      labels=["up", "down"])

    return test_tensor
Example #9
0
    def __init__(self, image_size, channel, bond_data, bond_inner, bond_label, layer_channel):
        self.size = image_size
        self.channel = channel
        self.bond_data = bond_data
        self.bond_inner = bond_inner
        self.bond_label = bond_label
        self.layer_comb_channel = layer_channel

        self.layer_size = []
        lsize = self.size//2
        while lsize > 0:
            self.layer_size.append(lsize)
            lsize //= 2
        self.num_layers = len(self.layer_size)
        assert(self.layer_comb_channel <= self.num_layers)
        if self.num_layers == self.layer_comb_channel:
            self.layer_size.insert(self.num_layers, 1)
        else:
            self.layer_size.insert(self.layer_comb_channel,self.layer_size[self.layer_comb_channel]*2)
        self.num_layers += 1
        self.layer_type = [1]*self.layer_comb_channel # 1: With Ch, 0: Comb Ch, -1:W.O. Ch
        self.layer_type.append(0)
        self.layer_type += [-1]*(self.num_layers-self.layer_comb_channel-1)

        self.flag_contract = []
        self.contracted = [0]
        self.ttn_tensor_list = []
        for l in range(self.num_layers):
            self.ttn_tensor_list.append([])
            self.flag_contract.append([])
            self.contracted.append([])
            if l == 0:
                if self.layer_type[l] == 1:
                    size_info = (self.bond_data,)*4 + (self.bond_inner,)
                    tensor_label = ["D00","D01","D10","D11","U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            self.ttn_tensor_list[l][i].append([])
                            self.flag_contract[l][i].append([])
                            self.contracted[l+1][i].append([])
                            for c in range(self.channel):
                                rnd_tensor = np.random.random(size_info)
                                self.ttn_tensor_list[l][i][j].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                                self.flag_contract[l][i][j].append(False)
                                self.contracted[l+1][i][j].append(0)
                elif self.layer_type[l] == 0:
                    size_info = (self.bond_data,)*self.channel+(self.bond_inner,)
                    tensor_label = ["C"+str(i) for i in range(self.channel)]+["U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            rnd_tensor = np.random.random(size_info)
                            self.ttn_tensor_list[l][i].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                            self.flag_contract[l][i].append(False)
                            self.contracted[l+1][i].append(0)
                else:
                    raise RuntimeError
            elif l == self.num_layers-1:
                if self.layer_type[l] == -1:
                    size_info = (self.bond_inner,)*4+(self.bond_label,)
                    tensor_label = ["D00","D01","D10","D11","U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            rnd_tensor = np.random.random(size_info)
                            self.ttn_tensor_list[l][i].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                            self.flag_contract[l][i].append(False)
                            self.contracted[l+1][i].append(0)
                elif self.layer_type[l] == 0:
                    size_info = (self.bond_inner,)*self.channel+(self.bond_label,)
                    tensor_label = ["C"+str(i) for i in range(self.channel)]+["U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            rnd_tensor = np.random.random(size_info)
                            self.ttn_tensor_list[l][i].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                            self.flag_contract[l][i].append(False)
                            self.contracted[l+1][i].append(0)
                else:
                    raise RuntimeError
            else:
                if self.layer_type[l] == 1:
                    size_info = (self.bond_inner,)*4 + (self.bond_inner,)
                    tensor_label = ["D00","D01","D10","D11","U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            self.ttn_tensor_list[l][i].append([])
                            self.flag_contract[l][i].append([])
                            self.contracted[l+1][i].append([])
                            for c in range(self.channel):
                                rnd_tensor = np.random.random(size_info)
                                self.ttn_tensor_list[l][i][j].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                                self.flag_contract[l][i][j].append(False)
                                self.contracted[l+1][i][j].append(0)
                elif self.layer_type[l] == -1:
                    size_info = (self.bond_inner,)*4+(self.bond_inner,)
                    tensor_label = ["D00","D01","D10","D11","U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            rnd_tensor = np.random.random(size_info)
                            self.ttn_tensor_list[l][i].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                            self.flag_contract[l][i].append(False)
                            self.contracted[l+1][i].append(0)
                else:
                    size_info = (self.bond_inner,)*self.channel+(self.bond_inner,)
                    tensor_label = ["C"+str(i) for i in range(self.channel)]+["U"]
                    for i in range(self.layer_size[l]):
                        self.ttn_tensor_list[l].append([])
                        self.flag_contract[l].append([])
                        self.contracted[l+1].append([])
                        for j in range(self.layer_size[l]):
                            rnd_tensor = np.random.random(size_info)
                            self.ttn_tensor_list[l][i].append(tn.Tensor(rnd_tensor, labels=tensor_label))
                            self.flag_contract[l][i].append(False)
                            self.contracted[l+1][i].append(0)
        logger.debug("TTN initialization completed.")