two_layer_neural_network.Rmd
Source: https://github.com/jcjohnson/pytorch-examples#pytorch-nn
In this example we use the torch nn
package to implement our two-layer network:
library(rTorch)
device = torch$device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU
N is batch size;
D_in is input dimension;
H is hidden dimension;
D_out is output dimension.
Use the nn
package to define our model as a sequence of layers. nn.Sequential
is a Module which contains other Modules, and applies them in sequence to produce its output. Each Linear Module computes output from input using a linear function, and holds internal Tensors for its weight and bias. After constructing the model we use the .to()
method to move it to the desired device.
The nn
package also contains definitions of popular loss functions; in this case we will use Mean Squared Error (MSE) as our loss function. Setting reduction='sum'
means that we are computing the sum of squared errors rather than the mean; this is for consistency with the examples above where we manually compute the loss, but in practice it is more common to use mean squared error as a loss by setting reduction='elementwise_mean'
.
learning_rate = 1e-4
for (t in 1:500) {
# Forward pass: compute predicted y by passing x to the model. Module objects
# override the __call__ operator so you can call them like functions. When
# doing so you pass a Tensor of input data to the Module and it produces
# a Tensor of output data.
y_pred = model(x)
# Compute and print loss. We pass Tensors containing the predicted and true
# values of y, and the loss function returns a Tensor containing the loss.
loss = loss_fn(y_pred, y)
cat(t, "\t")
cat(loss$item(), "\n")
# Zero the gradients before running the backward pass.
model$zero_grad()
# Backward pass: compute gradient of the loss with respect to all the learnable
# parameters of the model. Internally, the parameters of each Module are stored
# in Tensors with requires_grad=True, so this call will compute gradients for
# all learnable parameters in the model.
loss$backward()
# Update the weights using gradient descent. Each parameter is a Tensor, so
# we can access its data and gradients like we did before.
with(torch$no_grad(), {
for (param in iterate(model$parameters())) {
# in Python this code is much simpler. In R we have to do some conversions
# param$data <- torch$sub(param$data,
# torch$mul(param$grad$float(),
# torch$scalar_tensor(learning_rate)))
param$data <- param$data - param$grad * learning_rate
}
})
}
#> 1 628.2839
#> 2 584.9592
#> 3 546.728
#> 4 512.6885
#> 5 482.1306
#> 6 454.6265
#> 7 429.5551
#> 8 406.399
#> 9 384.6441
#> 10 364.3125
#> 11 345.3866
#> 12 327.6258
#> 13 310.8002
#> 14 294.8379
#> 15 279.7325
#> 16 265.308
#> 17 251.6231
#> 18 238.5361
#> 19 226.0232
#> 20 214.1025
#> 21 202.7003
#> 22 191.8059
#> 23 181.4473
#> 24 171.5948
#> 25 162.2114
#> 26 153.2922
#> 27 144.8526
#> 28 136.86
#> 29 129.2786
#> 30 122.0849
#> 31 115.2736
#> 32 108.8145
#> 33 102.702
#> 34 96.9281
#> 35 91.47466
#> 36 86.3278
#> 37 81.47502
#> 38 76.89146
#> 39 72.56895
#> 40 68.47774
#> 41 64.62608
#> 42 60.99714
#> 43 57.56915
#> 44 54.3427
#> 45 51.30913
#> 46 48.4516
#> 47 45.7645
#> 48 43.2405
#> 49 40.86606
#> 50 38.63006
#> 51 36.52527
#> 52 34.54914
#> 53 32.69345
#> 54 30.9498
#> 55 29.30734
#> 56 27.76275
#> 57 26.31089
#> 58 24.9431
#> 59 23.6538
#> 60 22.43881
#> 61 21.29258
#> 62 20.21278
#> 63 19.19288
#> 64 18.23302
#> 65 17.32939
#> 66 16.47696
#> 67 15.67115
#> 68 14.90951
#> 69 14.19054
#> 70 13.51087
#> 71 12.86819
#> 72 12.26046
#> 73 11.68514
#> 74 11.14105
#> 75 10.62579
#> 76 10.13727
#> 77 9.67443
#> 78 9.235745
#> 79 8.81936
#> 80 8.423415
#> 81 8.047976
#> 82 7.691305
#> 83 7.352424
#> 84 7.030395
#> 85 6.724676
#> 86 6.434433
#> 87 6.158278
#> 88 5.895441
#> 89 5.645772
#> 90 5.408593
#> 91 5.182788
#> 92 4.967783
#> 93 4.762821
#> 94 4.567942
#> 95 4.381676
#> 96 4.203955
#> 97 4.034254
#> 98 3.872373
#> 99 3.717669
#> 100 3.569852
#> 101 3.42879
#> 102 3.293934
#> 103 3.165006
#> 104 3.041733
#> 105 2.923832
#> 106 2.810992
#> 107 2.702966
#> 108 2.599546
#> 109 2.500533
#> 110 2.405609
#> 111 2.314677
#> 112 2.227567
#> 113 2.143984
#> 114 2.063824
#> 115 1.986898
#> 116 1.913161
#> 117 1.842398
#> 118 1.77456
#> 119 1.709334
#> 120 1.64675
#> 121 1.586662
#> 122 1.528932
#> 123 1.473498
#> 124 1.420194
#> 125 1.369045
#> 126 1.319939
#> 127 1.272713
#> 128 1.227352
#> 129 1.183716
#> 130 1.141713
#> 131 1.101289
#> 132 1.062379
#> 133 1.024975
#> 134 0.9889802
#> 135 0.9543175
#> 136 0.9209599
#> 137 0.8888108
#> 138 0.8578573
#> 139 0.828075
#> 140 0.7993811
#> 141 0.7717422
#> 142 0.7451235
#> 143 0.7194628
#> 144 0.6947174
#> 145 0.6708718
#> 146 0.6479479
#> 147 0.6259133
#> 148 0.6046655
#> 149 0.5841969
#> 150 0.5644467
#> 151 0.5453879
#> 152 0.5270226
#> 153 0.509312
#> 154 0.4922202
#> 155 0.4757309
#> 156 0.4598286
#> 157 0.444492
#> 158 0.4296912
#> 159 0.4153878
#> 160 0.4015924
#> 161 0.3882794
#> 162 0.3754345
#> 163 0.3630294
#> 164 0.3510535
#> 165 0.3394946
#> 166 0.3283299
#> 167 0.3175527
#> 168 0.3071378
#> 169 0.2970789
#> 170 0.2873671
#> 171 0.2779845
#> 172 0.2689149
#> 173 0.2601575
#> 174 0.2516952
#> 175 0.2435159
#> 176 0.2356067
#> 177 0.2279707
#> 178 0.2206163
#> 179 0.2135031
#> 180 0.2066278
#> 181 0.199988
#> 182 0.1935741
#> 183 0.1873666
#> 184 0.1813661
#> 185 0.1755629
#> 186 0.1699517
#> 187 0.1645233
#> 188 0.1592781
#> 189 0.1542067
#> 190 0.149301
#> 191 0.1445545
#> 192 0.1399648
#> 193 0.1355246
#> 194 0.1312279
#> 195 0.1270718
#> 196 0.1230509
#> 197 0.1191625
#> 198 0.115399
#> 199 0.1117588
#> 200 0.1082348
#> 201 0.1048254
#> 202 0.1015256
#> 203 0.09833349
#> 204 0.09524446
#> 205 0.0922533
#> 206 0.08935806
#> 207 0.08655693
#> 208 0.08384982
#> 209 0.081232
#> 210 0.07869583
#> 211 0.07624327
#> 212 0.0738695
#> 213 0.07157086
#> 214 0.06934651
#> 215 0.0671912
#> 216 0.06510548
#> 217 0.0630878
#> 218 0.0611335
#> 219 0.05924014
#> 220 0.05740607
#> 221 0.05563042
#> 222 0.05391053
#> 223 0.05224558
#> 224 0.05063397
#> 225 0.04907332
#> 226 0.04756238
#> 227 0.04609917
#> 228 0.0446822
#> 229 0.04330759
#> 230 0.04197593
#> 231 0.04068634
#> 232 0.03943768
#> 233 0.03822812
#> 234 0.0370564
#> 235 0.03592146
#> 236 0.03482136
#> 237 0.03375633
#> 238 0.0327246
#> 239 0.03172487
#> 240 0.03075593
#> 241 0.02981687
#> 242 0.02890735
#> 243 0.02802622
#> 244 0.02717285
#> 245 0.02634558
#> 246 0.02554371
#> 247 0.02476675
#> 248 0.0240143
#> 249 0.02328515
#> 250 0.02257836
#> 251 0.02189306
#> 252 0.02122923
#> 253 0.02058575
#> 254 0.01996238
#> 255 0.01935788
#> 256 0.01877213
#> 257 0.01820429
#> 258 0.01765402
#> 259 0.01712051
#> 260 0.01660351
#> 261 0.01610242
#> 262 0.0156171
#> 263 0.01514632
#> 264 0.01469009
#> 265 0.01424752
#> 266 0.01381863
#> 267 0.01340292
#> 268 0.01299987
#> 269 0.01260905
#> 270 0.01223013
#> 271 0.01186274
#> 272 0.01150682
#> 273 0.0111617
#> 274 0.01082683
#> 275 0.01050223
#> 276 0.01018802
#> 277 0.009882836
#> 278 0.009586995
#> 279 0.009300183
#> 280 0.009021979
#> 281 0.008752229
#> 282 0.008490788
#> 283 0.008237117
#> 284 0.007991165
#> 285 0.007752791
#> 286 0.007521485
#> 287 0.00729729
#> 288 0.0070798
#> 289 0.006868829
#> 290 0.006664461
#> 291 0.006466233
#> 292 0.006273943
#> 293 0.006087383
#> 294 0.005906438
#> 295 0.005730878
#> 296 0.005560733
#> 297 0.005395614
#> 298 0.005235502
#> 299 0.005080266
#> 300 0.004929713
#> 301 0.004783633
#> 302 0.004641939
#> 303 0.004504489
#> 304 0.004371175
#> 305 0.004241788
#> 306 0.004116328
#> 307 0.003994633
#> 308 0.003876575
#> 309 0.003762146
#> 310 0.003650998
#> 311 0.003543299
#> 312 0.003438761
#> 313 0.003337309
#> 314 0.003238867
#> 315 0.0031434
#> 316 0.003050815
#> 317 0.002960989
#> 318 0.002873863
#> 319 0.002789256
#> 320 0.002707202
#> 321 0.002627575
#> 322 0.002550329
#> 323 0.002475383
#> 324 0.002402639
#> 325 0.002332083
#> 326 0.002263665
#> 327 0.002197253
#> 328 0.0021328
#> 329 0.002070281
#> 330 0.002009582
#> 331 0.001950708
#> 332 0.001893577
#> 333 0.001838136
#> 334 0.001784306
#> 335 0.001732112
#> 336 0.001681432
#> 337 0.0016323
#> 338 0.001584614
#> 339 0.001538318
#> 340 0.00149336
#> 341 0.001449755
#> 342 0.001407453
#> 343 0.001366422
#> 344 0.001326558
#> 345 0.001287868
#> 346 0.001250318
#> 347 0.001213876
#> 348 0.00117851
#> 349 0.0011442
#> 350 0.001110891
#> 351 0.001078564
#> 352 0.001047205
#> 353 0.001016748
#> 354 0.000987183
#> 355 0.0009584787
#> 356 0.0009306203
#> 357 0.0009035908
#> 358 0.0008773552
#> 359 0.0008518948
#> 360 0.0008271908
#> 361 0.0008032027
#> 362 0.0007798997
#> 363 0.0007572919
#> 364 0.0007353517
#> 365 0.0007140448
#> 366 0.0006933782
#> 367 0.0006733016
#> 368 0.0006538091
#> 369 0.0006348886
#> 370 0.0006165295
#> 371 0.0005986958
#> 372 0.0005813859
#> 373 0.0005645752
#> 374 0.0005482732
#> 375 0.0005324451
#> 376 0.0005170752
#> 377 0.00050215
#> 378 0.0004876566
#> 379 0.0004735905
#> 380 0.0004599217
#> 381 0.000446661
#> 382 0.0004337932
#> 383 0.000421295
#> 384 0.000409168
#> 385 0.000397383
#> 386 0.0003859476
#> 387 0.0003748359
#> 388 0.000364068
#> 389 0.0003535805
#> 390 0.0003434142
#> 391 0.0003335507
#> 392 0.0003239612
#> 393 0.0003146584
#> 394 0.0003056151
#> 395 0.0002968509
#> 396 0.0002883257
#> 397 0.0002800551
#> 398 0.0002720189
#> 399 0.000264218
#> 400 0.0002566472
#> 401 0.0002492882
#> 402 0.0002421443
#> 403 0.0002352033
#> 404 0.000228468
#> 405 0.000221927
#> 406 0.0002155735
#> 407 0.000209406
#> 408 0.0002034151
#> 409 0.0001976036
#> 410 0.0001919538
#> 411 0.000186465
#> 412 0.000181138
#> 413 0.0001759652
#> 414 0.0001709464
#> 415 0.0001660616
#> 416 0.0001613203
#> 417 0.0001567196
#> 418 0.0001522457
#> 419 0.0001479028
#> 420 0.0001436849
#> 421 0.0001395875
#> 422 0.0001356086
#> 423 0.0001317492
#> 424 0.0001279981
#> 425 0.0001243516
#> 426 0.0001208117
#> 427 0.0001173674
#> 428 0.0001140324
#> 429 0.0001107887
#> 430 0.0001076371
#> 431 0.000104577
#> 432 0.0001016119
#> 433 9.872603e-05
#> 434 9.591727e-05
#> 435 9.320263e-05
#> 436 9.055879e-05
#> 437 8.798708e-05
#> 438 8.549038e-05
#> 439 8.306588e-05
#> 440 8.071403e-05
#> 441 7.842657e-05
#> 442 7.62079e-05
#> 443 7.40515e-05
#> 444 7.195051e-05
#> 445 6.991543e-05
#> 446 6.79373e-05
#> 447 6.601425e-05
#> 448 6.414913e-05
#> 449 6.233487e-05
#> 450 6.057473e-05
#> 451 5.886107e-05
#> 452 5.719856e-05
#> 453 5.558319e-05
#> 454 5.401246e-05
#> 455 5.24875e-05
#> 456 5.100614e-05
#> 457 4.957083e-05
#> 458 4.817137e-05
#> 459 4.681194e-05
#> 460 4.549512e-05
#> 461 4.420892e-05
#> 462 4.296327e-05
#> 463 4.175395e-05
#> 464 4.057565e-05
#> 465 3.943508e-05
#> 466 3.832371e-05
#> 467 3.724535e-05
#> 468 3.619579e-05
#> 469 3.517841e-05
#> 470 3.419127e-05
#> 471 3.322741e-05
#> 472 3.229243e-05
#> 473 3.138653e-05
#> 474 3.050298e-05
#> 475 2.964781e-05
#> 476 2.881304e-05
#> 477 2.800681e-05
#> 478 2.721956e-05
#> 479 2.645602e-05
#> 480 2.57129e-05
#> 481 2.499174e-05
#> 482 2.429051e-05
#> 483 2.361012e-05
#> 484 2.294818e-05
#> 485 2.230642e-05
#> 486 2.167962e-05
#> 487 2.107264e-05
#> 488 2.048336e-05
#> 489 1.991114e-05
#> 490 1.935353e-05
#> 491 1.88103e-05
#> 492 1.82841e-05
#> 493 1.777284e-05
#> 494 1.727619e-05
#> 495 1.67922e-05
#> 496 1.632309e-05
#> 497 1.586889e-05
#> 498 1.54238e-05
#> 499 1.499398e-05
#> 500 1.457493e-05
These two expression are equivalent, with the first being the long version natural way of doing it in PyTorch. The second is using the generics in R for subtraction, multiplication and scalar conversion.
param$data <- torch$sub(param$data,
torch$mul(param$grad$float(),
torch$scalar_tensor(learning_rate)))
}
param$data <- param$data - param$grad * learning_rate