数据
# -*- coding: utf-8 -*-
import tensorflow as tf
c1=tf.constant(2)
print (c1)
c2=tf.constant(2.5)
print (c2)
c3=tf.constant("a")
print (c3)
c4=tf.constant((1,-1),tf.complex64)
print (c4)
常数运算
c11=tf.constant(2)
c12=tf.constant(4)
print ((c11+c12).numpy())
print ((c11-c12).numpy())
print ((c11*c12).numpy())
print ((c11/c12).numpy())
print (tf.add(c11,c12).numpy())
print (tf.subtract(c11,c12).numpy())
print (tf.multiply(c11,c12).numpy())
print (tf.divide(c11,c12).numpy())
矩阵
t0=tf.constant([[1,2,3,4,5]],dtype=tf.float32)
print (t0)
t1=tf.constant([[1,2,3,4,5]],dtype=tf.int32)
print (t1)
t2=tf.constant([['1','2','3','4','5']],dtype=tf.string)
print (t2)
矩阵操作
mix=tf.constant([
[1,2,3],
[2,3,4],
[5,6,7]
])
print (mix)
print (mix.numpy())#只显示矩阵
print (mix.shape)#矩阵形状3*3
print (mix[1][1])#某行某列
print (mix[1][1].numpy())#某行某列的值
print (tf.reduce_sum(mix))#矩阵求和
print (tf.reduce_prod(mix))#矩阵求积
print (tf.reduce_mean(mix))#平均值
A=tf.constant([
[1,2,3],
[2,3,4],
[3,4,5]
])
B=tf.constant([
[1,2,3],
[2,3,4],
[3,4,5]
])
print(A*B)
print(A+B)
print(A-B)
print(tf.matmul(A,B))
print(tf.add(A,B))
print (tf.subtract(A,B))
变数
t=tf.Variable([2,1],[1,2])
print(t)
t1=tf.Variable(['Hello'],tf.string)
print(t1)
t2=tf.Variable([2.1,1],tf.float32)
print(t2)
t3=tf.Variable([2,1,0,4],tf.int32)
print(t3)
t4=tf.Variable([(2,-5),(1,-4)],tf.complex64)
print(t4)
随机值
r=tf.random.uniform([3,4],-5,5)#shape,3行4列;[-5,5]之间随机
print (r)
print(tf.round(r))#取整
r1=tf.random.normal([3,4],0,1)#68%[-1,1],95%[-2,2]
print (r1)
最大值
import numpy as np
r1=tf.random.normal([3,4],0,1)
print(np.max(r1))#最大值
print(np.max(r1,0))#每一列最大值
print(np.argmax(r1))#最大值位置
print(np.argmax(r1,0))#每一列最大值位置