Skip to content

Commit b269863

Browse files
committed
修改了传入数组的类型为 floatX
1 parent 5074143 commit b269863

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

09. theano/09.02 theano basics.ipynb

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@
3131
"cell_type": "code",
3232
"execution_count": 2,
3333
"metadata": {
34-
"collapsed": true
34+
"collapsed": false
3535
},
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+
],
3745
"source": [
3846
"import theano\n",
3947
"\n",
@@ -169,7 +177,7 @@
169177
"output_type": "stream",
170178
"text": [
171179
"<class 'theano.tensor.var.TensorVariable'>\n",
172-
"TensorType(float64, scalar)\n"
180+
"TensorType(float32, scalar)\n"
173181
]
174182
}
175183
],
@@ -202,7 +210,7 @@
202210
"cell_type": "code",
203211
"execution_count": 8,
204212
"metadata": {
205-
"collapsed": true
213+
"collapsed": false
206214
},
207215
"outputs": [],
208216
"source": [
@@ -226,7 +234,7 @@
226234
{
227235
"data": {
228236
"text/plain": [
229-
"array(9.0)"
237+
"array(9.0, dtype=float32)"
230238
]
231239
},
232240
"execution_count": 9,
@@ -255,7 +263,7 @@
255263
{
256264
"data": {
257265
"text/plain": [
258-
"array(9.0)"
266+
"array(9.0, dtype=float32)"
259267
]
260268
},
261269
"execution_count": 10,
@@ -347,7 +355,7 @@
347355
"cell_type": "code",
348356
"execution_count": 14,
349357
"metadata": {
350-
"collapsed": true
358+
"collapsed": false
351359
},
352360
"outputs": [],
353361
"source": [
@@ -386,21 +394,23 @@
386394
"name": "stdout",
387395
"output_type": "stream",
388396
"text": [
389-
"[array([ 18., 37.]), array(91.0)]\n"
397+
"[array([ 18., 37.], dtype=float32), array(91.0, dtype=float32)]\n"
390398
]
391399
}
392400
],
393401
"source": [
394402
"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"
398406
]
399407
},
400408
{
401409
"cell_type": "markdown",
402410
"metadata": {},
403411
"source": [
412+
"这里 `dtype=theano.config.floatX` 是为了与 `theano` 设置的浮点数精度保持一致,默认是 `float64`,但是在 `GPU` 上一般使用 `float32` 会更高效一些。\n",
413+
"\n",
404414
"我们还可以像定义普通函数一样,给 `theano` 函数提供默认值,需要使用 `theano.Param` 类:"
405415
]
406416
},
@@ -412,7 +422,7 @@
412422
},
413423
"outputs": [],
414424
"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",
416426
" [y, z])"
417427
]
418428
},
@@ -434,14 +444,14 @@
434444
"name": "stdout",
435445
"output_type": "stream",
436446
"text": [
437-
"[array([ 14., 32.]), array(91.0)]\n"
447+
"[array([ 14., 32.], dtype=float32), array(91.0, dtype=float32)]\n"
438448
]
439449
}
440450
],
441451
"source": [
442452
"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"
445455
]
446456
},
447457
{
@@ -462,15 +472,15 @@
462472
"name": "stdout",
463473
"output_type": "stream",
464474
"text": [
465-
"[array([ 18., 37.]), array(91.0)]\n"
475+
"[array([ 18., 37.], dtype=float32), array(91.0, dtype=float32)]\n"
466476
]
467477
}
468478
],
469479
"source": [
470480
"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"
474484
]
475485
},
476486
{
@@ -498,12 +508,12 @@
498508
"name": "stdout",
499509
"output_type": "stream",
500510
"text": [
501-
"TensorType(float64, matrix)\n"
511+
"CudaNdarrayType(float32, matrix)\n"
502512
]
503513
}
504514
],
505515
"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",
507517
"\n",
508518
"print shared_var.type"
509519
]
@@ -523,7 +533,7 @@
523533
},
524534
"outputs": [],
525535
"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))"
527537
]
528538
},
529539
{
@@ -544,7 +554,7 @@
544554
"data": {
545555
"text/plain": [
546556
"array([[ 3., 4.],\n",
547-
" [ 2., 1.]])"
557+
" [ 2., 1.]], dtype=float32)"
548558
]
549559
},
550560
"execution_count": 21,
@@ -613,7 +623,7 @@
613623
}
614624
],
615625
"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",
617627
"\n",
618628
"print f()"
619629
]
@@ -629,7 +639,7 @@
629639
"cell_type": "code",
630640
"execution_count": 24,
631641
"metadata": {
632-
"collapsed": true
642+
"collapsed": false
633643
},
634644
"outputs": [],
635645
"source": [
@@ -660,8 +670,7 @@
660670
"[[ 1. 2.]\n",
661671
" [ 3. 4.]]\n",
662672
"the return value:\n",
663-
"[[ 1. 2.]\n",
664-
" [ 3. 4.]]\n",
673+
"<CudaNdarray object at 0x000000003403E430>\n",
665674
"after update:\n",
666675
"[[ 0. 1.]\n",
667676
" [ 2. 3.]]\n"
@@ -673,7 +682,7 @@
673682
"print shared_var.get_value()\n",
674683
"\n",
675684
"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",
677686
"\n",
678687
"print 'after update:'\n",
679688
"print shared_var.get_value()"
@@ -705,7 +714,7 @@
705714
{
706715
"data": {
707716
"text/plain": [
708-
"array(20.0)"
717+
"array(20.0, dtype=float32)"
709718
]
710719
},
711720
"execution_count": 26,
@@ -745,17 +754,17 @@
745754
"name": "stderr",
746755
"output_type": "stream",
747756
"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",
749758
" from scan_perform.scan_perform import *\n"
750759
]
751760
}
752761
],
753762
"source": [
754763
"y_J = theano.gradient.jacobian(y, x)\n",
755764
"\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"
759768
]
760769
},
761770
{

0 commit comments

Comments
 (0)