关注"AI机器学习与深度学习算法"公众号
选择"星标"公众号,原创干货,第一时间送达
2002年 Torch 框架发布,Torch 是一个基于 BSD License 的开源机器学习框架,但是由于 Torch 框架支持的是比较小众的 Lua 开发语言,因此并没有大范围的流行起来。
2016年10月,Facebook 人工智能研究院(FAIR)基于 Torch 推出了测试版本的PyTorch。它是一个基于Python的可续计算包,提供两个高级功能:
2018年12月发布了第一个正式版本 PyTorch1.0,「其中在 PyTorch0.3 和 PyTorch0.4 之间有了较大的更新,可能会有部分不兼容的情况」,也就是说如果想要在 PyTorch0.4 以后的版本中运行PyTorch0.3以前的代码需要进行少量的代码修改。
目前比较公认的前两名深度学习框架为 PyTorch 和 TensorFlow1.X(TensorFlow2.X支持动态图),这两个框架最本质的区别是动态图优先还是静态图优先。
# 创建计算图 x_ph = tf.placeholder(tf.int32, name = 'x') y_ph = tf.placeholder(tf.int32, name = 'y') z_ph = tf.multiply(x_ph, y_ph, name = 'x * y') # 运算计算图 with tf.Session() as sess: z_val = sess.run(z_ph, feed_dict = {x_ph: [8], y_ph: [9]}) print(z_val)
「对于研究人员推荐使用PyTorch,对于工程师来说推荐TensorFlow2.X。」
import torch import time print(torch.__version__) print(torch.cuda.is_available()) # print('hello, world.') a = torch.randn(10000, 1000) b = torch.randn(1000, 2000) t0 = time.time() c = torch.matmul(a, b) t1 = time.time() print(a.device, t1 - t0, c.norm(2)) device = torch.device('cuda') a = a.to(device) b = b.to(device) # 第一次在cuda上面运行的时候需要完成一些环境的初始化 t0 = time.time() c = torch.matmul(a, b) t2 = time.time() print(a.device, t2 - t0, c.norm(2)) t0 = time.time() c = torch.matmul(a, b) t2 = time.time() print(a.device, t2 - t0, c.norm(2))
,有。
import torch from torch import autograd x = torch.tensor(1.) # requires_grad = True为可以进行求导张量 # 求导的张量必须为浮点类型 a = torch.tensor(1., requires_grad = True) b = torch.tensor(2., requires_grad = True) c = torch.tensor(3., requires_grad = True) y = a**2 * x + b * x + c print('before:', a.grad, b.grad, c.grad) # before: None None None # 计算y对a, b, c的导数 grads = autograd.grad(y, [a, b, c]) print('after:', grads[0], grads[1], grads[2]) # after: tensor(2.) tensor(1.) tensor(1.)
当
时,
视频地址:
本文分享自微信公众号 - AI机器学习与深度学习算法(AI-KangChen),作者:Chenkc
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间:2020-10-10
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句