TorchFusion 是一个深度学习框架,主要用于 AI 系统加速研究和开发。
TorchFusion 基于 PyTorch 并且完全兼容纯 PyTorch 和其他 PyTorch 软件包,它供了一个全面的可扩展训练框架,可以轻松用开发者的 PyTorch 模型进行训练,评估和运行推理。
该框架具有高度可扩展性,所以开发者可以根据自己特定的目的来进行训练。
Github 链接:
https://github.com/johnolafenwa/TorchFusion
注意:这只是 TorchFusion 的预发布版本,未来的 TorchFusion 将会跨越更多的深度学习领域。
安装 TorchFusion
pip3 install https://github.com/johnolafenwa/TorchFusion/releases/download/0.1.1/torchfusion-0.1.1-py3-none-any.whl
在 Windows 上安装 PyTorch:
https://pytorch.org/
CPU Only
With Python 3.6
pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl torchvision
With Python 3.5
pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp35-cp35m-win_amd64.whl torchvision
CUDA 支持
With Python 3.6
pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp36-cp36m-win_amd64.whl torchvision
With Python 3.5
pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.0-cp35-cp35m-win_amd64.whl
在 Linux 上安装 PyTorch:
https://pytorch.org/
CPU Only
With Python 3.6
pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-linux_x86_64.whl torchvision
With Python 3.5
pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp35-cp35m-linux_x86_64.whl torchvision
CUDA 支持
pip3 install torch torchvision
在 OSX 上安装 PyTorch:
https://pytorch.org/
CPU Only
pip3 install torch torchvision
MNIST in Five Minutes
import torchfusion as tf
from torchvision.datasets.mnist import MNIST
from torch.utils.data import DataLoader
from torchvision.transforms import transforms
import torch.nn as nn
from torch.optim import Adam
import torch.cuda as cuda
#Define a the classifier network
net = nn.Sequential(
tf.Flatten(),
nn.Linear(784, 100),
nn.ReLU(),
nn.Linear(100, 100),
nn.ReLU(),
nn.Linear(100, 100),
nn.ReLU(),
nn.Linear(100, 10)
)
batch_size = 64
#Transformations and data augmentation
transformations = transforms.Compose([
transforms.Resize(28),
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])
#Load the training and test sets
train_set = MNIST(root="./data",transform=transformations,download=True)
test_set = MNIST(root="./data",train=False,transform=transformations,download=True)
train_loader = DataLoader(train_set,shuffle=True,batch_size=batch_size,num_workers=4)
test_loader = DataLoader(test_set,shuffle=False,batch_size=batch_size,num_workers=4)
#Move to GPU if available
if cuda.is_available():
net.cuda()
#Setup the optimize and a loss function
optimizer = Adam(net.parameters(),lr=0.001)
loss_fn = nn.CrossEntropyLoss()
#Top 1 Train accuracy
train_metrics = tf.Accuracy(topK=1)
#Top 1 and Top 2 test accuracy
test_metrics_top1 = tf.Accuracy(name="Top 1 Acc ",topK=1)
test_metrics_top2 = tf.Accuracy(name="Top 2 Acc ",topK=2)
#Create an instance of the StandardModel
model = tf.StandardModel(net)
def train():
#print a summary of the network
print(model.summary((1,28,28)))
model.train(train_loader, loss_fn, optimizer, [train_metrics], test_loader,
[test_metrics_top1, test_metrics_top2], num_epochs=20,
model_dir="mnist_mlp_saved_models",save_logs="logs.txt")
if __name__ == "__main__":
train()
GAN in Five Minutes
import torchfusion.gan as tfgan
from torchvision.datasets import MNIST
from torchvision.transforms import transforms
from torch.optim import Adam
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.cuda as cuda
#Transformations and data augmentation
train_transformations = transforms.Compose([
transforms.Resize(28),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
batch_size = 64
# Load the training set
train_set = MNIST(root="./data", train=True, transform=train_transformations, download=True)
train_data = DataLoader(train_set,batch_size=batch_size,shuffle=True,num_workers=4)
#Create an instance of the NormalDistribution
source = tfgan.NormalDistribution(length=len(train_set),size=(100))
source_data = DataLoader(source,batch_size=batch_size,shuffle=True,num_workers=4)
#Create an instance of the Generator and Discriminator
G = tfgan.MLPGenerator(latent_size=100,output_size=(1,28,28))
D = tfgan.MLPDiscriminator(input_size=(1,28,28))
#Move the networks to GPU if available
if cuda.is_available():
G.cuda()
D.cuda()
#Setup the optimizers
g_optim = Adam(G.parameters(),lr=0.0002,betas=(0.5,0.999))
d_optim = Adam(D.parameters(),lr=0.0002,betas=(0.5,0.999))
#Define the loss function
loss_fn = nn.BCELoss()
if __name__ == "__main__":
#Create an instance of the StandardGANModel
trainer = tfgan.StandardGANModel(G,D,gen_loss_fn=loss_fn,disc_loss_fn=loss_fn)
#Train the two models
trainer.train(train_data,source_data,g_optim,d_optim,num_epochs=200,disc_steps=1,save_interval=3000)