大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

TensorFlow2.0学习(一)

学习tensorflow搭建神经网络的第一节

1、张量Tensor:多维数组(列表) 阶:张量的维数

维数           阶           名字                例子

0_D            0          标量scalar        s = 1,2,3

1_D            1          向量vector       v = [1,2,3]

2_D            2          矩阵matrix      m = [[1,2,3],[4,5,6],[7,8,9]]

n_D            n          张量tensor      t = [[[......]]]

2、数据类型

tf.int(整型)   tf.float(浮点型)

tf.int32(32位整型)  tf.float32(32位浮点型)  tf.float64(64位浮点型)

tf.bool(布尔型,1代表True,0代表False)

tf.string(字符串类型)如:tf.constant('hello,world')

3、创建张量

3.1、利用tf.constant()创建张量;

tf.constant(张量内容,dtype = 数据类型(可选))

 

import tensorflow as tf 
a = tf.constant([1,5],dtype = tf.int64)
print(a)
print(a.dtype)
print(a.shape)

 

 

tf.Tensor([1 5], shape=(2,), dtype=int64)
<dtype: 'int64'>
(2,)

  注:在shape中(2,),有一个逗号说明是一维张量,2代表有两个元素

3.2、将numpy的数据类型转换为Tensor数据类型,

tf.convert_to_tensor(数据名,dtype = 数据类型(可选))

1 import tensorflow as tf 
2 import numpy as np
3 a = np.arange(0,5)
4 b = tf.convert_to_tensor(a,dtype = tf.int64)
5 print(a)
6 print(b)
[0 1 2 3 4]
tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)

3.3、创建特殊类型的张量

tf.zeros(维度)#创建全为0的张量

tf.ones(维度)#创建全为1的张量

tf.fill(维度,指定值)#创建指定值的张量

tf.random.normal(维度,mean = 均值,stddev=标准差)#生成正态分布的随机数,默认均值为0,标准差为1

 

tf.random.truncated_normal(维度,mean = 均值,stddev=标准差)#生成正态分布的随机数,其数值分布在均值减去二倍的标准差和均值加上二倍的标准差之间

tf.random.uniform(维度,minval=最小值,maxval=最大值)#生成在最大值和最小值之间的均匀分布

注:一维直接写个数  二维用[m,n]   多维[m,n,f.........]

import tensorflow as tf 
import numpy as np
a = tf.zeros(4)
b = tf.zeros([3,3])
c = tf.ones(5)
d = tf.ones([3,2,1])
e = tf.fill([2,3],3)
f = tf.random.normal([2,2],mean = 0, stddev = 1)
g = tf.random.truncated_normal([2,2],mean = 1, stddev = 2)
h = tf.random.uniform([3,3],maxval = 9 ,minval =1)
print(a,b,c,d,e,f,g,h)
tf.Tensor([0. 0. 0. 0.], shape=(4,), dtype=float32) tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]], shape=(3, 3), dtype=float32)
tf.Tensor([1. 1. 1. 1. 1.], shape=(5,), dtype=float32) 
tf.Tensor([[[1.]
  [1.]]

 [[1.]
  [1.]]

 [[1.]
  [1.]]], shape=(3, 2, 1), dtype=float32)
tf.Tensor(
[[3 3 3]
 [3 3 3]], shape=(2, 3), dtype=int32) 
tf.Tensor(
[[-0.5249151   0.5259417 ]
 [ 0.03927734  0.9640167 ]], shape=(2, 2), dtype=float32)
tf.Tensor([[1.2206056 2.1828222]
 [0.8024043 4.9027243]], shape=(2, 2), dtype=float32)
tf.Tensor([[7.089182  6.730689  8.621998 ]
 [2.5503626 4.885522  3.416994 ]
 [4.168269  3.0248928 6.610244 ]], shape=(3, 3), dtype=float32)

  

 

posted @ 2020-09-29 23:43  xuexii  阅读(157)  评论(0)    收藏  举报