赞
踩
a = tf.constant(1)
print("a:",a)
b = tf.constant(2.)
print("b:",b)
try:
c = tf.constant(2.5,dtype = tf.int32)
except Exception as error:
print(error)
d = tf.constant([True,False])
print("d:",d)
e = tf.constant("Welcome to TensorFlow!")
print("e:",e)
with tf.device("cpu"):
a = tf.constant(5)
with tf.device("gpu"):
b = tf.constant(6)
print("a",a.device)
print("b",b.device)
aa = a.gpu()
print("aa:",aa.device)
bb = b.cpu()
print("bb:",bb.device)
"""
GPU上的tensor只能在GPU上操作
CPU上的Tenor只能在CPU上操作
"""
try:
print(bb+aa)
except Exception as error:
print(error)
print(aa.numpy(),type(aa.numpy()))
print(aa.shape)
"""
标量的形状为0
"""
t1 = tf.ones([5,6])
print("t1:",t1.shape)
print("t1:",t1.ndim)
print("t1:",tf.rank(t1)) # 同时返回shape和ndim
print("t1:",tf.is_tensor(t1))
print("t1:",isinstance(t1,tf.Tensor))
print("t1:",t1.dtype)
print("t1:",t1.dtype == tf.int32)
a = np.arange(10)
print("a:",a.dtype,a)
b = tf.convert_to_tensor(a)
print("b:",b.dtype,b)
c = tf.convert_to_tensor(a,dtype = tf.int16)
print("c:",c.dtype,c)
t1 = tf.constant(5)
print("t1:",t1)
t11 = tf.cast(t1,tf.float32)
print("t11:",t11)
t2 = tf.constant([0,1])
print("t2:",t2)
t21 = tf.cast(t2,tf.bool)
print("t21:",t21)
t22 = tf.cast(t21,tf.int32)
print("t22:",t22)
t3 = tf.range(5)
print("t3:",t3)
t31 = tf.Variable(t3)
print("t31:",t31)
print("t31:",t31.name)
print("t31:",t31.trainable) # 判断是否为变量,即是否可以求导
print('t31:',tf.is_tensor(t31))
print("t31:",isinstance(t31,tf.Tensor))
"""
所以用is_tensor方法更好更为准确
"""
t4 = tf.range(1,100,5)
print("t4:",t4)
t41 = t4.numpy()
print("t41:",t41.dtype,t41)
本文为参考龙龙老师的“深度学习与TensorFlow 2入门实战“课程书写的学习笔记
by CyrusMay 2022 04 06
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。