theano学习笔记(1)—代数

版权声明:本文为博主 http://blog.csdn.net/churximi 原创文章,未经允许不得转载,谢谢。 https://blog.csdn.net/churximi/article/details/51628773

theano教程:http://deeplearning.net/software/theano/tutorial/adding.html

两个标量相加

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from theano import function
import theano.tensor as T

# 第1步:定义两个变量及其类型
x = T.dscalar('x')  # 双精度浮点型的0-维数组(也就是标量)
y = T.dscalar('y')

# 第2步:构建表达式
z = x + y

# 构造函数f,输入[x, y],输出是一个0维的numpy.ndarray
f = function([x, y], z)

print f(2, 3)  # 使用函数

两个矩阵相加

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy
from theano import function
import theano.tensor as T

x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)

print f([[1, 2], [3, 4]], [[10, 20], [30, 40]])
print f(numpy.array([[1, 2], [3, 4]]), numpy.array([[10, 20], [30, 40]]))

练习

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from theano import function
import theano.tensor as T

a = T.vector()  # 向量
b = T.vector() 
out = a ** 2 + b ** 2 + 2 * a * b
f = function([a, b], out)
print f([0, 1], [1, 2])
>>>[ 1.  9.]

猜你喜欢

转载自blog.csdn.net/churximi/article/details/51628773