栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2021-09-23

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-09-23

test
import torchfrom torch import nnfrom skorch import NeuralNetClassifier#1. build class for the skorch APIclass Torch_Model(nn.Module):def init(self,):super(Torch_Model, self).init()self.convs = nn.Sequential(nn.Conv2d(1,32,3),nn.ReLU(),nn.Conv2d(32,64,3),nn.ReLU(),nn.MaxPool2d(2),nn.Dropout(0.25))self.fcs = nn.Sequential(nn.Linear(121264,128),nn.ReLU(),nn.Dropout(0.5),nn.Linear(128,10),)def forward(self, x): out = x out = self.convs(out) out = out.view(-1,121264) out = self.fcs(out) return out123456123456#2. create the classifierdevice = “cuda” if torch.cuda.is_available() else “cpu”classifier = NeuralNetClassifier(Torch_Model,criterion=nn.CrossEntropyLoss,optimizer=torch.optim.Adam,train_split=None,verbose=1,device=device)#3. load dataimport numpy as npfrom torch.utils.data import DataLoaderfrom torchvision.transforms import ToTensorfrom torchvision.datasets import MNISTmnist_data = MNIST(’.’, download=True, transform=ToTensor())dataloader = DataLoader(mnist_data, shuffle=True, batch_size=60000)X, y = next(iter(dataloader))read training dataX_train, X_test, y_train, y_test = X[:50000], X[50000:], y[:50000], y[50000:]X_train = X_train.reshape(50000, 1, 28, 28)X_test = X_test.reshape(10000, 1, 28, 28)assemble initial datan_initial = 1000initial_idx = np.random.choice(range(len(X_train)), size=n_initial, replace=False)X_initial = X_train[initial_idx]y_initial = y_train[initial_idx]generate the poolremove the initial data from the training datasetX_pool = np.delete(X_train, initial_idx, axis=0)[:5000]y_pool = np.delete(y_train, initial_idx, axis=0)[:5000]#4.build activelearnerfrom modAL.models import ActiveLearnerinitialize ActiveLearnerlearner = ActiveLearner(estimator=classifier,X_training=X_initial, y_training=y_initial,)#5. query datathe active learning loopn_queries = 10for idx in range(n_queries):print(‘Query no. %d’ % (idx + 1))query_idx, query_instance = learner.query(X_pool, n_instances=100)#use new data to train modellearner.teach(X=X_pool[query_idx], y=y_pool[query_idx], only_new=True,)# remove queried instance from poolX_pool = np.delete(X_pool, query_idx, axis=0)y_pool = np.delete(y_pool, query_idx, axis=0)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269305.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号