Skip to content

Commit

Permalink
Create 2.1-xtal2png-cnn-regression.ipynb
Browse files Browse the repository at this point in the history
@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
sgbaird committed Jun 1, 2022
1 parent a5dbbac commit a18aed1
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions notebooks/2.1-xtal2png-cnn-regression.ipynb
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
}

0 comments on commit a18aed1

Please sign in to comment.