栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Tensorflow张量的创建与操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Tensorflow张量的创建与操作

目录

一、张量创建

二、张量的操作

1、tf.strings

2、tf.debugging 

3、tf.random

4、tf.math

5、tf.dtypes


一、张量创建
import tensorflow as tf
print (tf.__version__)  ###打印tensorflow版本号
print (tf.test.is_gpu_available())   ###打印是否有gpu
#Rank 0
mamal = tf.Variable("Elephant",tf.string)
tf.print(tf.rank(mamal))
#Rank 1  一阶张量
mystr = tf.Variable(["Hello"],tf.string)
tf.print(tf.rank(mystr))
tf.print(tf.shape(mystr))
#Rank 2  二阶张量
mymat = tf.Variable([[7],[11]],tf.int16)
tf.print(tf.rank(mymat))
tf.print(tf.shape(mymat))

二、张量的操作

1、tf.strings
tf.strings.bytes_split('hello') ###按照字符切割,把0阶转换为1阶
tf.strings.split('hello world') ###单次切割 默认把句子按照空格切割,0阶变成1阶
tf.strings.to_hash_bucket(['hello','world'], num_buckets=10) ###string hash

2、tf.debugging 
# 判断x和y的shape
a=tf.random.uniform((10,10))
tf.debugging.assert_equal(x=a.shape,y=(10,10))
#当x和y的shape不同时,会报错
tf.debugging.assert_equal(x=a.shape,y=(20,10))

 3、tf.random
a = tf.random.uniform(shape=(10,5),minval=0,maxval=10)#随机初始化十行五列,最小为0,最大为10
a

 4、tf.math
a = tf.constant([[1,2],[3,4]])
b = tf.constant([[5,6],[7,8]])
tf.print(tf.math.add(a,b)) #加
tf.print(tf.math.subtract(a,b)) #减
tf.print(tf.math.multiply(a,b)) #乘
tf.print(tf.math.divide(a,b)) #除

 5、tf.dtypes
x = tf.constant([1.8,2.2], dtype = tf.float32)
x1 = tf.dtypes.cast(x, tf.int32)
x1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号