1.tensorflow版本的变迁

2019-06-21  本文已影响0人  李涛AT北京

Tensorflow 1.X —— 主要特征

TensorFlow 1.X ———— 架构

TensorFlow 2.0 ———— 主要特性

TensorFlow 2.0 ———— 架构

TensorFlow 2.0 ————开发流程

tf 1.x版本,2.0版本,torch 对比

# 实现 1 + 1/2 + 1/2^2 + 1/2^3 + ... + 1/2^n
import warnings
warnings.filterwarnings('ignore')
import tensorflow as tf
print(tf.__version__)

# 1.X 版本实现
x = tf.Variable(0.)
y = tf.Variable(1.)
print(x)
print(y)

# x=x+y
add_op = x.assign(x+y)
# y=y/2
div_op = y.assign(y/2)

#打开回话
with tf.Session() as sess:
    # 初始化回话
    sess.run(tf.global_variables_initializer())
    for iteration in range(50):
        sess.run(add_op)
        sess.run(div_op)
    print(x.eval())

运行结果

1.12.0
<tf.Variable 'Variable_18:0' shape=() dtype=float32_ref>
<tf.Variable 'Variable_19:0' shape=() dtype=float32_ref>
2.0
# torch 实现
import torch
print(torch.__version__)

x = torch.Tensor([0.])
y = torch.Tensor([1.])
for iteration in range(50):
    x=x+y
    y=y/2
print(x)

运行结果

1.0.1
tensor([2.])
# tf 2.0
import tensorflow as tf

print(tf.__version__)
x = tf.Variable(0.)
y = tf.Variable(1.)

for iteration in range(50):
    x=x+y
    y=y/2
print(x.numpy())

运行结果

2.0.0-beta1
2.0
上一篇 下一篇

猜你喜欢

热点阅读