您好,登錄后才能下訂單哦!
這篇文章給大家介紹mxnet模塊怎么在Python中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
具體如下:
import gluonbook as gb from mxnet import autograd,nd,init,gluon from mxnet.gluon import loss as gloss,data as gdata,nn,utils as gutils import mxnet as mx net = nn.Sequential() with net.name_scope(): net.add( nn.Conv2D(channels=32, kernel_size=5, activation='relu'), nn.MaxPool2D(pool_size=2, strides=2), nn.Flatten(), nn.Dense(128, activation='sigmoid'), nn.Dense(10, activation='sigmoid') ) lr = 0.5 batch_size=256 ctx = mx.gpu() net.initialize(init=init.Xavier(), ctx=ctx) train_data, test_data = gb.load_data_fashion_mnist(batch_size) trainer = gluon.Trainer(net.collect_params(),'sgd',{'learning_rate' : lr}) loss = gloss.SoftmaxCrossEntropyLoss() num_epochs = 30 def train(train_data, test_data, net, loss, trainer,num_epochs): for epoch in range(num_epochs): total_loss = 0 for x,y in train_data: with autograd.record(): x = x.as_in_context(ctx) y = y.as_in_context(ctx) y_hat=net(x) l = loss(y_hat,y) l.backward() total_loss += l trainer.step(batch_size) mx.nd.waitall() print("Epoch [{}]: Loss {}".format(epoch, total_loss.sum().asnumpy()[0]/(batch_size*len(train_data)))) if __name__ == '__main__': try: ctx = mx.gpu() _ = nd.zeros((1,), ctx=ctx) except: ctx = mx.cpu() ctx gb.train(train_data,test_data,net,loss,trainer,ctx,num_epochs)
關于mxnet模塊怎么在Python中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。