theano学习笔记(1)—代数

31 篇文章 15 订阅
7 篇文章 0 订阅

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.]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值