|
361 | 361 | }, |
362 | 362 | "outputs": [], |
363 | 363 | "source": [ |
364 | | - "torch.arange(3.0f, 5.0f, step=0.1f)" |
| 364 | + "torch.arange(3.0f, 5.0f, step=0.1)" |
365 | 365 | ] |
366 | 366 | }, |
367 | 367 | { |
368 | 368 | "cell_type": "markdown", |
369 | 369 | "metadata": {}, |
370 | 370 | "source": [ |
371 | | - "There is no way to make `arange` produce anything but a 1D tensor, so a common thing to do is to reshape it as soon as it's created:" |
| 371 | + "## reshape()\n", |
| 372 | + "\n", |
| 373 | + "There is no way to make `arange` produce anything but a 1D tensor, so a common thing to do is to reshape it as soon as it's created." |
372 | 374 | ] |
373 | 375 | }, |
374 | 376 | { |
|
381 | 383 | }, |
382 | 384 | "outputs": [], |
383 | 385 | "source": [ |
384 | | - "torch.arange(3.0f, 5.0f, step=0.1f).reshape(4L,5L)" |
| 386 | + "torch.arange(3.0f, 5.0f, step=0.1f).reshape(4,5)" |
385 | 387 | ] |
386 | 388 | }, |
387 | 389 | { |
|
401 | 403 | }, |
402 | 404 | "outputs": [], |
403 | 405 | "source": [ |
404 | | - "torch.arange(3.0f, 5.0f, step=0.1f).reshape(4L,5L).str(\"0.00\")" |
| 406 | + "torch.arange(3.0f, 5.0f, step=0.1f).reshape(4,5).str(\"0.00\")" |
| 407 | + ] |
| 408 | + }, |
| 409 | + { |
| 410 | + "cell_type": "markdown", |
| 411 | + "metadata": {}, |
| 412 | + "source": [ |
| 413 | + "`reshape()` is, of course, useful for many other things, too, not just shaping the result of `arange()`. One thing that is very useful to know is that you can pass in '-1' for __one__ of the dimensions, and it has a very special meaning. Let's look at an example:" |
| 414 | + ] |
| 415 | + }, |
| 416 | + { |
| 417 | + "cell_type": "code", |
| 418 | + "execution_count": null, |
| 419 | + "metadata": { |
| 420 | + "dotnet_interactive": { |
| 421 | + "language": "fsharp" |
| 422 | + } |
| 423 | + }, |
| 424 | + "outputs": [], |
| 425 | + "source": [ |
| 426 | + "let t = torch.rand(3,4,4,4);\n", |
| 427 | + "t.reshape(12, 4, 4).ToString()" |
| 428 | + ] |
| 429 | + }, |
| 430 | + { |
| 431 | + "cell_type": "code", |
| 432 | + "execution_count": null, |
| 433 | + "metadata": { |
| 434 | + "dotnet_interactive": { |
| 435 | + "language": "fsharp" |
| 436 | + } |
| 437 | + }, |
| 438 | + "outputs": [], |
| 439 | + "source": [ |
| 440 | + "t.reshape(-1,4,4).ToString()" |
| 441 | + ] |
| 442 | + }, |
| 443 | + { |
| 444 | + "cell_type": "code", |
| 445 | + "execution_count": null, |
| 446 | + "metadata": { |
| 447 | + "dotnet_interactive": { |
| 448 | + "language": "fsharp" |
| 449 | + } |
| 450 | + }, |
| 451 | + "outputs": [], |
| 452 | + "source": [ |
| 453 | + "t.reshape(4,-1,6).ToString()" |
| 454 | + ] |
| 455 | + }, |
| 456 | + { |
| 457 | + "cell_type": "markdown", |
| 458 | + "metadata": {}, |
| 459 | + "source": [ |
| 460 | + "As you can see, -1 is a wildcard. After the rest of the arguments specify their respective sizes, the -1 dimension is determined from the overall number of elements and the dimensions that have been specified. Obviously, it can only be used to construct a proper tensor if the other dimensions are correct. This, for example, results in an excpetion:" |
| 461 | + ] |
| 462 | + }, |
| 463 | + { |
| 464 | + "cell_type": "code", |
| 465 | + "execution_count": null, |
| 466 | + "metadata": { |
| 467 | + "dotnet_interactive": { |
| 468 | + "language": "fsharp" |
| 469 | + } |
| 470 | + }, |
| 471 | + "outputs": [], |
| 472 | + "source": [ |
| 473 | + "t.reshape(4,-1,5).ToString()" |
405 | 474 | ] |
406 | 475 | }, |
407 | 476 | { |
|
423 | 492 | }, |
424 | 493 | "outputs": [], |
425 | 494 | "source": [ |
426 | | - "let t = torch.arange(3.0f, 5.0f, step=0.1f).reshape(2L,2L,5L);\n", |
| 495 | + "let t = torch.arange(3.0f, 5.0f, step=0.1f).reshape(2,2,5);\n", |
427 | 496 | "\n", |
428 | 497 | "// The overall shape of the tensor:\n", |
429 | 498 | "t.shape" |
|
0 commit comments