美文网首页
tensorflow2数据和变量(一)

tensorflow2数据和变量(一)

作者: 小小树苗儿 | 来源:发表于2020-07-13 16:16 被阅读0次

数据类型

  1. 数据载体
  • list 支持不同的数据类型,效率低
  • np.array 相同类型的载体,效率高;但是不支持GPC,不支持自动求导
  • tf.Tensor TensorFlow中存储大量连续数据的载体
  1. 基本数据类型
    tf.int32 tf.constant(1)
    tf.float32 tf.constant(1.)
    tf.float64 tf.constant(1., dtype=tf.double)
    tf.bool tf.constant(True)
    tf.string tf.constant('hello')

  2. 基本属性

import tensorflow as tf
a = tf.range(4)  # <tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3], dtype=int32)>
a.numpy()  # array([0, 1, 2, 3], dtype=int32)
a.ndim # 1  (0的话就是标量)
tf.rank(a)  # <tf.Tensor: shape=(), dtype=int32, numpy=1>
a.shape  # TensorShape([4])
tf.is_tensor(a)  # True

tf.rank和a.ndim的区别在于返回的类型不同

HelloWord

import tensorflow as tf

hello = tf.constant("hello tensorflow!")
print(hello.numpy())   # b'hello tensorflow!'

alist = tf.constant([1,2,3])
alist.numpy()  # array([1, 2, 3], dtype=int32)

创建变量

为了区分需要计算梯度信息的张量与不需要计算梯度信息的张量, TensorFlow 增加了
一种专门的数据类型来支持梯度信息的记录: tf.Variable。 tf.Variable 类型在普通的张量类
型基础上添加了 name, trainable 等属性来支持计算图的构建。

# tf.random.normal 方法返回形状为(1,4)的张量。它的4个元素符合均值为100、标准差为0.35的正态分布。
W = tf.Variable(initial_value=tf.random.normal(shape=(1, 4), mean=100, stddev=0.35), name="W")
b = tf.Variable(tf.zeros([4]), name="b")
c = tf.Variable(tf.zeros([4]))

(<tf.Variable 'W:0' shape=(1, 4) dtype=float32, numpy=array([[100.11003 , 100.05794 , 100.005936, 100.06278 ]], dtype=float32)>,
 <tf.Variable 'b:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>,
 <tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>,
 True)

变量赋值

b.assign_add([1, 1, 1, 1])  # <tf.Variable 'UnreadVariable' shape=(4,) dtype=float32, numpy=array([1., 1., 1., 1.], dtype=float32)>
b.assign([3,4,5,6])  # <tf.Variable 'UnreadVariable' shape=(4,) dtype=float32, numpy=array([3., 4., 5., 6.], dtype=float32)>

tensor

0阶张量

mammal = tf.Variable("Elephant", tf.string)
ignition = tf.Variable(451, tf.int16)
floating = tf.Variable(3.14159265359, tf.float64)
its_complicated = tf.Variable(12.3 - 4.85j, tf.complex64)

1阶张量

mystr = tf.Variable(["Hello", "World"], tf.string)
cool_numbers  = tf.Variable([3.14159, 2.71828], tf.float32)
first_primes = tf.Variable([2, 3, 5, 7, 11], tf.int32)
its_very_complicated = tf.Variable([12.3 - 4.85j, 7.5 - 6.23j], tf.complex64)

2阶张量

mymat = tf.Variable([[7],[11]], tf.int16)
myxor = tf.Variable([[False, True],[True, False]], tf.bool)
linear_squares = tf.Variable([[4], [9], [16], [25]], tf.int32)
squarish_squares = tf.Variable([ [4, 9], [16, 25] ], tf.int32)
rank_of_squares = tf.rank(squarish_squares)
mymatC = tf.Variable([[7],[11]], tf.int32)

4阶张量

my_image = tf.zeros([10, 299, 299, 3])  # batch x height x width x color
my_image.shape, tf.rank(my_image)
# (TensorShape([10, 299, 299, 3]), <tf.Tensor: shape=(), dtype=int32, numpy=4>)

常用函数

tf.convert_to_tensor(data)
tf.zeros(shape)
tf.ones(1) 生成一个一维tensor,包含一个1
tf.ones([]) 生成一个标量1
tf.ones([2]) 生成一个一维tensor,包含两个1
tf.ones_like(a) 相当于tf.ones(a.shape)
tf.fill([3,4], 9) 全部填充9
tf.random.normal([3,4], mean=1, stddev=1)
tf.random.truncated_normal([3,4], mean=0, stddev=1) 带截断的正态分布,(大于某个值重新采样),比如在经过sigmoid激活后,如果用不带截断的,容易出现梯度消失问题。
tf.random.uniform([3,4], minval=0, maxval=100, dtype=tf.int32) 平均分布

类型转换

  1. 字符串编码转换
docs = tf.constant([u'Everything not saved will be lost.'.encode('UTF-16-BE'), u'Sad☹'.encode('UTF-16-BE')])
utf8_docs = tf.strings.unicode_transcode(docs, input_encoding='UTF-16-BE', output_encoding='UTF-8')
print(utf8_docs.numpy())
# [b'Everything not saved will be lost.' b'Sad\xe2\x98\xb9']
  1. numpy转换tensor
import numpy as np
a=np.arange(5)
a.dtype # dtype('int64')
aa=tf.convert_to_tensor(a) # <tf.Tensor: id=466678, shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])>

  1. 类型转换
aa = tf.convert_to_tensor(a, dtype=tf.int32)
x = tf.cast(aa, tf.float32)  # <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>

简单求导

# 求w*w 在w=1.0处的导数
w = tf.Variable([[1.0]])
with tf.GradientTape() as t:
    loss = w * w
dw = t.gradient(loss, w)
dw.numpy()
# array([[2.]], dtype=float32)

额外一例子

x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as t:
    t.watch(x)       # Ensures that `tensor` is being traced by this tape.
    y = x * x
    z = y * y
dz_dx = t.gradient(z, x)  # 108.0 (4*x^3 at x = 3)
dy_dx = t.gradient(y, x)  # 6.0
print("dz/dx=", dz_dx.numpy())
print("dy/dx=", dy_dx.numpy())
del t  # Drop the reference to the tape

相关文章

  • tensorflow2数据和变量(一)

    数据类型 数据载体 list 支持不同的数据类型,效率低 np.array 相同类型的载体,效率高;但是不支持GP...

  • [Tensorflow2] 数据加载

    针对小型常用数据集,tensorflow2中加载数据通常有两种方法:1、使用keras.datasets 有几种数...

  • TensorFlow操作进阶

    在本章脑图中主要是介绍了TensorFlow2的进阶操作,包含: 合并和分割 数据统计,聚合函数 数据限幅实现 填...

  • Kotlin 数据之本变量

    变量和常量用来存储数据和传递数据,类型则用来描述变量和常量保存的是什么样的数据类型。 数据类型 变量和常量都有数据...

  • Java 变量和基本数据类型

    变量和基本数据类型 变量 ​ 保存数据的存储单位(容器) ​ 声明变量的语法: 类型 变量名称 基本数据类...

  • 第002篇:变量、运算符、位运算

    1、变量 1.1、什么是变量和变量的声明?  - 变量是用来申请空间保存数据的(变量就是用来存数据的)  1.1....

  • C语言基础

    C 变量: 变量的数据类型: 变量按照数据的分类分为整数和浮点数 不同变量的数据类型是不同的 数据类型有整型,浮点...

  • python 变量进阶(理解)

    变量进阶(理解) 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是...

  • 16 变量进阶(理解)

    变量进阶(理解) 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是...

  • [Python基础]D9 变量的进阶

    变量进阶(理解) 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是...

网友评论

      本文标题:tensorflow2数据和变量(一)

      本文链接:https://www.haomeiwen.com/subject/bspacktx.html