-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathREADME.Rmd
1205 lines (879 loc) · 20.5 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[![Travis build status](https://travis-ci.org/f0nzie/rTorch.svg?branch=master)](https://travis-ci.org/f0nzie/rTorch)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/f0nzie/rTorch?branch=master&svg=true)](https://ci.appveyor.com/project/f0nzie/rTorch)
<!-- badges: end -->
# rTorch
The goal of `rTorch` is providing an R wrapper to [PyTorch](https://pytorch.org/). `rTorch` provides all the functionality of PyTorch plus all the features that R provides. We have borrowed some ideas and code used in R [tensorflow](https://github.com/rstudio/tensorflow) to implement `rTorch`.
Besides the module `torch`, which directly provides `PyTorch` methods, classes and functions, the package also provides the modules `numpy` as a method called `np`, and `torchvision`, as well. The dollar sign `$` after the module will provide you access to all their sub-objects. Example:
```{r}
tv <- rTorch::torchvision
tv
np <- rTorch::np
np
torch_module <- rTorch::torch
torch_module
```
## rTorch Examples
To lighten up the time in building this `rTorch` package, we moved the examples that use _tensor operations_ and _neural networks_ to separate repositories. There are two sets of examples:
* _The rTorch Minimal Book_ at https://f0nzie.github.io/rtorch-minimal-book/
* _rTorch Advanced Examples_ at https://github.com/f0nzie/rTorch.examples
## rTorch installation
`rTorch` is available via CRAN and from GitHub.
### From CRAN
Install from CRAN using `install.packages("rTorch")` from the R console, or from *RStudio* using `Tools`, `Install Packages` from the menu.
### From GitHub
For the latest version install from GitHub.
Install `rTorch` with:
`devtools::install_github("f0nzie/rTorch")`
Installing from GitHub gives you the flexibility of experimenting with the latest development version of `rTorch`. For instance, to install `rTorch` from the `develop` branch:
`devtools::install_github("f0nzie/rTorch", ref="develop")`
or clone with Git with:
```
git clone https://github.com/f0nzie/rTorch.git
```
# Getting Started
## Tensor types
There are five major type of Tensors in PyTorch:
* Byte
* Float
* Double
* Long
* Bool
```{r}
library(rTorch)
byte_tensor <- torch$ByteTensor(3L, 3L)
float_tensor <- torch$FloatTensor(3L, 3L)
double_tensor <- torch$DoubleTensor(3L, 3L)
long_tensor <- torch$LongTensor(3L, 3L)
bool_tensor <- torch$BoolTensor(5L, 5L)
byte_tensor
float_tensor
double_tensor
long_tensor
bool_tensor
```
A `4D` tensor like in MNIST hand-written digits recognition dataset:
```{r}
mnist_4d <- torch$FloatTensor(60000L, 3L, 28L, 28L)
# size
mnist_4d$size()
# length
length(mnist_4d)
# shape, like in numpy
mnist_4d$shape
# number of elements
mnist_4d$numel()
```
A `3D` tensor:
```{r}
ft3d <- torch$FloatTensor(4L, 3L, 2L)
ft3d
```
```{r}
# get first element in a tensor
ft3d[1, 1, 1]
```
```{r}
# create a tensor with a value
torch$full(list(2L, 3L), 3.141592)
```
## Basic Tensor Operations
### Add tensors
```{r}
# 3x5 matrix uniformly distributed between 0 and 1
mat0 <- torch$FloatTensor(3L, 5L)$uniform_(0L, 1L)
# fill a 3x5 matrix with 0.1
mat1 <- torch$FloatTensor(3L, 5L)$uniform_(0.1, 0.1)
# a vector with all ones
mat2 <- torch$FloatTensor(5L)$uniform_(1, 1)
```
```{r}
# add two tensors
mat0 + mat1
```
```{r}
# add three tensors
mat0 + mat1 + mat2
```
```{r}
# PyTorch add two tensors using add() function
x = torch$rand(5L, 4L)
y = torch$rand(5L, 4L)
print(x$add(y))
print(x + y)
```
### Add a tensor element to another tensor
```{r}
# add an element of a tensor to another tensor
mat1[1, 1] + mat2
```
```{r}
mat1
```
```{r}
# extract part of the tensor
indices <- torch$tensor(c(0L, 3L))
torch$index_select(mat1, 1L, indices) # rows = 0; columns = 1
```
### Add a scalar to a tensor
```{r}
# add a scalar to a tensor
mat0 + 0.1
```
### Multiply a tensor by a scalar
```{r}
# Multiply tensor by scalar
tensor = torch$ones(4L, dtype=torch$float64)
scalar = np$float64(4.321)
message("a numpy scalar: ", scalar)
message("a PyTorch scalar: ", torch$scalar_tensor(scalar))
message("\nResult")
(prod = torch$mul(tensor, torch$scalar_tensor(scalar)))
```
```{r}
# short version using generics
(prod = tensor * scalar)
```
### Multiply two 1D tensors
```{r}
t1 = torch$tensor(c(1, 2))
t2 = torch$tensor(c(3, 2))
t1
t2
```
```{r}
t1 * t2
```
```{r}
t1 = torch$tensor(list(
c(1, 2, 3),
c(1, 2, 3)
))
t2 = torch$tensor(list(
c(1, 2),
c(1, 2),
c(1, 2)
))
t1
t2
```
```{r}
torch$mm(t1, t2)
```
### Dot product for 1D tensors (vectors)
```{r}
t1 = torch$tensor(c(1, 2))
t2 = torch$tensor(c(3, 2))
t1
t2
```
```{r}
# dot product of two vectors
torch$dot(t1, t2)
```
```{r}
# Dot product of 1D tensors is a scalar
p <- torch$Tensor(list(4L, 2L))
q <- torch$Tensor(list(3L, 1L))
(r = torch$dot(p, q)) # 14
(r <- p %.*% q)
```
```{r}
# torch$dot product will work for vectors not matrices
t1 = torch$tensor(list(
c(1, 2, 3),
c(1, 2, 3)
))
t2 = torch$tensor(list(
c(1, 2),
c(1, 2),
c(1, 2)
))
t1$shape
t2$shape
```
```{r invalid-dot-product-1, eval=FALSE}
# RuntimeError: 1D tensors expected, got 2D, 2D tensors
torch$dot(t1, t2)
```
### Dot product for 2D tensors (matrices)
The number of columns of the first matrix must be equal to the number of rows of the second matrix.
```{r}
# for the dot product of nD tensors we use torch$mm()
t1 = torch$tensor(list(
c(1, 2, 3),
c(1, 2, 3)
))
t2 = torch$tensor(list(
c(1, 2),
c(1, 2),
c(1, 2)
))
torch$mm(t1, t2)
```
```{r}
torch$mm(t2, t1)
```
```{r}
# for the dot product of 2D tensors we use torch$mm()
t1 = torch$arange(1, 11)$view(c(2L,5L))
t2 = torch$arange(11, 21)$view(c(5L,2L))
t1
t2
```
```{r}
# result
torch$mm(t1, t2)
```
### Multiplication for nD tensors
```{r}
# 1D tensor
t1 = torch$tensor(c(1, 2))
t2 = torch$tensor(c(3, 2))
torch$matmul(t1, t2)
```
```{r}
# 2D tensor
t1 = torch$tensor(list(
c(1, 2, 3),
c(1, 2, 3)
))
t2 = torch$tensor(list(
c(1, 2),
c(1, 2),
c(1, 2)
))
torch$matmul(t1, t2)
```
```{r}
# for the dot product of 3D tensors we use torch$matmul()
t1 = torch$arange(1, 13)$view(c(2L, 2L, 3L)) # number of columns = 2
t2 = torch$arange(0, 18)$view(c(2L, 3L, 3L)) # number of rows = 2
t1
t2
message("result")
torch$matmul(t1, t2)
```
```{r}
t1 = torch$arange(1, 13)$view(c(3L, 2L, 2L)) # number of columns = 3
t2 = torch$arange(0, 12)$view(c(3L, 2L, 2L)) # number of rows = 3
t1
t2
message("result")
torch$matmul(t1, t2)
```
### cross product
```{r}
m1 = torch$ones(3L, 5L)
m2 = torch$ones(3L, 5L)
v1 = torch$ones(3L)
# Cross product
# Size 3x5
(r = torch$cross(m1, m2))
```
## NumPy and PyTorch
`numpy` has been made available as a module inside `rTorch`. We could call functions from `numpy` refrerring to it as `np$any_function`. Examples:
```{r}
# a 2D numpy array
syn0 <- np$random$rand(3L, 5L)
syn0
```
```{r}
# numpy arrays of zeros
syn1 <- np$zeros(c(5L, 10L))
syn1
```
```{r}
# add a scalar to a numpy array
syn1 = syn1 + 0.1
syn1
```
```{r}
# in numpy a multidimensional array needs to be defined with a tuple
# From R we use a vector to refer to a tuple in Python
l1 <- np$ones(c(5L, 5L))
l1
```
```{r}
# vector-matrix multiplication
np$dot(syn0, syn1)
```
```{r}
# build a numpy array from three R vectors
X <- np$array(rbind(c(1,2,3), c(4,5,6), c(7,8,9)))
X
```
```{r}
# transpose the array
np$transpose(X)
```
### Copying a numpy object
With newer PyTorch versions we should work with NumPy array copies
There have been minor changes in the latest versions of PyTorch that prevents a direct use of a NumPy array. You will get this warning:
sys:1: UserWarning: The given NumPy array is not writeable, and PyTorch does
not support non-writeable tensors. This means you can write to the underlying
(supposedly non-writeable) NumPy array using the tensor. You may want to copy
the array to protect its data or make it writeable before converting it to a
tensor. This type of warning will be suppressed for the rest of this program.
For instance, this code will produce the warning:
```{r, eval=FALSE}
# as_tensor. Modifying tensor modifies numpy object as well
a = np$array(list(1, 2, 3))
t = torch$as_tensor(a)
print(t)
torch$tensor(list( 1, 2, 3))
t[1L]$fill_(-1)
print(a)
```
while this other one -with some extra code- will not:
```{r}
a = np$array(list(1, 2, 3))
a_copy = r_to_py(a)$copy() # we make a copy of the numpy array first
t = torch$as_tensor(a_copy)
print(t)
torch$tensor(list( 1, 2, 3))
t[1L]$fill_(-1)
print(a)
```
### Function `make_copy()`
To make easier to copy an object in `rTorch` we implemented the function `make_copy`, which makes a safe copy regardless if it is a torch, numpy or an R type object.
```{r}
a = np$array(list(1, 2, 3, 4, 5))
a_copy <- make_copy(a)
t <- torch$as_tensor(a_copy)
t
```
### Convert a numpy array to a tensor
```{r}
# convert a numpy array to a tensor
np_a = np$array(c(c(3, 4), c(3, 6)))
t_a = torch$from_numpy(r_to_py(np_a)$copy())
print(t_a)
```
## Creating tensors
### Random tensor
```{r}
# a random 1D tensor
np_arr <- np$random$rand(5L)
ft1 <- torch$FloatTensor(r_to_py(np_arr)$copy()) # make a copy of numpy array
ft1
```
```{r}
# tensor as a float of 64-bits
np_copy <- r_to_py(np$random$rand(5L))$copy() # make a copy of numpy array
ft2 <- torch$as_tensor(np_copy, dtype= torch$float64)
ft2
```
This is a very common operation in machine learning:
```{r}
# convert tensor to a numpy array
a = torch$rand(5L, 4L)
b = a$numpy()
print(b)
```
### Change the type of a tensor
```{r}
# convert tensor to float 16-bits
ft2_dbl <- torch$as_tensor(ft2, dtype = torch$float16)
ft2_dbl
```
### Create an uninitialized tensor
Create a tensor of size (5 x 7) with uninitialized memory:
```{r}
a <- torch$FloatTensor(5L, 7L)
print(a)
```
### Create a tensor and then change its shape
```{r}
# using arange to create tensor. starts from 0
v = torch$arange(9L)
(v = v$view(3L, 3L))
```
## Distributions
Initialize a tensor randomized with a normal distribution with mean=0, var=1:
```{r}
a <- torch$randn(5L, 7L)
print(a)
print(a$size())
```
### Uniform matrix
```{r}
library(rTorch)
# 3x5 matrix uniformly distributed between 0 and 1
mat0 <- torch$FloatTensor(3L, 5L)$uniform_(0L, 1L)
# fill a 3x5 matrix with 0.1
mat1 <- torch$FloatTensor(3L, 5L)$uniform_(0.1, 0.1)
# a vector with all ones
mat2 <- torch$FloatTensor(5L)$uniform_(1, 1)
mat0
mat1
```
### Binomial distribution
```{r}
Binomial <- torch$distributions$binomial$Binomial
m = Binomial(100, torch$tensor(list(0 , .2, .8, 1)))
(x = m$sample())
```
```{r}
m = Binomial(torch$tensor(list(list(5.), list(10.))),
torch$tensor(list(0.5, 0.8)))
(x = m$sample())
```
### Exponential distribution
```{r}
Exponential <- torch$distributions$exponential$Exponential
m = Exponential(torch$tensor(list(1.0)))
m$sample() # Exponential distributed with rate=1
```
### Weibull distribution
```{r}
Weibull <- torch$distributions$weibull$Weibull
m = Weibull(torch$tensor(list(1.0)), torch$tensor(list(1.0)))
m$sample() # sample from a Weibull distribution with scale=1, concentration=1
```
## Tensor default data types
Only floating-point types are supported as the default type.
### float32
```{r}
# Default data type
torch$tensor(list(1.2, 3))$dtype # default for floating point is torch.float32
```
### float64
```{r}
# change default data type to float64
torch$set_default_dtype(torch$float64)
torch$tensor(list(1.2, 3))$dtype # a new floating point tensor
```
### double
```{r}
torch$set_default_dtype(torch$double)
torch$tensor(list(1.2, 3))$dtype
```
## Tensor resizing
### Using *view*
```{r}
x = torch$randn(2L, 3L) # Size 2x3
y = x$view(6L) # Resize x to size 6
z = x$view(-1L, 2L) # Size 3x2
print(y)
print(z)
```
```{r}
# 0 1 2
# 3 4 5
# 6 7 8
v = torch$arange(9L)
(v = v$view(3L, 3L))
```
### Concatenating tensors
```{r}
# concatenate tensors
x = torch$randn(2L, 3L)
print(x)
# concatenate tensors by dim=0"
torch$cat(list(x, x, x), 0L)
# concatenate tensors by dim=1
torch$cat(list(x, x, x), 1L)
```
### Reshape tensors
```{r}
# ----- Reshape tensors -----
img <- torch$ones(3L, 28L, 28L)
print(img$size())
img_chunks <- torch$chunk(img, chunks = 3L, dim = 0L)
print(length(img_chunks))
# 1st chunk member
img_chunk_1 <- img_chunks[[1]]
print(img_chunk_1$size())
print(img_chunk_1$sum())
# 2nd chunk member
img_chunk_1 <- img_chunks[[2]]
print(img_chunk_1$size())
print(img_chunk_1$sum())
# index_select. get layer 1
indices = torch$tensor(c(0L))
img2 <- torch$index_select(img, dim = 0L, index = indices)
print(img2$size())
print(img2$sum())
# index_select. get layer 2
indices = torch$tensor(c(1L))
img2 <- torch$index_select(img, dim = 0L, index = indices)
print(img2$size())
print(img2$sum())
# index_select. get layer 3
indices = torch$tensor(c(2L))
img2 <- torch$index_select(img, dim = 0L, index = indices)
print(img2$size())
print(img2$sum())
```
## Special tensors
### Identity matrix
```{r}
# identity matrix
eye = torch$eye(3L) # Create an identity 3x3 tensor
print(eye)
```
### Ones
```{r}
(v = torch$ones(10L)) # A tensor of size 10 containing all ones
(v = torch$ones(2L, 1L, 2L, 1L)) # Size 2x1x2x1
```
### Eye
```{r}
v = torch$ones_like(eye) # A tensor with same shape as eye. Fill it with 1.
v
```
### Zeros
```{r}
(z = torch$zeros(10L)) # A tensor of size 10 containing all zeros
```
## Tensor fill
### Fill with a unique value
```{r}
# a tensor filled with ones
(v = torch$ones(3L, 3L))
```
### Change the tensor values by rows
```{r}
# change two rows in the tensor
# we are using 1-based index
v[2L, ]$fill_(2L) # fill row 1 with 2s
v[3L, ]$fill_(3L) # fill row 2 with 3s
```
```{r}
print(v)
```
### Fill a tensor with a set increment
```{r}
# Initialize Tensor with a range of values
(v = torch$arange(10L)) # similar to range(5) but creating a Tensor
```
```{r}
(v = torch$arange(0L, 10L, step = 1L)) # Size 5. Similar to range(0, 5, 1)
```
### With decimal increments
```{r}
u <- torch$arange(0, 10, step = 0.5)
u
```
### Including the ending value
```{r}
# range of values with increments including the end value
start <- 0
end <- 10
step <- 0.25
w <- torch$arange(start, end+step, step)
w
```
### Initialize a linear or log scale Tensor
```{r}
# Initialize a linear or log scale Tensor
# Create a Tensor with 10 linear points for (1, 10) inclusively
(v = torch$linspace(1L, 10L, steps = 10L))
# Size 5: 1.0e-10 1.0e-05 1.0e+00, 1.0e+05, 1.0e+10
(v = torch$logspace(start=-10L, end = 10L, steps = 5L))
```
### In-place / Not-in-place
```{r}
a = torch$rand(5L, 4L)
print(class(a))
```
```{r}
# converting the tensor to a numpy array, R automatically converts it
b = a$numpy()
print(class(b))
```
```{r}
a$fill_(3.5)
# a has now been filled with the value 3.5
# add a scalar to a tensor.
# notice that was auto-converted from an array to a tensor
b <- a$add(4.0)
# a is still filled with 3.5
# new tensor b is returned with values 3.5 + 4.0 = 7.5
print(a)
print(b)
```
### Tensor element assigment not implemented yet
```{r, eval=FALSE}
# this will throw an error because we don't still have a function for assignment
a[1, 1] <- 7.7
print(a)
# Error in a[1, 1] <- 7.7 : object of type 'environment' is not subsettable
```
```{r}
# This would be the right wayy to assign a value to a tensor element
a[1, 1]$fill_(7.7)
```
```{r}
# we can see that the first element has been changed
a
```
> Some operations like`narrow` do not have in-place versions, and hence, `.narrow_` does not exist. Similarly, some operations like `fill_` do not have an out-of-place version, so `.fill` does not exist.
```{r}
# a[[0L, 3L]]
a[1, 4]
```
## Access to tensor elements
### Change a tensor element given its index
```{r}
# replace an element at position 0, 0
(new_tensor = torch$Tensor(list(list(1, 2), list(3, 4))))
```
```{r}
# first row, firt column
print(new_tensor[1L, 1L])
```
```{r}
# change row 1, col 1 with value of 5
new_tensor[1L, 1L]$fill_(5)
```
```{r}
# which is the same as doing this
new_tensor[1, 1]$fill_(5)
```
> Notice that the element was changed in-place because of `fill_`.
### In R the index is 1-based
```{r}
print(new_tensor) # tensor([[ 5., 2.],[ 3., 4.]])
```
```{r}
# access an element at position (1, 0), 0-based index
print(new_tensor[2L, 1L]) # tensor([ 3.])
```
```{r}
# convert it to a scalar value
print(new_tensor[2L, 1L]$item()) # 3.
```
```{r}
# which is the same as
print(new_tensor[2, 1])
```
```{r}
# and the scalar
print(new_tensor[2, 1]$item())
```
### Extract part of a tensor
```{r}
# Select indices
x = torch$randn(3L, 4L)
print(x)
```
```{r}
# extract first and third row
# Select indices, dim=0
indices = torch$tensor(list(0L, 2L))
torch$index_select(x, 0L, indices)
```
```{r}
# extract first and third column
# Select indices, dim=1
torch$index_select(x, 1L, indices)
```
```{r}
# Take by indices
src = torch$tensor(list(list(4, 3, 5),
list(6, 7, 8)) )
print(src)
print( torch$take(src, torch$tensor(list(0L, 2L, 5L))) )
```
## Tensor operations
### Transpose
```{r}
# two dimensions: 3x3
x <- torch$arange(9L)
x <- x$view(c(3L, 3L))
t <- torch$transpose(x, 0L, 1L)
x # "Original tensor"
t # "Transposed"
```
```{r}
# three dimensions: 1x2x3
x <- torch$ones(c(1L, 2L, 3L))
t <- torch$transpose(x, 1L, 0L)
print(x) # original tensor
print(t) # transposed
print(x$shape) # original tensor
print(t$shape) # transposed
```
### Permutation
### permute a 2D tensor
```{r}
x <- torch$tensor(list(list(list(1,2)), list(list(3,4)), list(list(5,6))))
xs <- torch$as_tensor(x$shape)
xp <- x$permute(c(1L, 2L, 0L))
xps <- torch$as_tensor(xp$shape)
print(x) # original tensor
print(xp) # permuted tensor
print(xs) # shape original tensor
print(xps) # shape permuted tensor
```