import tensorflow as tf
a = tf. constant( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] )
b = tf. constant( [ [ 10 , 20 , 30 , 40 ] , [ 50 , 60 , 70 , 80 ] ] )
tf_add = tf. add( a, b)
print ( '加法:' , tf_add)
tf_sub = tf. subtract( a, b)
print ( '减法:' , tf_sub)
tf_mul = tf. multiply( a, b)
print ( '乘法:' , tf_mul)
tf_div = tf. divide( b, a)
print ( '除法:' , tf_div)
>> > 加法: tf. Tensor(
[ [ 11 22 33 44 ]
[ 55 66 77 88 ] ] , shape= ( 2 , 4 ) , dtype= int32)
>> > 减法: tf. Tensor(
[ [ - 9 - 18 - 27 - 36 ]
[ - 45 - 54 - 63 - 72 ] ] , shape= ( 2 , 4 ) , dtype= int32)
>> > 乘法: tf. Tensor(
[ [ 10 40 90 160 ]
[ 250 360 490 640 ] ] , shape= ( 2 , 4 ) , dtype= int32)
>> > 除法: tf. Tensor(
[ [ 10. 10. 10. 10. ]
[ 10. 10. 10. 10. ] ] , shape= ( 2 , 4 ) , dtype= float64)
import tensorflow as tf
a = tf. constant( [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] )
b = tf. constant( [ [ 10 , 20 , ] , [ 50 , 60 , 70 , 80 ] ] )
tf_add = tf. add( a, b)
print ( '加法:' , tf_add)
Traceback ( most recent call last) :
File "D:\pycharm\pythonProject2\tensor\a.py" , line 5 , in < module>
b = tf. constant( [ [ 10 , 20 , ] , [ 50 , 60 , 70 , 80 ] ] )
File "D:\anaconda\envs\tf2x\lib\site-packages\tensorflow\python\framework\constant_op.py" , line 261 , in constant
return _constant_impl( value, dtype, shape, name, verify_shape= False ,
File "D:\anaconda\envs\tf2x\lib\site-packages\tensorflow\python\framework\constant_op.py" , line 270 , in _constant_impl
t = convert_to_eager_tensor( value, ctx, dtype)
File "D:\anaconda\envs\tf2x\lib\site-packages\tensorflow\python\framework\constant_op.py" , line 96 , in convert_to_eager_tensor
return ops. EagerTensor( value, ctx. device_name, dtype)
ValueError: Can't convert non- rectangular Python sequence to Tensor.
import tensorflow as tf
a = tf. constant( [ 1 , - 2 , - 3 ] , dtype= tf. float32)
b = a = tf. constant( [ 4 , - 7 , - 6 ] , dtype= tf. float32)
tf_abs = tf. abs ( a)
print ( '绝对值:' , tf_abs)
tf_neg = tf. negative( a)
print ( '取负:' , tf_neg)
tf_sign = tf. sign( a)
print ( '返回:' , tf_sign)
tf_squ = tf. square( a)
print ( '平方:' , tf_squ)
tf_sqrt = tf. sqrt( a)
print ( '开根号:' , tf_sqrt)
tf_pow = tf. pow ( a, b)
print ( '幂次方:' , tf_pow)
tf_max = tf. maximum( a, b)
print ( '最大值:' , tf_max)
>> > 绝对值: tf. Tensor( [ 4. 7. 6. ] , shape= ( 3 , ) , dtype= float32)
>> > 取负: tf. Tensor( [ - 4. 7. 6. ] , shape= ( 3 , ) , dtype= float32)
>> > 返回: tf. Tensor( [ 1. - 1. - 1. ] , shape= ( 3 , ) , dtype= float32)
>> > 平方: tf. Tensor( [ 16. 49. 36. ] , shape= ( 3 , ) , dtype= float32)
>> > 开根号: tf. Tensor( [ 2. nan nan] , shape= ( 3 , ) , dtype= float32)
>> > 幂次方: tf. Tensor( [ 2.5600000e+02 - 1.2142656e-06 2.1433470e-05 ] , shape= ( 3 , ) , dtype= float32)
>> > 最大值: tf. Tensor( [ 4. - 7. - 6. ] , shape= ( 3 , ) , dtype= float32)
import tensorflow as tf
a = tf. constant( [ 1 , - 2 , - 3 ] )
print ( a. dtype)
< dtype: 'int32' >
import tensorflow as tf
a = tf. constant( [ 1 , - 2 , - 3 ] )
print ( a. dtype)
tf_sqrt = tf. sqrt( a)
print ( '开根号:' , tf_sqrt)
>> > File "D:\pycharm\pythonProject2\tensor\a.py" , line 8 , in < module>
tf_sqrt = tf. sqrt( a)
import tensorflow as tf
a = tf. constant( [ 1 , - 2 , - 3 ] )
print ( a. dtype)
a = tf. constant( [ 1 , - 2 , - 3 ] , dtype= tf. float32)
print ( a. dtype)
tf_sqrt = tf. sqrt( a)
print ( '开根号:' , tf_sqrt)
>> > < dtype: 'int32' >
>> > < dtype: 'float32' >
>> > 开根号: tf. Tensor( [ 1. nan nan] , shape= ( 3 , ) , dtype= float32)