|
31 | 31 | "cell_type": "code", |
32 | 32 | "execution_count": 2, |
33 | 33 | "metadata": { |
34 | | - "collapsed": true |
| 34 | + "collapsed": false |
35 | 35 | }, |
36 | | - "outputs": [], |
| 36 | + "outputs": [ |
| 37 | + { |
| 38 | + "name": "stderr", |
| 39 | + "output_type": "stream", |
| 40 | + "text": [ |
| 41 | + "Using gpu device 0: GeForce GTX 850M\n" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
37 | 45 | "source": [ |
38 | 46 | "import theano\n", |
39 | 47 | "\n", |
|
169 | 177 | "output_type": "stream", |
170 | 178 | "text": [ |
171 | 179 | "<class 'theano.tensor.var.TensorVariable'>\n", |
172 | | - "TensorType(float64, scalar)\n" |
| 180 | + "TensorType(float32, scalar)\n" |
173 | 181 | ] |
174 | 182 | } |
175 | 183 | ], |
|
202 | 210 | "cell_type": "code", |
203 | 211 | "execution_count": 8, |
204 | 212 | "metadata": { |
205 | | - "collapsed": true |
| 213 | + "collapsed": false |
206 | 214 | }, |
207 | 215 | "outputs": [], |
208 | 216 | "source": [ |
|
226 | 234 | { |
227 | 235 | "data": { |
228 | 236 | "text/plain": [ |
229 | | - "array(9.0)" |
| 237 | + "array(9.0, dtype=float32)" |
230 | 238 | ] |
231 | 239 | }, |
232 | 240 | "execution_count": 9, |
|
255 | 263 | { |
256 | 264 | "data": { |
257 | 265 | "text/plain": [ |
258 | | - "array(9.0)" |
| 266 | + "array(9.0, dtype=float32)" |
259 | 267 | ] |
260 | 268 | }, |
261 | 269 | "execution_count": 10, |
|
347 | 355 | "cell_type": "code", |
348 | 356 | "execution_count": 14, |
349 | 357 | "metadata": { |
350 | | - "collapsed": true |
| 358 | + "collapsed": false |
351 | 359 | }, |
352 | 360 | "outputs": [], |
353 | 361 | "source": [ |
|
386 | 394 | "name": "stdout", |
387 | 395 | "output_type": "stream", |
388 | 396 | "text": [ |
389 | | - "[array([ 18., 37.]), array(91.0)]\n" |
| 397 | + "[array([ 18., 37.], dtype=float32), array(91.0, dtype=float32)]\n" |
390 | 398 | ] |
391 | 399 | } |
392 | 400 | ], |
393 | 401 | "source": [ |
394 | 402 | "print linear_mix(np.array([[1, 2, 3],\n", |
395 | | - " [4, 5, 6]]), #A\n", |
396 | | - " np.array([1, 2, 3]), #x\n", |
397 | | - " np.array([4, 5])) #b" |
| 403 | + " [4, 5, 6]], dtype=theano.config.floatX), #A\n", |
| 404 | + " np.array([1, 2, 3], dtype=theano.config.floatX), #x\n", |
| 405 | + " np.array([4, 5], dtype=theano.config.floatX)) #b" |
398 | 406 | ] |
399 | 407 | }, |
400 | 408 | { |
401 | 409 | "cell_type": "markdown", |
402 | 410 | "metadata": {}, |
403 | 411 | "source": [ |
| 412 | + "这里 `dtype=theano.config.floatX` 是为了与 `theano` 设置的浮点数精度保持一致,默认是 `float64`,但是在 `GPU` 上一般使用 `float32` 会更高效一些。\n", |
| 413 | + "\n", |
404 | 414 | "我们还可以像定义普通函数一样,给 `theano` 函数提供默认值,需要使用 `theano.Param` 类:" |
405 | 415 | ] |
406 | 416 | }, |
|
412 | 422 | }, |
413 | 423 | "outputs": [], |
414 | 424 | "source": [ |
415 | | - "linear_mix_default = theano.function([A, x, theano.Param(b, default=np.zeros(2))],\n", |
| 425 | + "linear_mix_default = theano.function([A, x, theano.Param(b, default=np.zeros(2, dtype=theano.config.floatX))],\n", |
416 | 426 | " [y, z])" |
417 | 427 | ] |
418 | 428 | }, |
|
434 | 444 | "name": "stdout", |
435 | 445 | "output_type": "stream", |
436 | 446 | "text": [ |
437 | | - "[array([ 14., 32.]), array(91.0)]\n" |
| 447 | + "[array([ 14., 32.], dtype=float32), array(91.0, dtype=float32)]\n" |
438 | 448 | ] |
439 | 449 | } |
440 | 450 | ], |
441 | 451 | "source": [ |
442 | 452 | "print linear_mix_default(np.array([[1, 2, 3],\n", |
443 | | - " [4, 5, 6]]), #A\n", |
444 | | - " np.array([1, 2, 3])) #x" |
| 453 | + " [4, 5, 6]], dtype=theano.config.floatX), #A\n", |
| 454 | + " np.array([1, 2, 3], dtype=theano.config.floatX)) #x" |
445 | 455 | ] |
446 | 456 | }, |
447 | 457 | { |
|
462 | 472 | "name": "stdout", |
463 | 473 | "output_type": "stream", |
464 | 474 | "text": [ |
465 | | - "[array([ 18., 37.]), array(91.0)]\n" |
| 475 | + "[array([ 18., 37.], dtype=float32), array(91.0, dtype=float32)]\n" |
466 | 476 | ] |
467 | 477 | } |
468 | 478 | ], |
469 | 479 | "source": [ |
470 | 480 | "print linear_mix_default(np.array([[1, 2, 3],\n", |
471 | | - " [4, 5, 6]]), #A\n", |
472 | | - " np.array([1, 2, 3]), #x\n", |
473 | | - " np.array([4, 5])) #b" |
| 481 | + " [4, 5, 6]], dtype=theano.config.floatX), #A\n", |
| 482 | + " np.array([1, 2, 3], dtype=theano.config.floatX), #x\n", |
| 483 | + " np.array([4, 5], dtype=theano.config.floatX)) #b" |
474 | 484 | ] |
475 | 485 | }, |
476 | 486 | { |
|
498 | 508 | "name": "stdout", |
499 | 509 | "output_type": "stream", |
500 | 510 | "text": [ |
501 | | - "TensorType(float64, matrix)\n" |
| 511 | + "CudaNdarrayType(float32, matrix)\n" |
502 | 512 | ] |
503 | 513 | } |
504 | 514 | ], |
505 | 515 | "source": [ |
506 | | - "shared_var = theano.shared(np.array([[1.0, 2.0], [3.0, 4.0]]))\n", |
| 516 | + "shared_var = theano.shared(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=theano.config.floatX))\n", |
507 | 517 | "\n", |
508 | 518 | "print shared_var.type" |
509 | 519 | ] |
|
523 | 533 | }, |
524 | 534 | "outputs": [], |
525 | 535 | "source": [ |
526 | | - "shared_var.set_value(np.array([[3.0, 4], [2, 1]]))" |
| 536 | + "shared_var.set_value(np.array([[3.0, 4], [2, 1]], dtype=theano.config.floatX))" |
527 | 537 | ] |
528 | 538 | }, |
529 | 539 | { |
|
544 | 554 | "data": { |
545 | 555 | "text/plain": [ |
546 | 556 | "array([[ 3., 4.],\n", |
547 | | - " [ 2., 1.]])" |
| 557 | + " [ 2., 1.]], dtype=float32)" |
548 | 558 | ] |
549 | 559 | }, |
550 | 560 | "execution_count": 21, |
|
613 | 623 | } |
614 | 624 | ], |
615 | 625 | "source": [ |
616 | | - "shared_var.set_value(np.array([[1.0, 2], [3, 4]]))\n", |
| 626 | + "shared_var.set_value(np.array([[1.0, 2], [3, 4]], dtype=theano.config.floatX))\n", |
617 | 627 | "\n", |
618 | 628 | "print f()" |
619 | 629 | ] |
|
629 | 639 | "cell_type": "code", |
630 | 640 | "execution_count": 24, |
631 | 641 | "metadata": { |
632 | | - "collapsed": true |
| 642 | + "collapsed": false |
633 | 643 | }, |
634 | 644 | "outputs": [], |
635 | 645 | "source": [ |
|
660 | 670 | "[[ 1. 2.]\n", |
661 | 671 | " [ 3. 4.]]\n", |
662 | 672 | "the return value:\n", |
663 | | - "[[ 1. 2.]\n", |
664 | | - " [ 3. 4.]]\n", |
| 673 | + "<CudaNdarray object at 0x000000003403E430>\n", |
665 | 674 | "after update:\n", |
666 | 675 | "[[ 0. 1.]\n", |
667 | 676 | " [ 2. 3.]]\n" |
|
673 | 682 | "print shared_var.get_value()\n", |
674 | 683 | "\n", |
675 | 684 | "print 'the return value:'\n", |
676 | | - "print f_update(np.array([[1.0, 1], [1, 1]]))\n", |
| 685 | + "print f_update(np.array([[1.0, 1], [1, 1]], dtype=theano.config.floatX))\n", |
677 | 686 | "\n", |
678 | 687 | "print 'after update:'\n", |
679 | 688 | "print shared_var.get_value()" |
|
705 | 714 | { |
706 | 715 | "data": { |
707 | 716 | "text/plain": [ |
708 | | - "array(20.0)" |
| 717 | + "array(20.0, dtype=float32)" |
709 | 718 | ] |
710 | 719 | }, |
711 | 720 | "execution_count": 26, |
|
745 | 754 | "name": "stderr", |
746 | 755 | "output_type": "stream", |
747 | 756 | "text": [ |
748 | | - "C:\\Anaconda\\lib\\site-packages\\theano\\scan_module\\scan_perform_ext.py:133: RuntimeWarning: numpy.ndarray size changed, may indicate binary incompatibility\n", |
| 757 | + "C:\\Miniconda\\lib\\site-packages\\theano\\scan_module\\scan_perform_ext.py:133: RuntimeWarning: numpy.ndarray size changed, may indicate binary incompatibility\n", |
749 | 758 | " from scan_perform.scan_perform import *\n" |
750 | 759 | ] |
751 | 760 | } |
752 | 761 | ], |
753 | 762 | "source": [ |
754 | 763 | "y_J = theano.gradient.jacobian(y, x)\n", |
755 | 764 | "\n", |
756 | | - "print y_J.eval({A: np.array([[9.0, 8, 7], [4, 5, 6]]), #A\n", |
757 | | - " x: np.array([1.0, 2, 3]), #x\n", |
758 | | - " b: np.array([4.0, 5])}) #b" |
| 765 | + "print y_J.eval({A: np.array([[9.0, 8, 7], [4, 5, 6]], dtype=theano.config.floatX), #A\n", |
| 766 | + " x: np.array([1.0, 2, 3], dtype=theano.config.floatX), #x\n", |
| 767 | + " b: np.array([4.0, 5], dtype=theano.config.floatX)}) #b" |
759 | 768 | ] |
760 | 769 | }, |
761 | 770 | { |
|
0 commit comments