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(([email protected](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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章