Tensorflow基础API使用_1.基础api引用


import matplotlib as mpl #Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用

import matplotlib.pyplot as plt #Python数据可视化matplotlib.pyplot

#%matplotlib inline #在使用jupyter notebook 或者 jupyter qtconsole的时候,经常会用到%matplotlib inline。其作用就是在你调用plot()进行画图或者直接输入Figure的实例对象的时候,会自动的显示并把figure嵌入到console中。

import numpy as np#数值计算扩展。这种工具可用来存储和处理大型矩阵

import sklearn#机器学习中常用的第三方模块,对常用的机器学习方法进行了封装,包括回归(Regression)、降维(Dimensionality Reduction)、分类(Classfication)、聚类(Clustering)等方法。
    
import pandas as pd#是python的一个数据分析包
import os #系统编程的操作模块,可以处理文件和目录
import sys #sys模块包含了与Python解释器和它的环境有关的函数
import time 
import tensorflow as tf

from tensorflow import keras
##################################################################################################
#选择GPU
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

##################################################################################################

print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
    print(module.__name__, module.__version__)
################################################################################################
#定义常量
t = tf.constant([[1.,2.,3.],[4.,5.,6.]])
print(t)
print(t[:,1:])#取出第二列之后的所有行
print(t[..., 1])#只取出第二列


################################################################################################
#op运算
print(t+10)
print(tf.square(t)) #对矩阵中的所有值进行运算

print(t @ tf.transpose(t)) #矩阵乘以它自己的转置

################################################################################################
# tensorflow 与 Numpy的转化的过程
print(t.numpy())
print(np.square(t))

np_t = np.array([[1.,2.,3.],[4.,5.,6.]])

print(tf.constant(np_t))
################################################################################################
# TensorFlow 0维的,叫做scalars 数
t = tf.constant(2.718)
print(t)
print(t.numpy())
print(t.shape) 

################################################################################################
# Tensorflow tensor 不仅存储数字还能存储字符串
# strings
t = tf.constant("cafe")
print(t)
print(tf.strings.length(t)) #获得字符串的长度
print(tf.strings.length(t,unit="UTF8_CHAR")) #获得UTF8_CHAR字符串的长度
print(tf.strings.unicode_decode(t,"UTF8")) #转成UTF8的编码

################################################################################################
# string array
t = tf.constant(["cafe", "coffee", "咖啡"])
print(tf.strings.length(t, unit="UTF8_CHAR"))
r = tf.strings.unicode_decode(t, "UTF8")
print(r)

################################################################################################
# ragged tensor #不规则的数据,相当于每一行有不同的列

r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
# index op
print(r)
print(r[1])#取出第二行
print(r[1:2])#取出第二行到第三行
################################################################################################

# ops on ragged tensor
r2 = tf.ragged.constant([[51, 52], [], [71]])
print(tf.concat([r, r2], axis = 0)) #拼接,axis =0表示按行拼接,r2就是拼到r的下面


r3 = tf.ragged.constant([[13, 14], [15], [], [42, 43]])
print(tf.concat([r, r3], axis = 1)) #表示按列拼接,r3就是拼到r的右边面

#tensor是规则矩阵
print(r.to_tensor())#以最大列长为矩阵的长,其他列缺失的数补零,但是零都补到值的后面

################################################################################################
# sparse tensor
#在指定坐标的位置指定值,其他都是零
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],#指定的坐标
                    values = [1., 2., 3.],#与指定的坐标,对应的值
                    dense_shape = [3, 4])#tensor矩阵的大小
print(s)
print(tf.sparse.to_dense(s)) # 转为ragged tensor

#indices 必须是排序后的
s5 = tf.SparseTensor(indices = [[0, 2], [0, 1], [2, 3]],#指定的坐标
                    values = [1., 2., 3.],#与指定的坐标对应的值
                    dense_shape = [3, 4])#tensor矩阵的大小
print(s)
s6 = tf.sparse.reorder(s5)#如果没有排序,使用自动排序,不然后一句就会报错
print(tf.sparse.to_dense(s6)) # 转为ragged tensor

################################################################################################
# ops on sparse tensors

#乘法
s2 = s * 2.0
print(s2)

#加法,sparse tensors不能做加法
try:
    s3 = s+1
except TypeError as ex:
    print(ex)

#矩阵相乘
s4 = tf.constant([[10., 20.],
                  [30., 40.],
                  [50., 60.],
                  [70., 80.]])
print(tf.sparse.sparse_dense_matmul(s,s4))

################################################################################################
#常量在运算过程是不能变的,相当于不能重新赋值

# 定义变量
v = tf.Variable([[1., 2., 3.], [4., 5., 6.]])
print(v)
print(v.value())
print(v.numpy())

################################################################################################
#重新赋值
v.assign(2*v)
print(v.numpy())

v[0,1].assign(42)#对矩阵中的特定的值赋值
print(v.numpy())

v[1].assign([7.,8.,9.])#对矩阵中第二行赋值
print(v.numpy())


#不能使用等于号赋值
try:
    v[1] = [7., 8., 9.]
except TypeError as ex:
    print(ex)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值