mxnet的基本用法

本文章全为一个note, 代码来自zh.d2.ai. 目的在于自己看到这代码能想起来mxnet的基本用法.

import d2lzh as dd2l
from mxnet import autograd, gluon, init, nd
from mxnet.gluon import data as gdata, loss as gloss, nn
import numpy as np
import pandas as pd


train_data = pd.read_csv('data/kaggle_house_pred_train.csv')
test_data = pd.read_csv('data/kaggle_house_pred_test.csv')

# 获得数字特征
all_features = pd.concat((train_data.iloc[:, 1:-1], test_data.iloc[:,1:]))
numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index


# 数字特征进行规则化
all_features[numeric_features] = all_features[numeric_features].apply(lambda x : (x - x.mean()) / x.std())
all_features[numeric_features] = all_features[numeric_features].fillna(0)
# 非数字特征进行数字化 get_dummies() 
all_features = pd.get_dummies(all_features, dummy_na=True)



m_train = train_data.shape[0]
train_features = nd.array(all_features.iloc[0:m_train, :])
test_features = nd.array(all_features.iloc[m_train:, :])
train_labels = nd.array(train_data.iloc[:, -1]).reshape((-1, 1))


loss = gloss.L2Loss()

def get_net():
    net = nn.Sequential()
    net.add(nn.Dense(1))
    net.initialize()
    return net

def log_error(net, features, labels):
    # 将小于1的log值设成1
    preds = nd.clip(net(features), 1, float('inf'))
    error = nd.sqrt(2 * loss(preds.log(), labels.log()).mean())
    return error.asscalar()


def train(net, train_features, train_labels, num_epochs, learning_rate, weight_decay, batch_size):
    train_loss = []
    train_iter = gdata.DataLoader(gdata.ArrayDataset(train_features, train_labels), batch_size=batch_size, shuffle=True)
    #ADM优化算法 - 梯度下降改进
    trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate' : learning_rate, 'wd' : weight_decay})
    for epoch in range(num_epochs):
        for data, label in train_iter:
            # 自动求梯度
            with autograd.record():
                l = loss(net(data), label)
            l.backward()
            # 更新网络权重
            trainer.update(batch_size) #or trainer.step(batch_size)
        train_loss.append(log_error(net, train_features, train_labels))
    return train_loss



def train_and_pred(train_features, train_labels, test_features, test_labels, num_epochs, learning_rate, weight_decay, batch_size):
    net = get_net()
    train_loss = train(net, train_features, train_labels, num_epochs, learning_rate, weight_decay, batch_size)
    # d2lzh包画图
    dd2l.semilogy(range(1, num_epochs + 1), train_loss, 'num_epoch', 'loss')
    # 测试数据预测
    preds = net(test_features).asnumpy()
    test_data['SalePrice'] = preds.reshape((-1, 1))
    submission = pd.concat([test_data['Id'], test_data['SalePrice']], axis=1)
    # 写入预测结果
    submission.to_csv('submission.csv', index=False)



# 超参数设置
num_epochs, learning_rate, weight_decay, batch_size = 100, 10, 0, 64


train_and_pred(train_features, train_labels, test_features, None, num_epochs, learning_rate, weight_decay, batch_size)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值