{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-13T11:05:13.300407Z","iopub.execute_input":"2022-12-13T11:05:13.301235Z","iopub.status.idle":"2022-12-13T11:05:13.306562Z","shell.execute_reply.started":"2022-12-13T11:05:13.301195Z","shell.execute_reply":"2022-12-13T11:05:13.305018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 导入PyTorch包\nimport torch\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:13.309027Z","iopub.execute_input":"2022-12-13T11:05:13.310151Z","iopub.status.idle":"2022-12-13T11:05:13.323563Z","shell.execute_reply.started":"2022-12-13T11:05:13.310115Z","shell.execute_reply":"2022-12-13T11:05:13.322425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:13.325339Z","iopub.execute_input":"2022-12-13T11:05:13.325661Z","iopub.status.idle":"2022-12-13T11:05:13.334065Z","shell.execute_reply.started":"2022-12-13T11:05:13.325632Z","shell.execute_reply":"2022-12-13T11:05:13.333178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 导入库\nimport torch.nn as nn  # 模型搭建\nimport torch.nn.functional as F  # 内部封装了非常多的计算函数\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\ntorch.manual_seed(1)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:13.514489Z","iopub.execute_input":"2022-12-13T11:05:13.515607Z","iopub.status.idle":"2022-12-13T11:05:13.525582Z","shell.execute_reply.started":"2022-12-13T11:05:13.515564Z","shell.execute_reply":"2022-12-13T11:05:13.524334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:13.527530Z","iopub.execute_input":"2022-12-13T11:05:13.527878Z","iopub.status.idle":"2022-12-13T11:05:13.534740Z","shell.execute_reply.started":"2022-12-13T11:05:13.527847Z","shell.execute_reply":"2022-12-13T11:05:13.533843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\nsample_submission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:13.535998Z","iopub.execute_input":"2022-12-13T11:05:13.536315Z","iopub.status.idle":"2022-12-13T11:05:17.046502Z","shell.execute_reply.started":"2022-12-13T11:05:13.536287Z","shell.execute_reply":"2022-12-13T11:05:17.045330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = train['label'].values\ntrain = train.iloc[:, 1:].values\ntest = test.iloc[:, 1:].values\n#test\n#iloc[:, 1:]前闭后开\n# 训练图片60000张\nprint(train.shape)\n# 测试图片5000张\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:17.049368Z","iopub.execute_input":"2022-12-13T11:05:17.049725Z","iopub.status.idle":"2022-12-13T11:05:17.060836Z","shell.execute_reply.started":"2022-12-13T11:05:17.049691Z","shell.execute_reply":"2022-12-13T11:05:17.059691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mu = train.mean()\nstd = train.std()\n\ntrain = (train - mu) / std\ntest = (test - mu) / std\n\nvalid = train[50000:]\ntrain = train[:50000]\n\nvalid_label = train_label[50000:]\ntrain_label = train_label[:50000]","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:17.062403Z","iopub.execute_input":"2022-12-13T11:05:17.062840Z","iopub.status.idle":"2022-12-13T11:05:17.929126Z","shell.execute_reply.started":"2022-12-13T11:05:17.062797Z","shell.execute_reply":"2022-12-13T11:05:17.927879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MNISTDataset(Dataset):\n    \n    def __init__(self, x, y=None):\n        self.x = x\n        self.y = y\n    \n    def __len__(self):\n        return len(self.x)\n    \n    def __getitem__(self, item):\n        xi = torch.tensor(self.x[item]).float()\n        if self.y is None:\n            return xi\n        else:\n            yi = torch.tensor(self.y[item]).long()   #float类型用于连续变量，long类型用于整数变量\n            # 注意，多分类需要用到CrossEntropyLoss\n            return xi, yi","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:17.932259Z","iopub.execute_input":"2022-12-13T11:05:17.933022Z","iopub.status.idle":"2022-12-13T11:05:17.940576Z","shell.execute_reply.started":"2022-12-13T11:05:17.932975Z","shell.execute_reply":"2022-12-13T11:05:17.939409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = MNISTDataset(train, train_label)\nvalid_ds = MNISTDataset(valid, valid_label)\n\ntest_ds = MNISTDataset(test)\n\ntrain_dl = DataLoader(train_ds, shuffle=True, batch_size=16)\nvalid_dl = DataLoader(valid_ds, shuffle=True, batch_size=16)\ntest_dl = DataLoader(test_ds, shuffle=False, batch_size=16)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:17.942105Z","iopub.execute_input":"2022-12-13T11:05:17.943361Z","iopub.status.idle":"2022-12-13T11:05:17.958974Z","shell.execute_reply.started":"2022-12-13T11:05:17.943324Z","shell.execute_reply":"2022-12-13T11:05:17.957981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):\n    \"\"\"绘制图像列表\"\"\"\n    figsize = (num_cols * scale, num_rows * scale)\n    _, axes = plt.subplots(num_rows, num_cols, figsize=figsize)\n    axes = axes.flatten()\n    imgs = imgs.reshape(-1, 28, 28)\n    for i, (ax, img) in enumerate(zip(axes, imgs)):\n        if torch.is_tensor(img):\n            # 图片张量\n            ax.imshow(img.numpy())\n        else:\n            # PIL图片\n            ax.imshow(img)\n        ax.axes.get_xaxis().set_visible(False)\n        ax.axes.get_yaxis().set_visible(False)\n        if titles:\n            ax.set_title(titles[i])\n    return axes\n\n\nfor x, y in train_dl:\n    break\nshow_images(x, 4, 4)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:17.960293Z","iopub.execute_input":"2022-12-13T11:05:17.960765Z","iopub.status.idle":"2022-12-13T11:05:18.793715Z","shell.execute_reply.started":"2022-12-13T11:05:17.960733Z","shell.execute_reply":"2022-12-13T11:05:18.792604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:18.795039Z","iopub.execute_input":"2022-12-13T11:05:18.795488Z","iopub.status.idle":"2022-12-13T11:05:18.800743Z","shell.execute_reply.started":"2022-12-13T11:05:18.795454Z","shell.execute_reply":"2022-12-13T11:05:18.799554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 使用PyTorch中nn这个库来构建逻辑回归模型\nclass LogisticRegressionModel(nn.Module):\n    def __init__(self, n_feature):\n        super().__init__()    # 必须执行的标准步骤\n        self.linear = nn.Linear(n_feature, 1)   # 线性层\n        self.sigmoid = nn.Sigmoid()             # sigmoid  \n\n    def forward(self, x):\n        x = self.linear(x)\n        pred = self.sigmoid(x)\n        return pred\n\nmodel = LogisticRegressionModel(n_feature=2)\nprint(model)\n\n# 使用optim 提供了不同的梯度下降优化器，包括SGD, RMSProp, Adam等，这里我们采用最基本的随机梯度下降优化器SGD\noptimizer = torch.optim.SGD(\n    model.parameters(), # 需要优化的模型参数\n    lr=0.1          # 学习率\n)\n\n# 损失函数，采用2分类损失函 Binary Classification Error loss\nloss_func = torch.nn.BCELoss()","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:18.805695Z","iopub.execute_input":"2022-12-13T11:05:18.806065Z","iopub.status.idle":"2022-12-13T11:05:18.815605Z","shell.execute_reply.started":"2022-12-13T11:05:18.806033Z","shell.execute_reply":"2022-12-13T11:05:18.814405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 定义模型\nclass Net(torch.nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.fc1 = torch.nn.Linear(784, 256)\n        self.relu = torch.nn.ReLU()\n        self.fc3 = torch.nn.Linear(256, 10)\n    \n\n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.relu(x)\n        logits = self.fc3(x)\n        return logits\n    \nnet = Net()\nnet\n","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:18.817277Z","iopub.execute_input":"2022-12-13T11:05:18.817872Z","iopub.status.idle":"2022-12-13T11:05:18.835704Z","shell.execute_reply.started":"2022-12-13T11:05:18.817828Z","shell.execute_reply":"2022-12-13T11:05:18.834480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(net.parameters(), lr=0.0003)\nloss_func = torch.nn.CrossEntropyLoss()  # 使用交叉熵损失函数\n\n# 设置迭代次数\n#enumerate enumerate(sequence, [start=0]) 其中参数为: sequence一个序列\nfor epoch in range(5):\n    model.train()\n    for batch_idx, (data, target) in enumerate(train_dl):\n        cal_data = net(data)\n        # 将计算的数据与目标数据求误差损失\n        loss = loss_func(cal_data, target) ##一直更新？一个data的len16 ？\n\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        # 每隔200*batcsize(200) = 40000 打印输出结果  train_dl的 batch_size=16 \n        if batch_idx % 200 == 0:  #batch_index是每次取16个数据返回一个编号\n            print('训练: {} [{}/{} ({:.0f}%)]\\t Loss: {:.6f}'.format(\n                epoch, batch_idx * len(data), len(train_dl.dataset),\n                       100. * batch_idx / len(train_dl), loss.item())) #loss.item()?\n    \n    # 验证\n    model.eval()\n    test_loss = 0\n    correct = 0\n    for data, target in valid_dl:\n        logits = net(data)\n        test_loss += loss_func(logits, target).item()\n        # 取出预测最大值的索引编号，即预测值\n        pred = logits.data.argmax(dim=1)\n        # 统计正确预测的个数\n        correct += pred.eq(target.data).sum()\n\n    test_loss /= len(valid_dl.dataset)\n    # 打印输出测试误差及准确率\n    print('\\n测试: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\\n'.format(\n        test_loss, correct, len(valid_dl.dataset),\n        100. * correct / len(valid_dl.dataset)))","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:18.837173Z","iopub.execute_input":"2022-12-13T11:05:18.837489Z","iopub.status.idle":"2022-12-13T11:05:53.183024Z","shell.execute_reply.started":"2022-12-13T11:05:18.837461Z","shell.execute_reply":"2022-12-13T11:05:53.182128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 预测测试集合\nnet.eval()\npreds = []\nfor data in test_dl:\n    logits = net(data)\n    preds.append(logits.argmax(dim=1).cpu().numpy())  # 选择预测概率最高的类别作为预测结果","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.184253Z","iopub.execute_input":"2022-12-13T11:05:53.185086Z","iopub.status.idle":"2022-12-13T11:05:53.365322Z","shell.execute_reply.started":"2022-12-13T11:05:53.185052Z","shell.execute_reply":"2022-12-13T11:05:53.363987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = np.concatenate(preds)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.366976Z","iopub.execute_input":"2022-12-13T11:05:53.367351Z","iopub.status.idle":"2022-12-13T11:05:53.373819Z","shell.execute_reply.started":"2022-12-13T11:05:53.367318Z","shell.execute_reply":"2022-12-13T11:05:53.372503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[:10]","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.375462Z","iopub.execute_input":"2022-12-13T11:05:53.375807Z","iopub.status.idle":"2022-12-13T11:05:53.386579Z","shell.execute_reply.started":"2022-12-13T11:05:53.375777Z","shell.execute_reply":"2022-12-13T11:05:53.385545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = pd.Series(preds.reshape(1, -1)[0])\nsubmission = pd.DataFrame({'id': range(0,5000),\n                             'label': preds})\nsubmission.to_csv('../working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.388119Z","iopub.execute_input":"2022-12-13T11:05:53.388444Z","iopub.status.idle":"2022-12-13T11:05:53.402384Z","shell.execute_reply.started":"2022-12-13T11:05:53.388414Z","shell.execute_reply":"2022-12-13T11:05:53.401306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.403877Z","iopub.execute_input":"2022-12-13T11:05:53.404257Z","iopub.status.idle":"2022-12-13T11:05:53.413577Z","shell.execute_reply.started":"2022-12-13T11:05:53.404226Z","shell.execute_reply":"2022-12-13T11:05:53.412422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.414841Z","iopub.execute_input":"2022-12-13T11:05:53.415199Z","iopub.status.idle":"2022-12-13T11:05:53.430166Z","shell.execute_reply.started":"2022-12-13T11:05:53.415168Z","shell.execute_reply":"2022-12-13T11:05:53.428979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-12-13T11:05:53.433171Z","iopub.execute_input":"2022-12-13T11:05:53.433928Z","iopub.status.idle":"2022-12-13T11:05:53.445948Z","shell.execute_reply.started":"2022-12-13T11:05:53.433864Z","shell.execute_reply":"2022-12-13T11:05:53.444648Z"},"trusted":true},"execution_count":null,"outputs":[]}]}