-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2.1-xtal2png-cnn-regression.ipynb
@faris-k, just leftover from before I messaged you. No need to use this, but the final presentation will probably be in notebook form
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"Modified from https://skorch.readthedocs.io/en/stable/user/quickstart.html\"\"\"\n", | ||
"from skorch.regressor import NeuralNetRegressor\n", | ||
"import numpy as np\n", | ||
"from sklearn.datasets import make_classification\n", | ||
"from torch import nn\n", | ||
"import torch.nn.functional as F\n", | ||
"import torch\n", | ||
"import torch.nn as nn\n", | ||
"import torch.optim as optim\n", | ||
"from sklearn.metrics import mean_absolute_error, mean_squared_error\n", | ||
"from skorch.callbacks import EarlyStopping\n", | ||
"\n", | ||
"\n", | ||
"# Regular PyTorch Module\n", | ||
"class ConvNet(torch.nn.Module):\n", | ||
" def __init__(self, in_channels, num_classes):\n", | ||
" super().__init__()\n", | ||
"\n", | ||
" # num_classes is used by the corn loss function\n", | ||
" self.num_classes = num_classes\n", | ||
"\n", | ||
" # Initialize CNN layers\n", | ||
" all_layers = [\n", | ||
" torch.nn.Conv2d(in_channels=in_channels, out_channels=3, \n", | ||
" kernel_size=(3, 3), stride=(1, 1), \n", | ||
" padding=1),\n", | ||
" torch.nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)),\n", | ||
" torch.nn.Conv2d(in_channels=3, out_channels=6, \n", | ||
" kernel_size=(3, 3), stride=(1, 1), \n", | ||
" padding=1),\n", | ||
" torch.nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2)),\n", | ||
" torch.nn.Flatten()\n", | ||
" ]\n", | ||
" \n", | ||
" # CORN output layer --------------------------------------\n", | ||
" # Regular classifier would use num_classes instead of \n", | ||
" # num_classes-1 below\n", | ||
" output_layer = torch.nn.Linear(294, num_classes-1)\n", | ||
" # ---------------------------------------------------------\n", | ||
" \n", | ||
" all_layers.append(output_layer)\n", | ||
" self.model = torch.nn.Sequential(*all_layers)\n", | ||
" \n", | ||
" def forward(self, x):\n", | ||
" x = self.model(x)\n", | ||
" return x\n", | ||
"\n", | ||
"net = NeuralNetRegressor(\n", | ||
" ConvNet(dropout=0.1),\n", | ||
" criterion=nn.MSELoss,\n", | ||
" max_epochs=100,\n", | ||
" optimizer=optim.Adam,\n", | ||
" optimizer__lr = .005,\n", | ||
" callbacks=[EarlyStopping()],\n", | ||
")\n", | ||
"\n", | ||
"net.fit(X_train, y_train)\n", | ||
"y_pred = net.predict(X_val)\n", | ||
"\n", | ||
"mae = mean_absolute_error(y_val, y_pred)\n", | ||
"rmse = mean_squared_error(y_val, y_pred, squared=False)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"language_info": { | ||
"name": "python" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |