def train(self):
     net = self.net()
     optimizer=torch.optim.SGD(net.parameters(),lr=0.5)
     n_epochs,lr,batch_size=5,100,256
     loss = nn.CrossEntropyLoss()
     train_iter,test_iter=utils.load_data_fashion_mnist(batch_size)
     utils.train_ch3(net,train_iter,test_iter,loss,n_epochs,batch_size,None,None,optimizer=optimizer)
Exemple #2
0
 def train(self):
     num_epoch,lr=5,0.1
     batch_size=256
     train_iter,test_iter=utils.load_data_fashion_mnist()
     utils.train_ch3(self.net,train_iter,test_iter,self.cross_entropy,num_epoch,batch_size,[self.W,self.b],lr)
 def train(self):
     optimizer = torch.optim.SGD(params=self.net.parameters(), lr=0.5)
     utils.train_ch3(self.net, self.train_iter, self.test_iter, self.loss(),
                     5, self.batch_size, None, None, optimizer)
 def train(self):
     num_epochs, lr = 5, 100.
     utils.train_ch3(self.net,self.train_iter,self.test_iter,self.loss(), \
                     num_epochs,self.batch_size,self.params,lr)
    dtype=torch.float, requires_grad=True)
b2 = torch.zeros(dim_hiddens2, requires_grad=True)
W3 = torch.tensor(np.random.normal(0, 0.01, size=(dim_hiddens2, dim_outputs)),
    dtype=torch.float, requires_grad=True)
b3 = torch.zeros(dim_outputs, requires_grad=True)

params = [W1, b1, W2, b2, W3, b3]

# 定义模型
drop_prob1, drop_prob2 = 0.2, 0.5

def net(X, is_training=True):
    """
    X: 输入; is_training: bool, 是否为训练集
    """
    X = X.view(-1, dim_inputs)
    H1 = (torch.matmul(X, W1) + b1).relu() # relu()
    if is_training:
        H1 = dropout(H1, drop_prob1) # 第一层dropout
    H2 = (torch.matmul(H1, W2) + b2).relu()
    if is_training:
        H2 = dropout(H2, drop_prob2) # 第二层dropout
    return torch.matmul(H2, W3) + b3

# 训练模型
num_epochs, lr, batch_size = 5, 100.0, 256
loss = torch.nn.CrossEntropyLoss()
train_iter, test_iter = utils.load_data_fashion_mnist(batch_size)
utils.train_ch3(net, train_iter, test_iter, loss, num_epochs,
    batch_size, params, lr)
Exemple #6
0
from torch import nn
from torch.nn import init
import numpy as np
import sys
import utils as d2l

print(torch.__version__)

num_inputs, num_outputs, num_hiddens = 784, 10, 256

net = nn.Sequential(
    d2l.FlattenLayer(),
    nn.Linear(num_inputs, num_hiddens),
    nn.ReLU(),
    nn.Linear(num_hiddens, num_outputs),
)

for params in net.parameters():
    init.normal_(params, mean=0, std=0.01)

batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(
    batch_size, root='input/FashionMNIST2065')
loss = torch.nn.CrossEntropyLoss()

optimizer = torch.optim.SGD(net.parameters(), lr=0.5)

num_epochs = 5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None,
              None, optimizer)
 def train(self):
     n_epochs,lr,batch_size=5,100,256
     loss = nn.CrossEntropyLoss()
     train_iter,test_iter=utils.load_data_fashion_mnist(batch_size)
     utils.train_ch3(self.net,train_iter,test_iter,loss,n_epochs,batch_size,self.params,lr)
def softmax(X):
    X_exp = X.exp()
    partition = X_exp.sum(dim=1, keepdim=True)
    return X_exp / partition  # broadcast


# 单层softmax模型
def net(X):
    # 使用view将原始输入图像展开成向量
    return softmax(torch.mm(X.view((-1, dim_inputs)), W) + b)


# 交叉熵损失函数
def cross_entropy(y_hat, y):
    return -torch.log(y_hat.gather(1, y.view(
        -1, 1)))  # 对y_hat的每个预测值, 取y对应的那个位置对应的值


# 训练
num_epochs, lr = 5, 0.1
utils.train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs,
                batch_size, [W, b], lr)

# 预测
X, y = iter(test_iter).next()

true_labels = utils.get_fashion_mnist_labels(y.numpy())
pred_labels = utils.get_fashion_mnist_labels(net(X).argmax(dim=1).numpy())
titles = [true + '\n' + pred for true, pred in zip(true_labels, pred_labels)]

utils.show_fashion_mnist(X[0:9], titles[0:9])
 def train(self):
     self.init_model()
     utils.train_ch3(self.net,self.train_iter,self.test_iter,self.loss(), self.num_epoch, \
                     self.batch_size,None,None,optimizer=self.optimizer())