tensorflow2.0——创建张量2

 

import tensorflow as tf

############################创建全0全1张量#########################
a = tf.ones(shape = (3,5))
print('a:',a)
b = tf.ones([6])
print('b:',b)
c = tf.zeros([2,3],dtype = tf.int32)
print('c:',c)
print()
############################创建元素值都相同的张量#########################
a = tf.fill([3,4],3.9)
print('全相同张量fill():',a)
print()
a = tf.constant(value = 4.8,shape = [3,2])
print('全相同张量constant():',a)
print()
############################创建元素满足正态分布的张量#########################
a = tf.random.normal(shape = [3,4],mean = 0,stddev = 1,dtype = tf.float32)
print('正态分布为:',a)
print()
############################创建元素满足截断正态分布的张量#########################
a = tf.random.truncated_normal(shape = [3,4],mean = 0,stddev = 1,dtype = tf.float32)
print('截断正态分布为:',a)
print()
############################创建元素满足均匀分布的张量#########################
a = tf.random.uniform(shape = [4,5],minval = 0,maxval = 5,dtype = tf.int32)
print('均匀分布为:',a)
print()
############################随机打乱shuffle函数#########################
a = tf.constant([[1,2],[3,4],[5,6],[7,8]])                      #   只沿着第一维打乱
a = tf.random.shuffle(a)
print("打乱后的a:",a)
############################创建序列tf.range()函数#########################
a = tf.range(start = 0,limit = 10,delta = 2,dtype = tf.int32)
print('创建的序列为:',a)
############################查看张量的属性 ndim,shaoe,dtype#########################
a = tf.constant([[1,2],[3,4],[5,6],[7,8]])
print('维度ndim:',a.ndim)
print('数据类型dtype:',a.dtype)
print('shape:',a.shape)
print('tf.shape(a):',tf.shape(a))
print('元素总数tf.size(a):',tf.size(a))
print('张量维度tf.rank(a):',tf.rank(a))
print()

猜你喜欢

转载自www.cnblogs.com/cxhzy/p/13385166.html