TensorFlow numpy等基础运算

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras

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.]])

with tf.Session() as sess:
    print(t.eval())#Tensor("Const:0", shape=(2, 3), dtype=float32)
    print(t[:,1:].eval())#取出第2列以后的数据  t[起始行:结束行, 起始列:结束列]
    print(t[...,1].eval())#只取出第2列 [2. 5.]
    print(t[:, 1].eval())#只取出第2列 [2. 5.]

    print((t+10).eval())#矩阵每个元素都+10
    print(tf.square(t).eval())#矩阵每个元素都平方
    print((t@tf.transpose(t)).eval()) #矩阵 * 矩阵的转置

    #tensor转换为numpy
    #print((t.numpy()))  #TensorFlow2.0直接调用numpy()
    print("t.shape = ",t.shape)#t.shape =  (2, 3)
    print("t = ",t)#t =  Tensor("Const:0", shape=(2, 3), dtype=float32)
    data_numpy = t.eval()#TensorFlow1.x调用eval()
    print(data_numpy)#TensorFlow1.x
    print(np.square(data_numpy))
    print("data_numpy.shape = ",data_numpy.shape)#data_numpy.shape =  (2, 3)
    print("type(data_numpy) = ",type(data_numpy))#type(data_numpy) =  <class 'numpy.ndarray'>
    print("data_numpy.dtype = ",data_numpy.dtype)#data_numpy.dtype =  float32
    data_numpy2 = data_numpy.astype(np.int32)#// 转换数据类型  float -> int
    print("data_numpy2.dtype int32 = ", data_numpy2.dtype)  #data_numpy2.dtype int32 =  int32


    #numpy转换为tensor
    np_t = np.array([[1.,2.,3.], [4.,5.,6.]])
    print(np_t)
    print((tf.constant(np_t)))#.eval()  #Tensor("Const_1:0", shape=(2, 3), dtype=float64)

    #numpy 字符串数组转换为数值型
    numeric_strings = np.array(['1.2', '2.3', '3.2141'], dtype=np.string_)
    print(numeric_strings)#[b'1.2' b'2.3' b'3.2141']
    print("numeric_strings.dtype = ",numeric_strings.dtype)#numeric_strings.dtype =  |S6
    numeric_float = numeric_strings.astype(float)
    print(numeric_float)#[1.2    2.3    3.2141]
    print("numeric_strings.dtype = ",numeric_strings.dtype)#numeric_strings.dtype =  |S6

    #tensor格式的strings
    st = tf.constant("cafe")
    print(st.eval())#b'cafe'    Tensor("Const_2:0", shape=(), dtype=string)
    print(tf.strings.length(st).eval())#Tensor("StringLength:0", shape=(), dtype=int32)     【需加eval()才打印4】
    print(tf.strings.length(st,unit="UTF8_CHAR").eval())#4
    print(tf.strings.unicode_decode(st,"UTF8").eval())#[ 99  97 102 101]

    #tensor格式string的数组array
    sta = tf.constant(["cafe","caffee","咖啡"])
    print(tf.strings.length(sta,unit="UTF8_CHAR").eval())#[4 6 2]
    print(tf.strings.unicode_decode(sta,"UTF8"))#tf.RaggedTensor(values=Tensor("UnicodeDecode_1/UnicodeDecode:1", shape=(?,), dtype=int32), row_splits=Tensor("UnicodeDecode_1/UnicodeDecode:0", shape=(4,), dtype=int64))

    # #ragged tensorf形状分布不固定的Tensor [TensorFlow2.0新特性]
    # r = tf.ragged.constant([ [11,12],
    #                          [21,22,23],
    #                          [],
    #                          [41] ])
    # print(r.eval())

    #sparse tensor
    s = tf.SparseTensor(indices=[[0,1],[1,0],[2,3]],#说明上面数组的这些下标的位置是有值的,[0,1]:第0行,第1列位置有值;[1,0]:第1行,第0列位置有值;[2,3]:第2行,第3列位置有值;
                        values=[1.,2.,3.],#indices所指示的位置的具体值
                        dense_shape=[3,4])#生成数组的形状
    print(s.eval())
    #运行结果:
    # SparseTensorValue(indices=array([[0, 1],
    #                                  [1, 0],
    #                                  [2, 3]], dtype=int64), values=array([1., 2., 3.], dtype=float32),
    #                   dense_shape=array([3, 4], dtype=int64))
    print(tf.sparse.to_dense(s).eval())#Tensor("SparseToDense:0", shape=(3, 4), dtype=float32) 【to_dense需要indices中排好顺序,否则会报错】

    #sparse tensor的操作
    s2 = s * 2.0
    print(s2.eval())
    # 运行结果:
    # SparseTensorValue(indices=array([[0, 1],
    #                                  [1, 0],
    #                                  [2, 3]], dtype=int64), values=array([2., 4., 6.], dtype=float32),
    #                   dense_shape=array([3, 4], dtype=int64))

    try:
        s3 = s + 1
    except TypeError as ex:
        print(ex)#unsupported operand type(s) for +: 'SparseTensor' and 'int'

    s4 = tf.constant([ [10.,20.],
                       [30.,40.],
                       [50.,60.],
                       [70.,80.] ] )
    print(tf.sparse.sparse_dense_matmul(s,s4).eval())

    #SparseTensor使用需要注意的“坑”
    s5 = tf.SparseTensor(indices=[[0,2],[0,1],[2,3]],#说明上面数组的这些下标的位置是有值的,[0,1]:第0行,第1列位置有值;[1,0]:第1行,第0列位置有值;[2,3]:第2行,第3列位置有值;
                        values=[1.,2.,3.],#indices所指示的位置的具体值
                        dense_shape=[3,4])#生成数组的形状
    print(s5.eval())
    s6 = tf.sparse.reorder(s5)#需要这个,否则报错:tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[1] = [0,1] is out of order
    #默认按照从左到右,从上到下的顺序构造,但是[0,2],[0,1]的顺序发生反转,故需要reorder,否则报错
    print(tf.sparse.to_dense(s6).eval())


    #变量variables
    v = tf.Variable( [ [1.,2.,3.],[4.,5.,6.] ] )
    print(v)#<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32_ref>
    print(v.value())#Tensor("Variable/read:0", shape=(2, 3), dtype=float32)
    

执行结果:
1.14.0
sys.version_info(major=3, minor=5, micro=6, releaselevel='final', serial=0)
matplotlib 1.5.3
numpy 1.17.1
pandas 0.18.1
sklearn 0.19.0
tensorflow 1.14.0
tensorflow.python.keras.api._v1.keras 2.2.4-tf
WARNING: Logging before flag parsing goes to stderr.

2020-01-31 15:12:37.886900: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[[1. 2. 3.]
 [4. 5. 6.]]
[[2. 3.]
 [5. 6.]]
[2. 5.]
[2. 5.]
[[11. 12. 13.]
 [14. 15. 16.]]
[[ 1.  4.  9.]
 [16. 25. 36.]]
[[14. 32.]
 [32. 77.]]
t.shape =  (2, 3)
t =  Tensor("Const:0", shape=(2, 3), dtype=float32)
[[1. 2. 3.]
 [4. 5. 6.]]
[[ 1.  4.  9.]
 [16. 25. 36.]]
data_numpy.shape =  (2, 3)
type(data_numpy) =  <class 'numpy.ndarray'>
data_numpy.dtype =  float32
data_numpy2.dtype int32 =  int32
[[1. 2. 3.]
 [4. 5. 6.]]
Tensor("Const_1:0", shape=(2, 3), dtype=float64)
[b'1.2' b'2.3' b'3.2141']
numeric_strings.dtype =  |S6
[1.2    2.3    3.2141]
numeric_strings.dtype =  |S6
b'cafe'
4
4
[ 99  97 102 101]
[4 6 2]
tf.RaggedTensor(values=Tensor("UnicodeDecode_1/UnicodeDecode:1", shape=(?,), dtype=int32), row_splits=Tensor("UnicodeDecode_1/UnicodeDecode:0", shape=(4,), dtype=int64))
SparseTensorValue(indices=array([[0, 1],
       [1, 0],
       [2, 3]], dtype=int64), values=array([1., 2., 3.], dtype=float32), dense_shape=array([3, 4], dtype=int64))
[[0. 1. 0. 0.]
 [2. 0. 0. 0.]
 [0. 0. 0. 3.]]
SparseTensorValue(indices=array([[0, 1],
       [1, 0],
       [2, 3]], dtype=int64), values=array([2., 4., 6.], dtype=float32), dense_shape=array([3, 4], dtype=int64))
unsupported operand type(s) for +: 'SparseTensor' and 'int'
[[ 30.  40.]
 [ 20.  40.]
 [210. 240.]]
SparseTensorValue(indices=array([[0, 2],
       [0, 1],
       [2, 3]], dtype=int64), values=array([1., 2., 3.], dtype=float32), dense_shape=array([3, 4], dtype=int64))
[[0. 2. 1. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 3.]]
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32_ref>
Tensor("Variable/read:0", shape=(2, 3), dtype=float32)

Process finished with exit code 0
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值