基本类型
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
"""
constant(value, dtype=None, shape=None, name='Const')
Creates a constant tensor from a tensor-like object.
"""
#整型数据
l11 = tf.constant(1,4)
print (l11)
l12 = tf.constant([1,2,3,4,5,6])
print (l12)
#无符号整型
l1 = tf.constant(1,dtype=tf.uint8)
print (l1)
l2 = tf.constant([1,2,3,4,5,6],dtype=tf.uint8)
print (l2)
#单浮点型数据
l21 = tf.constant(1.)
print (l21)
l22 = tf.constant([1,2,3,4,5,6],dtype=tf.float32)
print (l22)
#双浮点型数据
l31 = tf.constant(1.,dtype=tf.double)
print (l31)
l32 = tf.constant([1,2,3,4,5,6],dtype=tf.float64)
print (l32)
#布尔型数据
l41 = tf.constant(True)
print (l41)
l42 = tf.constant([True,False])
print (l42)
#字符串型数据
l51 = tf.constant("the world")
print (l51)
l52 = tf.constant(["I","am","she","no"])
print (l52)
#复数型数据
l61 = tf.constant((2,-5),tf.complex64)
print (l61)
l62 = tf.constant([(2,5),(3,1)],tf.complex64)
print (l62)
print ("-".center(10,"-"))
查看帮助
help(tf.constant)
查看tf属性,函数
def search_data():
all_data = dir(tf)
for i in range(len(all_data)):
print (str(i) + " -->",all_data [i])
search_data()
基本类型数字代号
#constant(value, dtype=None, shape=None, name='Const')
#说明,可以只写两个参数,第一个value,第二个dtype;dtype可以用数字代替
for i in range(1,25):
try:
t = tf.constant(1,i)
#print (t)
print (i,"->",t.dtype)
except:
try:
t = tf.constant("1",i)
#print (t)
print (i,"->",t.dtype)
except:
pass
#tf.constant(True,10)#10代表布尔型
#tf.constant(1,1) <==> tf.constant(1,dtype = tf.float32)
数据类型拓展
###################gpu与cpu环境转换##################
###################gpu与cpu环境转换##################
###################gpu与cpu环境转换##################
with tf.device("cpu"):
a = tf.constant(1)
with tf.device("gpu"):
b = tf.range(1)
print (a.device)#返回设备名字
print (b.device)#返回设备名字
aa =a.gpu()#转换为gpu环境
bb =b.cpu()#转换为cpu环境
print (a.device)#返回设备名字
print (b.device)#返回设备名字
###################gpu与cpu环境转换##################
###################gpu与cpu环境转换##################
###################gpu与cpu环境转换##################
###################tensor与numpy转换##################
###################tensor与numpy转换##################
###################tensor与numpy转换##################
data = tf.constant([1,2,3,4,5,6])
num = data.numpy()#tensor-->numpy
print (num)#返回数据
print (data.shape)#返回形状
print (data.ndim)#返回ndim
result = tf.convert_to_tensor(num)#numpy-->tensor
result = tf.convert_to_tensor(num,dtype=tf.float64)#指定转换类型
print (result.dtype)
print (num.dtype)
###################tensor与numpy转换##################
###################tensor与numpy转换##################
###################tensor与numpy转换##################
###################tensor类型判断######################
###################tensor类型判断######################
###################tensor类型判断######################
r2= tf.is_tensor(data)
print (r2)
r1 = isinstance(num,tf.Tensor)
print (r1)
###################tensor类型判断######################
###################tensor类型判断######################
###################tensor类型判断######################
###################tensor类型转换######################
###################tensor类型转换######################
###################tensor类型转换######################
g_ = tf.convert_to_tensor(np.arange(6))
g = tf.range(6)
print (g.dtype)
g1 = tf.cast(g,1)#5代表数据类型
print (g1.dtype)
g2 = tf.cast(g1,6)#6代表数据类型
print (g2.dtype)
t1 = tf.constant([0,1])
print (t1.dtype)
t2 = tf.cast(t1,10)#10代表数据类型
print (t2.dtype)
###################tensor variable类型######################
###################tensor variable类型######################
###################tensor variable类型######################
d = tf.Variable(g)#神经网络参数相关类型
print (d.name)
print (d.trainable)
###################tensor variable类型######################
###################tensor variable类型######################
###################tensor variable类型######################
###################tensor 全部为1的矩阵阵####################
###################tensor 全部为1的矩阵阵####################
###################tensor 全部为1的矩阵阵####################
a= tf.ones(3,3)#3=一维数组三个元素,3=数据类型整型数据
print (a)
b= tf.ones([3,3])#二维数组3行3列
print (b)
###################tensor 全部为1的矩阵阵####################
###################tensor 全部为1的矩阵阵####################
###################tensor 全部为1的矩阵阵####################