def main(): # 网络参数全局配置 global W1, b1, W2, b2, num_inputs # config parameters num_inputs = 784 num_outputs = 10 num_hiddens = 256 batch_size = 64 num_epochs = 10 lr = 0.5 # 获取和读取数据 train_iter, test_iter = load_data_fashion_mnist(batch_size=batch_size) # 初始化神经网络模型参数 params = init_weight(num_inputs, num_hiddens, num_outputs) [W1, b1, W2, b2] = params # 定义损失函数和优化器 loss = torch.nn.CrossEntropyLoss() # 神经网络训练 train(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr) # 测试集预测 predict(net, test_iter)
def main(): # config parameters num_inputs = 784 num_outputs = 10 num_hiddens = 256 batch_size = 64 num_epochs = 30 # Step 1:获取和读取数据 train_iter, test_iter = load_data_fashion_mnist(batch_size=batch_size) # X, y = iter(train_iter).next() # print("X shape:",X.shape) # print("y shape:",y.shape) # Step 2:网络模型结构定义,几种不同的写法 from collections import OrderedDict # method1 net = nn.Sequential( # FlattenLayer(), # nn.Linear(num_inputs, num_outputs) OrderedDict([('flatten', FlattenLayer()), ('linear1', nn.Linear(num_inputs, num_hiddens)), ('relu', nn.ReLU()), ('linear2', nn.Linear(num_hiddens, num_outputs))])) # # method2 # net = nn.Sequential( # FlattenLayer(), # nn.Linear(num_inputs,num_hiddens), # nn.ReLU(), # nn.Linear(num_hiddens,num_outputs) # ) print(net) # Step 3:权重初始化 # print("net.parameters:",list(net.parameters())) # shape: [784,256],[256],[256,10],[10] for param in net.parameters(): init.normal_(param, mean=0, std=0.01) # init.normal_(net.linear.weight, mean=0, std=0.01) # init.constant_(net.linear.bias, val=0) # Step 4:定义损失函数 loss = torch.nn.CrossEntropyLoss() # Step 5:定义优化算法 # optimizer = torch.optim.SGD(net.parameters(), lr=0.5) # optimizer = torch.optim.Adagrad(net.parameters(),lr=0.5) optimizer = torch.optim.Adadelta(net.parameters(), lr=0.5) # optimizer = torch.optim.Adam(net.parameters(),lr=0.5) # Step 6:神经网络训练 train(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer) # Step 7:测试集预测 predict(net, test_iter)
def main(): """begin 神经网络参数配置""" global num_inputs, num_hiddens1, num_hiddens2, num_outputs, \ drop_prob1, drop_prob2, num_epochs, lr, batch_size global net, W1, b1, W2, b2, W3, b3 # 神经网络参数配置及权重初始化 num_inputs, num_outputs, num_hiddens1, num_hiddens2 = 784, 10, 256, 256 W1 = torch.tensor(np.random.normal(0, 0.01, size=(num_inputs, num_hiddens1)), dtype=torch.float, requires_grad=True) b1 = torch.zeros(num_hiddens1, requires_grad=True) W2 = torch.tensor(np.random.normal(0, 0.01, size=(num_hiddens1, num_hiddens2)), dtype=torch.float, requires_grad=True) b2 = torch.zeros(num_hiddens2, requires_grad=True) W3 = torch.tensor(np.random.normal(0, 0.01, size=(num_hiddens2, num_outputs)), dtype=torch.float, requires_grad=True) b3 = torch.zeros(num_outputs, requires_grad=True) # 待优化参数 params = [W1, b1, W2, b2, W3, b3] # 给不同隐藏层输出,设置不同的dropout参数值 drop_prob1, drop_prob2 = 0.2, 0.5 # 神经网络超参数 num_epochs, lr, batch_size = 50, 100.0, 256 train_iter, test_iter = load_data_fashion_mnist(batch_size) # 损失函数 loss = torch.nn.CrossEntropyLoss() # 网络模型 net = net """end 神经网络参数配置""" # 模型训练 train(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr) # 模型预测 predict(net, test_iter)
def main(): """begin 神经网络参数配置""" global num_inputs, num_hiddens1, num_hiddens2, num_outputs, num_epochs, lr, batch_size # Step 1:神经网络参数配置及权重初始化 num_inputs, num_outputs, num_hiddens1, num_hiddens2 = 784, 10, 256, 256 # Step 2:网络模型结构定义,几种不同的写法 from collections import OrderedDict # method1 net = nn.Sequential( OrderedDict([ ('flatten', FlattenLayer()), ('linear1', nn.Linear(num_inputs, num_hiddens1)), ('relu1', nn.ReLU()), ('dropout1', nn.Dropout(0.2)), ('linear2', nn.Linear(num_hiddens1, num_hiddens2)), ('relu2', nn.ReLU()), ('dropout2', nn.Dropout(0.5)), ('output', nn.Linear(num_hiddens2, num_outputs)), ('relu3', nn.ReLU()), ])) print(net, type(net)) # Step 3:权重初始化 # print("net.parameters:", list(net.parameters())) for param in net.parameters(): init.normal_(param, mean=0, std=0.01) # Step 4:神经网络超参数与数据集加载 num_epochs, lr, batch_size = 1000, 0.3, 64 train_iter, test_iter = load_data_fashion_mnist(batch_size) # Step 5:损失函数 loss = torch.nn.CrossEntropyLoss() # Step 6:定义优化算法 # optimizer = torch.optim.SGD(net.parameters(), lr=0.5) # optimizer = torch.optim.Adagrad(net.parameters(),lr=0.5) optimizer = torch.optim.Adadelta(net.parameters(), lr=0.5) # optimizer = torch.optim.Adam(net.parameters(),lr=0.5) # Step 7:模型训练 train(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer) # Step 8:模型预测 predict(net, test_iter)
def main(): # 全局变量配置, Python中的全局变量只能先定义后赋值 global num_inputs, W, b # Step 1:config parameters batch_size = 256 num_inputs = 784 num_outputs = 10 num_epochs = 30 lr = 0.1 # Step 2:加载训练数据和测试数据迭代对象 train_iter, test_iter = load_data_fashion_mnist(batch_size) # # 数据集测试 # from matplotlib import pyplot as plt # for X, y in train_iter: # print('Image:',X.shape,'label:',y.shape) # img = X[0].view(28,28).numpy() # label = get_fashion_mnist_labels([y[0]]) # plt.imshow(img) # plt.title(label) # plt.axis('off') # plt.savefig('../figures/3.6_train_iter_test.jpg') # break # Step 3:网络权重和偏移量初始化 W, b = init_weight(num_inputs, num_outputs) # Step 4:调用训练函数 train(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, params=[W, b], lr=lr) # Step 5:测试集评价模型 predict(net, test_iter)
# 最后返回mask之后的X,注意除此之外数值的范围进行伸缩变换 return mask * X * scale # test dropout A = nd.arange(20).reshape((5, 4)) print(dropout(A, 0.0)) print(dropout(A, 0.5)) print(dropout(A, 1.0)) import sys sys.path.append('..') from utils import utils batch_size = 256 train_data, test_data = utils.load_data_fashion_mnist(batch_size) # 含有两个隐含层的多层感知机 num_inputs = 28 * 28 num_outputs = 10 num_hidden1 = 256 num_hidden2 = 256 weight_scale = 0.1 # scale 为分布的标准差 W1 = nd.random_normal(shape=(num_inputs, num_hidden1), scale=weight_scale) b1 = nd.zeros(num_hidden1) W2 = nd.random_normal(shape=(num_hidden1, num_hidden2), scale=weight_scale) b2 = nd.zeros(num_hidden2)