主要介绍
1:tensorflow版本变动
2:tensorflow1.0 vs tensorflow2.0 vs pytorch
3:代码例子
代码示例如下:
# -*- coding: utf-8 -*-
# 最简单的运算比较 tensorflow 1.0 vs pytorch vs tensorflow 2.0
# tensorflow 1.0 vs pytorch vs tensorflow 2.0 后两者很像 但是tensorflow 序列部署更通用 pytorch很简单 工程适用性太窄
# 1 + 1/2 +1/2^2 + 1/2^3 +...
import tensorflow as tf
print(tf.__version__)
# 1:构造计算图
x = tf.Variable(0.)
y = tf.Variable(1.)
print(x)
print(y)
# 2:构造两个操作
# x=x+y
add_op = x.assign(x+y)
# y=y/2
div_op = y.assign(y / 2)
# 3:会话计算算子
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for iteration in range(50):
sess.run(add_op)
sess.run(div_op)
# x任然是tf.Variable 变量 故eval
print (x.eval())
# 动态图结构 无需定义会话和算子
import torch
print(torch.__version__)
x= torch.Tensor([0.])
y= torch.Tensor([1.])
for i in range(50):
x = x+y
y /= 2
print (x)
# tensorflow 2.0 因为其与1.0差别 主要是动态图结构 故只要打开eager mode即可 也与python类似
tf.enable_eager_execution()
print(tf.__version__)
# 1:构造计算图
x = tf.constant(0.)
y = tf.constant(1.)
for i in range(50):
x = x+y
y /= 2
print (x.numpy()) # 和torch不同
网友评论