深度学习(机器学习)tensorflow学习第二课——创建tensor

创建Tensor:

1、From numpy,list CREATE tensor:

import os

os.environ
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"
import tensorflow as tf
import numpy as np

# From numpy,list CREATE tensor
a = tf.convert_to_tensor(np.zeros([2, 3]))  # 创建一个两行三列的0矩阵
# tf.Tensor(
# [[0. 0. 0.]
#  [0. 0. 0.]], shape=(2, 3), dtype=float64)
print(a)

a1 = tf.convert_to_tensor(np.ones([2, 3]))  # 创建一个两行三列的1矩阵
# tf.Tensor(
# [[1. 1. 1.]
#  [1. 1. 1.]], shape=(2, 3), dtype=float64)
print(a1)

a2 = tf.convert_to_tensor([1, 2])  # 直接传入一个list 也可以

print(a2)  # tf.Tensor([1 2], shape=(2,), dtype=int32)

a3 = tf.convert_to_tensor([1, 2.])  # 如果前面的是int,后面是float,则会依照后面的类型
print(a3)  # tf.Tensor([1. 2.], shape=(2,), dtype=float32)

a4=tf.convert_to_tensor([[1],[2]])
# tf.Tensor(
# [[1]
#  [2]], shape=(2, 1), dtype=int32)
print(a4)

2、tf.zeros 创建tensor:

"""
tf.zeros 创建tensor
zeros()就是
创建一个所有元素设置为零的张量。
这个操作返回一个类型为' dtype '的张量,其中shape为' shape '和
所有元素设置为零。
zeros()传入的都是shape

shape=()是什么意思?
简单的说举个例子,[[4,5,6],
                  [7,8,9]]
首先我们把最外面的大括号拿掉,就剩下[4,5,6],[7,8,9],就剩下了2块
在拿下一个[],就剩下了3个数字,所以shape(2,3)
"""

b = tf.zeros([])
print(b)  # tf.Tensor(0.0, shape=(), dtype=float32)

b1 = tf.zeros([1])
print(b1)  # tf.Tensor([0.], shape=(1,), dtype=float32)

b2 = tf.zeros([2, 2])
# tf.Tensor(
# [[0. 0.]
#  [0. 0.]], shape=(2, 2), dtype=float32)
print(b2)

b3 = tf.zeros([2, 3, 3])
# tf.Tensor(
# [[[0. 0. 0.]
#   [0. 0. 0.]
#   [0. 0. 0.]]
#
#  [[0. 0. 0.]
#   [0. 0. 0.]
#   [0. 0. 0.]]], shape=(2, 3, 3), dtype=float32)
print(b3)

b4 = tf.zeros_like(b3)  # 复制
# tf.Tensor(
# [[[0. 0. 0.]
#   [0. 0. 0.]
#   [0. 0. 0.]]
#
#  [[0. 0. 0.]
#   [0. 0. 0.]
#   [0. 0. 0.]]], shape=(2, 3, 3), dtype=float32)
print(b4)

3、tf.ones 创建tensor:

"""
tf.ones 创建tensor
ones()就是
创建一个所有元素设置为1的张量。
这个操作返回一个类型为' dtype '的张量,其中shape为' shape '和
所有元素设置为零。
ones()传入的也都是shape

不管是zeros()或者ones() 方法,主要用于后期算法中,初始化全为0或者初始化全为1的时候
"""
c = tf.ones(1)
print(c)#tf.Tensor([1.], shape=(1,), dtype=float32)

c1=tf.ones([1])
print(c1)#tf.Tensor([1.], shape=(1,), dtype=float32)

c2=tf.ones([2,2])
# tf.Tensor(
# [[1. 1.]
#  [1. 1.]], shape=(2, 2), dtype=float32)
# print(c2)

c3=tf.ones([2,3,3])
#tf.Tensor(
# [[[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]
#
#  [[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]], shape=(2, 3, 3), dtype=float32)
print(c3)

c4=tf.ones_like(c3)
# tf.Tensor(
# [[[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]
#
#  [[1. 1. 1.]
#   [1. 1. 1.]
#   [1. 1. 1.]]], shape=(2, 3, 3), dtype=float32)
print(c3)

4、tf.fill 创建tensor:

"""
不管是zeros()或者ones() 方法,主要用于后期算法中,初始化全为0或者初始化全为1的时候
但是如果既不想初始化全为0,又不想初始化全为1的话,我们可以用fill()方法
fill(shape,填充的数字)
"""

d=tf.fill([2,2],1)
# tf.Tensor(
# [[1 1]
#  [1 1]], shape=(2, 2), dtype=int32)
print(d)

d1=tf.fill([2,2],9)
# tf.Tensor(
# [[9 9]
#  [9 9]], shape=(2, 2), dtype=int32)
print(d1)

d2=tf.fill([2,3,3],6)
# tf.Tensor(
# [[[6 6 6]
#   [6 6 6]
#   [6 6 6]]
#
#  [[6 6 6]
#   [6 6 6]
#   [6 6 6]]], shape=(2, 3, 3), dtype=int32)
print(d2)

 

4、tf.random.normal

"""
随机化初始化,最常用的tf.random.normal
正态分布初始化

"""

f=tf.random.normal([2,2],mean=1,stddev=1) # mean=均线,stddev=方差
# tf.Tensor(
# [[ 0.8271891   0.49986267]
#  [-0.55206406 -0.30683088]], shape=(2, 2), dtype=float32)
print(f)

f1=tf.random.truncated_normal([2,2],mean=0,stddev=1) # 将正态分布的面积截取一块之后再初始化
# tf.Tensor(
# [[ 1.3095937 -0.4562364]
#  [-1.0924749 -0.5573949]], shape=(2, 2), dtype=float32)
print(f1)

 

5、tf.random.uniform

"""
均匀分布uniform:从minval=0,maxval=1,就是从0-1均匀采样,形成初始化shape
还可以选择类型
"""

g=tf.random.uniform([2,2],minval=0,maxval=1)
# tf.Tensor(
# [[0.00387156 0.95663893]
#  [0.41450417 0.864311  ]], shape=(2, 2), dtype=float32)
print(g)

g1=tf.random.uniform([2,2],minval=0,maxval=100,dtype=tf.int32)
# tf.Tensor(
# [[55 70]
#  [ 8 46]], shape=(2, 2), dtype=int32)
print(g1)

6、小小应用————打散数据

"""
小小应用————打散数据
"""

idx = tf.range(10)  # 创建0-9的数据
print(idx)  # tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

_idx = tf.random.shuffle(idx)  # 打散 数据
print(_idx)  # tf.Tensor([7 4 9 1 8 3 2 0 5 6], shape=(10,), dtype=int32)

b = tf.random.uniform([10], maxval=10, dtype=tf.int32)
print("random_uniform:", b)  # random_uniform: tf.Tensor([9 2 2 9 9 7 1 1 9 7], shape=(10,), dtype=int32)

_b = tf.gather(b, _idx)
print("对应打散的b:", _b)  # 对应b: tf.Tensor([2 9 9 9 2 1 9 1 7 7], shape=(10,), dtype=int32)

 

posted @ 2020-07-27 14:41  PY憨  阅读(401)  评论(0编辑  收藏  举报