Пример #1
0
def main():
    n = np.random.randint(2, 20)

    # Generate an undirected graph
    graph = np.random.randint(2, size=[n, n])
    graph = np.tril(graph, -1) + np.tril(graph, -1).T

    label = np.random.randint(2)
    print(graph)
    print(graph.shape)
    print('label', label)

    dim_feature = 10
    x = np.zeros([n, dim_feature])
    x[:, 0] = 1
    W = np.random.normal(0, 0.4, [dim_feature, dim_feature])
    A = np.random.normal(0, 0.4, dim_feature)
    b = np.array([0.])
    model = GraphNeuralNetwork(W, A, b, 2)
    optimizer = SGD(model, lr=0.001)
    for i in range(500):
        grads_flat = calc_grads(model, graph, x, label, lossfunc=bce_with_logit, eps=1e-4)

        outputs = model(graph, x)
        train_loss = bce_with_logit(outputs, label)
        optimizer.update(grads_flat)
        print('step: %d, train_loss: %.15f' % (i, train_loss))
def __train(weight_init_std):
    #初始化网络结构
    bn_network=multi_net_extend(input_size=784, hidden_size_list=[100, 100, 100, 100, 100], output_size=10, 
                                    weight_init_std=weight_init_std, use_batchnorm=True)
    network=multi_net_extend(input_size=784, hidden_size_list=[100, 100, 100, 100, 100], output_size=10,
                                weight_init_std=weight_init_std)
    
    train_acc_list=[]
    bn_train_acc_list=[]
    
    optimizer=SGD(lr=0.01)
    per_epoch=50
    
    epoch_cnt = 0
    
    for i in range(1000000000):
        ##minibatch
        batch_mask=np.random.choice(train_size,batch_size)
        x_batch=x_train[batch_mask]
        t_batch=t_train[batch_mask]
        
        
        ##梯度下降
        for _network in (bn_network, network):
            grads = _network.gradient(x_batch, t_batch)
            optimizer.update(_network.params, grads)
            
        if i %10 == 0:
            train_acc = network.accuracy(x_train, t_train)
            bn_train_acc = bn_network.accuracy(x_train, t_train)
            train_acc_list.append(train_acc)
            bn_train_acc_list.append(bn_train_acc)
    
            print("epoch:" + str(epoch_cnt) + " | " + str(train_acc) + " - " + str(bn_train_acc))
    
            epoch_cnt += 1
            if epoch_cnt >= max_epochs:
                break
    return train_acc_list, bn_train_acc_list
for key, weight in weight_type.items():
    networks[key] = multi_layer_net(input_size=784,
                                    hidden_layer_list=[100, 100, 100, 100],
                                    output_size=10,
                                    weight_init_std=weight)
    train_loss[key] = []

for i in range(iter_num):
    ##取batch数据
    batch_mask = np.random.choice(train_size, batch_size)
    x_batch = x_train[batch_mask]
    t_batch = t_train[batch_mask]
    #计算梯度
    for key in weight_type.keys():
        grads = networks[key].gradient(x_batch, t_batch)
        optimizer.update(networks[key].params, grads)

        #计算损失
        batch_loss = networks[key].loss(x_batch, t_batch)
        train_loss[key].append(batch_loss)

        if i % 100 == 0:
            print('+++++++++++')
            for key in weight_type.keys():
                loss = networks[key].loss(x_batch, t_batch)
                print(key + ":" + str(loss))

#绘图
makers = {'std=0.01': 'o', 'Xavier': 's', 'He': 'D'}
x = np.arange(iter_num)
for key in weight_type.keys():