tensoflow2.0-数据类型

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

在这里插入图片描述
在这里插入图片描述

基本类型

# -*- 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的矩阵阵####################


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

佐倉

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

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

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

打赏作者

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

抵扣说明:

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

余额充值