Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 0,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "import torch\n",
- "import torch.nn as nn\n",
- "import torch.optim as optim\n",
- "import torch.nn.functional as F"
- ]
- },{
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "import torch
- import torch.nn as nn
- import torch.nn.functional as F
- import torch.optim as optim
- import numpy as np
- class Testen(nn.Module): \n",
- " def __init__(self): \n",
- " super(Testen, self).__init__() \n",
- " self.layer_1 = torch.nn.Linear(20, 20)\n",
- " self.layer_2 = torch.nn.Linear(30, 30)\n",
- " self.layer_3 = torch.nn.Linear(30, 50)\n",
- "\n",
- " def forward(self, x):\n",
- " x = x.view(-1, 3072)\n",
- " x = F.relu(self.layer_1(x))\n",
- " x = F.relu(self.layer_2(x))\n",
- " x = F.relu(self.layer_3(x))\n",
- " return x\n"
- ]
- },{
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "net = Rompear()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "criterion = nn.CrossEntropyLoss() \n",
- "optimizer = optim.Adam(net.parameters(), lr = 0.0001)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n",
- "net.to(device)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "##############################\n",
- "# REPLACE WITH YOUR OWN DATA #\n",
- "##############################\n",
- "x = torch.from_numpy(np.random.rand(1042,32,3,32,32)).type(dtype=torch.float).to(device) #(samples, channels, heigth, width) \n",
- "y = torch.from_numpy(np.random.randint(20,size=(1042, 32))).to(device) #(number_of_classes, samples)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "for epoch in range(2): # loop over the dataset multiple times \n",
- " running_loss = 0.0 \n",
- " for i in range(0, x.shape[0]):\n",
- " # get the inputs \n",
- " inputs, labels = x[i], y[i]\n",
- "\n",
- " # zero the parameter gradients \n",
- " optimizer.zero_grad() \n",
- "\n",
- " # forward + backward + optimize \n",
- " outputs = net(inputs) \n",
- " loss = criterion(outputs, labels) \n",
- " loss.backward() \n",
- " optimizer.step() \n",
- "\n",
- " # print statistics \n",
- " running_loss += loss.item() \n",
- " if i % 200 == 199:\n",
- " print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 200)) \n",
- " running_loss = 0.0 \n",
- "print('Finished Training')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.5.4"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Add Comment
Please, Sign In to add comment