Tensorflow2.0的简介

Tensorflow2.0的简介

Tensorflow1.X版本有哪些问题?

  • 调试困难
  • API混乱不堪
  • 导致从入门到放弃

为什么使用Tensorflow,而不是Pytroch呢?

  • GPU加速
  • 自动求导
  • 神经网络Layers

最大改变就是移除tf.contrib,分别用tf.keras tf.losses tf.metrices等开代替

使用免费薅羊毛的Google colab做简单说明:

在这里插入图片描述

import tensorflow as tf
import timeit

# 定义两个矩阵,并求乘机,使用GPU加速计算
with tf.device('/gpu:0'):
  mat_a = tf.random.normal([1000,800])
  mat_b = tf.random.normal([800,600])

def run():
  with tf.device('/gpu:0'):
    result = tf.matmul(mat_a, mat_b)
  return result

# 计算10次的时间
run_time = timeit.timeit(run, number=10)
print('run_time: ', run_time)
run_time:  0.0023373889998765662     #速度飞快,远高于CPU
import tensorflow as tf
import os

# 设置显示框显示信息等级,更清爽
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

x = tf.constant(1.)
a = tf.constant(2.)
b = tf.constant(3.)

# 新增tf.GradientTape 用于自动微分运算
with tf.GradientTape() as tape:
  tape.watch([a, b])
  y = a**2 * x + b * x

# 分别计算y对a和b的求导值
[da, db] = tape.gradient(y, [a, b])
print(da, db)
tf.Tensor(4.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)

e=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值