😘
tensorflow 2.0实战笔记
  • Tensorflow 2.0 实战笔记
  • 第一章 Tensorflow 2.0入门
    • 1.1 Tensorflow简介
    • 1.2 Tensorflow安装
      • 1.2.1 Windows下安装
      • 1.2.2 Ubuntu下安装
      • 1.2.3 环境测试
    • 1.3 Tensorflow1.x 和2.x接口区别
  • 第二章 Tensorflow基础篇 I
    • 2.1 张量与操作
    • 2.2 三种定义模型方式
    • 2.3 两种模型训练方式
    • 2.4 计算图机制
    • 2.5 模型保存与加载
    • 注意
    • 相关bug详解
  • 第三章 Tensorflow基础篇 II
    • 3.1 自定义模型层
    • 3.2 损失函数及自定义损失函数
    • 3.3 优化器及自定义优化器
    • 3.4 评估函数及自定义评估函数
    • 3.5 激活函数及自定义激活函数
    • 3.6 Tensorboard使用
    • 注意
  • 第四章 Tensorflow数据管道
    • 4.1 tf.data简介
    • 4.2 Dataset使用
    • 4.3 TFrecord使用
    • 注意
  • 第五章 卷积神经网络
    • 5.1 浅谈卷积神经网络
    • 5.2 拆解卷积层
    • 5.3 拆解池化层
    • 5.4 实战三:Quick, Draw! Google涂鸦识别挑战项目
    • 注意
  • 第六章 循环神经网络
    • 6.1-浅谈循环神经网络
    • 6.2-word2vec简介及词向量构建
    • 6.3-实战四:LSTM实现新闻分类算法
  • 第七章 Transorformer网络
    • 7.1-Transfromer原理详解
    • 7.2-实战五:Transformer实现英译中机器翻译
  • 第八章 tf.hub初探
  • 第九章 Tensorflo7部署
  • 第九章 相许Tensorflow
由 GitBook 提供支持
在本页
  • 张量
  • array(numpy)和Tensor(Tensorflow)对比
  • 操作

这有帮助吗?

  1. 第二章 Tensorflow基础篇 I

2.1 张量与操作

上一页第二章 Tensorflow基础篇 I下一页2.2 三种定义模型方式

最后更新于4年前

这有帮助吗?

张量

TensorFlow使用一种叫张量(tensor)的数据结构去定义所有的数据,我们可以把 tensor 看成是 n 维的 array 或者 list。在 TensorFlow 的各部分图形间流动传递的只能是tensor。

编写TensorFlow程序时,操纵并传递的主要对象是tf.Tensor:

  • 一个数据类型(例如 float32,int32,或string)

  • 以及shape

array(numpy)和Tensor(Tensorflow)对比

Numpy

TensorFlow

a = np.zeros((2,2)); b = np.ones((2,2))

a = tf.zeros((2,2)); b = tf.ones((2,2))

np.sum(b,axis=1)

tf.reduce_sum(a,axis=1)

a.shape

a.get_shape()

np.reshape(a,(1,4))

tf.reshape(a,(1,4))

b*5+1

b*5+1

np.dot(a,b)

tf.matmul(a,b)

a[0,0]; a[:,0]; a[0,:]

a[0,0]; a[:,0]; a[0,:]

操作

  • tf.strings (常用于推荐算法场景、**NLP场景**)

  • tf.debugging

  • tf.dtypes

  • tf.math

  • tf.random

  • (常用于结构化数据特征处理)

tf.strings

#字符切割
tf.strings.bytes_split('hello')
#单词切割
tf.strings.split('hello world')
#string hash
tf.strings.to_hash_bucket(['hello','world'], num_buckets=10)

tf.debugging

#tf自带debug函数
a=tf.random.uniform((10,10))
tf.debugging.assert_equal(x=a.shape,y=(10,10))
#错误示范
tf.debugging.assert_equal(x=a.shape,y=(20,10))

tf.random

a = tf.random.uniform(shape=(10,5),minval=0,maxval=10)

tf.math

a = tf.constant([[1,2],[3,4]])
b = tf.constant([[5,6],[7,8]])

tf.print(tf.math.add(a,b))
tf.print(tf.math.subtract(a,b))
tf.print(tf.math.multiply(a,b))
tf.print(tf.math.divide(a,b))

tf.dtypes

x =tf.constant([1.8,2.2],dtype=tf.float32)

x1=tf.dtypes.cast(x,tf.int32)
tf.feature_column