Skip to content

Commit feee43d

Browse files
committed
Created using Colaboratory
1 parent 9303f2e commit feee43d

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed

Neural_Networks_using_Pytorch.ipynb

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyOqT4fV0HqqEjJ2PIVhiM/p",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
},
17+
"accelerator": "GPU",
18+
"gpuClass": "standard"
19+
},
20+
"cells": [
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {
24+
"id": "view-in-github",
25+
"colab_type": "text"
26+
},
27+
"source": [
28+
"<a href=\"https://colab.research.google.com/github/smlra-kjsce/Pytorch-101-2023/blob/main/Neural_Networks_using_Pytorch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"source": [
34+
"import torch"
35+
],
36+
"metadata": {
37+
"id": "tZeF_iz7llZh"
38+
},
39+
"execution_count": null,
40+
"outputs": []
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"metadata": {
46+
"colab": {
47+
"base_uri": "https://localhost:8080/"
48+
},
49+
"id": "RqcIgy3Iy5GF",
50+
"outputId": "67bc0af0-9d28-4f5d-ff99-9357f67ec58f"
51+
},
52+
"outputs": [
53+
{
54+
"output_type": "stream",
55+
"name": "stdout",
56+
"text": [
57+
"no of features: 4\n",
58+
"tensor([[1., 0., 1., 0.],\n",
59+
" [1., 0., 1., 1.],\n",
60+
" [0., 1., 0., 1.]]) \n",
61+
"\n",
62+
"tensor([[1.],\n",
63+
" [1.],\n",
64+
" [0.]])\n"
65+
]
66+
}
67+
],
68+
"source": [
69+
"\n",
70+
"#Input tensor\n",
71+
"X = torch.Tensor([[1,0,1,0],[1,0,1,1],[0,1,0,1]])\n",
72+
"\n",
73+
"print(\"no of features: \", X.shape[1])\n",
74+
"#Output\n",
75+
"y = torch.Tensor([[1],[1],[0]])\n",
76+
"\n",
77+
"\n",
78+
"print(X, '\\n')\n",
79+
"print(y)"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"source": [
85+
"#Sigmoid Function\n",
86+
"def sigmoid (x):\n",
87+
" return 1/(1 + torch.exp(-x))\n",
88+
"\n",
89+
"#Derivative of Sigmoid Function/\n",
90+
"def derivatives_sigmoid(x):\n",
91+
" return sigmoid(x) * (1 - sigmoid(x))"
92+
],
93+
"metadata": {
94+
"id": "nLfTl3Yny53Q"
95+
},
96+
"execution_count": null,
97+
"outputs": []
98+
},
99+
{
100+
"cell_type": "code",
101+
"source": [
102+
"#Variable initialization\n",
103+
"\n",
104+
"epoch=7000 #Setting training iterations\n",
105+
"lr=0.1 #Setting learning rate\n",
106+
"\n",
107+
"inputlayer_neurons = X.shape[1] #number of features in data set\n",
108+
"hiddenlayer_neurons = 3 #number of hidden layer neurons\n",
109+
"output_neurons = 1 #number of neurons in output layer\n",
110+
"\n",
111+
"#weight and bias initialization\n",
112+
"wh=torch.randn(inputlayer_neurons, hiddenlayer_neurons).type(torch.FloatTensor)\n",
113+
"bh=torch.randn(1, hiddenlayer_neurons).type(torch.FloatTensor)\n",
114+
"\n",
115+
"wout=torch.randn(hiddenlayer_neurons, output_neurons)\n",
116+
"bout=torch.randn(1, output_neurons)"
117+
],
118+
"metadata": {
119+
"id": "0rqFB4BHzDV2"
120+
},
121+
"execution_count": null,
122+
"outputs": []
123+
},
124+
{
125+
"cell_type": "code",
126+
"source": [
127+
"for i in range(epoch):\n",
128+
" #Forward Propogation\n",
129+
" hidden_layer_input1 = torch.mm(X, wh)\n",
130+
" hidden_layer_input = hidden_layer_input1 + bh\n",
131+
" hidden_layer_activations = sigmoid(hidden_layer_input)\n",
132+
"\n",
133+
" output_layer_input1 = torch.mm(hidden_layer_activations, wout)\n",
134+
" output_layer_input = output_layer_input1 + bout\n",
135+
" output = sigmoid(output_layer_input)\n",
136+
"\n",
137+
" #Backpropagation\n",
138+
" E = y-output\n",
139+
" slope_output_layer = derivatives_sigmoid(output)\n",
140+
" slope_hidden_layer = derivatives_sigmoid(hidden_layer_activations)\n",
141+
" d_output = E * slope_output_layer\n",
142+
" Error_at_hidden_layer = torch.mm(d_output, wout.t())\n",
143+
" d_hiddenlayer = Error_at_hidden_layer * slope_hidden_layer\n",
144+
" wout += torch.mm(hidden_layer_activations.t(), d_output) *lr\n",
145+
" bout += d_output.sum() *lr\n",
146+
" wh += torch.mm(X.t(), d_hiddenlayer) *lr\n",
147+
" bh += d_output.sum() *lr"
148+
],
149+
"metadata": {
150+
"id": "HqxBtOtCzFbf"
151+
},
152+
"execution_count": null,
153+
"outputs": []
154+
},
155+
{
156+
"cell_type": "code",
157+
"source": [
158+
"print('actual :\\n', y, '\\n')\n",
159+
"print('predicted :\\n', output)"
160+
],
161+
"metadata": {
162+
"colab": {
163+
"base_uri": "https://localhost:8080/"
164+
},
165+
"id": "Llt_Alt0zHq6",
166+
"outputId": "41ab11c6-4a95-416d-b414-85d379a2b724"
167+
},
168+
"execution_count": null,
169+
"outputs": [
170+
{
171+
"output_type": "stream",
172+
"name": "stdout",
173+
"text": [
174+
"actual :\n",
175+
" tensor([[1.],\n",
176+
" [1.],\n",
177+
" [0.]]) \n",
178+
"\n",
179+
"predicted :\n",
180+
" tensor([[0.9965],\n",
181+
" [0.9964],\n",
182+
" [0.0056]])\n"
183+
]
184+
}
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"source": [],
190+
"metadata": {
191+
"id": "M4ti0eLazJRb"
192+
},
193+
"execution_count": null,
194+
"outputs": []
195+
}
196+
]
197+
}

0 commit comments

Comments
 (0)