tensorflow2.0-Tensor创建

33 篇文章 0 订阅
30 篇文章 0 订阅

constant

data = tf.constant([2])
data1 = tf.constant(1)
data2 = tf.constant([1,2])
data3 = tf.constant([True,False])
data4 = tf.constant(["T","H","F"])
print (data)
print (data1)
print (data2)
print (data3)
print (data4)
"""
result:
	tf.Tensor([2], shape=(1,), dtype=int32)
	tf.Tensor(1, shape=(), dtype=int32)
	tf.Tensor([1 2], shape=(2,), dtype=int32)
	tf.Tensor([ True False], shape=(2,), dtype=bool)
	tf.Tensor([b'T' b'H' b'F'], shape=(3,), dtype=string)
"""

说明:创建tensor

range

#和range()用法类似
#tf.range(start=10,limit=120,delta=2)
l = tf.range(10)
l1 = tf.range(10,20)
l2 = tf.range(10,120,2)
print (l)
print (l1)
print (l2)
"""
result:
	tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
	tf.Tensor([10 11 12 13 14 15 16 17 18 19], shape=(10,), dtype=int32)
	tf.Tensor(
	[ 10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44
	  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80
	  82  84  86  88  90  92  94  96  98 100 102 104 106 108 110 112 114 116
	 118], shape=(55,), dtype=int32)
 """

说明:创建tensor列表形式

convert_to_tensor

d = np.arange(10)
data = tf.convert_to_tensor(d)
print (d,"->",data)

d1 = np.ones([2,3])
data1 = tf.convert_to_tensor(d1)
print (d1,"->",data1)

d2 = np.zeros([3,1])
data2 = tf.convert_to_tensor(d2)
print (d2,"->",data2)

d3 = np.zeros([3,1,4])
data3 = tf.convert_to_tensor(d3)
print (d3,"->",data3)

d4 =[[2],[1.]]
data4 = tf.convert_to_tensor(d4)
print (d4,"->",data4)
 """
 result:
	[0 1 2 3 4 5 6 7 8 9] -> tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
	[[1. 1. 1.]
	 [1. 1. 1.]] -> tf.Tensor(
	[[1. 1. 1.]
	 [1. 1. 1.]], shape=(2, 3), dtype=float64)
	[[0.]
	 [0.]
	 [0.]] -> tf.Tensor(
	[[0.]
	 [0.]
	 [0.]], shape=(3, 1), dtype=float64)
	[[[0. 0. 0. 0.]]
	
	 [[0. 0. 0. 0.]]
	
	 [[0. 0. 0. 0.]]] -> tf.Tensor(
	[[[0. 0. 0. 0.]]
	
	 [[0. 0. 0. 0.]]
	
	 [[0. 0. 0. 0.]]], shape=(3, 1, 4), dtype=float64)
	[[2], [1.0]] -> tf.Tensor(
	[[2.]
	 [1.]], shape=(2, 1), dtype=float32)
 """

说明:将数据转化为tensor

特殊函数zeros,ones,fill,zeros_like

data = tf.zeros([])
print (data)

data1= tf.zeros([3,2])
print (data1)

data2 = tf.zeros([2])
print (data2)

data3 = tf.zeros_like(data2)
print (data3)

data4 = tf.ones([1,2,2])
print (data4)

data5 = tf.fill([2,2],6)
print (data5)
 """
 result:
	tf.Tensor(0.0, shape=(), dtype=float32)
	tf.Tensor(
	[[0. 0.]
	 [0. 0.]
	 [0. 0.]], shape=(3, 2), dtype=float32)
	tf.Tensor([0. 0.], shape=(2,), dtype=float32)
	tf.Tensor([0. 0.], shape=(2,), dtype=float32)
	tf.Tensor(
	[[[1. 1.]
	  [1. 1.]]], shape=(1, 2, 2), dtype=float32)
	tf.Tensor(
	[[6 6]
	 [6 6]], shape=(2, 2), dtype=int32)
 """

random

r = tf.random.normal([2,2],mean = 1,stddev = 1)
print (r)

r1 = tf.random.normal([2,2])
print (r1)

r2 = tf.random.truncated_normal([2,2],mean = 0,stddev = 1)
print (r2)

r3 = tf.random.normal([12,322])
print (r3)


r4 = tf.random.uniform([2,2],minval=0,maxval=100)
print (r4)

r5 = tf.random.uniform([10],maxval=100,dtype =tf.int32)
print (r5)

 """
  result:
	tf.Tensor(
	[[1.4705815  1.9700427 ]
	 [0.8980557  0.59356326]], shape=(2, 2), dtype=float32)
	tf.Tensor(
	[[ 0.7434185  -0.2920409 ]
	 [-0.8815004  -0.15134895]], shape=(2, 2), dtype=float32)
	tf.Tensor(
	[[-0.17634201 -0.09042784]
	 [ 1.313838    0.3804888 ]], shape=(2, 2), dtype=float32)
	tf.Tensor(
	[[ 1.2842529  -0.80094755 -0.42826483 ...  1.9505789   0.5388328
	   0.40579897]
	 [-0.8771489  -0.18126725 -1.6080959  ...  0.14207745  0.17735407
	   0.6299817 ]
	 [-0.1086649   0.20002969 -0.8741922  ...  0.888306    0.32527128
	   1.3414456 ]
	 ...
	 [-0.37660804 -0.35642526  0.35903797 ... -1.6636043   1.0195013
	  -0.12622574]
	 [-0.5705916   0.9588496  -0.74653375 ...  0.09025645 -0.9704016
	  -0.00901179]
	 [ 1.1720883  -1.3659464   0.63668466 ...  0.2542699   0.6924406
	  -2.2573204 ]], shape=(12, 322), dtype=float32)
	tf.Tensor(
	[[49.862324 72.64964 ]
	 [97.64084  83.83745 ]], shape=(2, 2), dtype=float32)
	tf.Tensor([45 70 24 84 46 64 25  4 67 97], shape=(10,), dtype=int32)
	 """

说明:随机tensor数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值