import matplotlib as mpl
import matplotlib. pyplot as plt
% matplotlib inline
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__)
2.0.0
sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0)
matplotlib 3.1.3
numpy 1.18.1
pandas 1.0.0
sklearn 0.22.1
tensorflow 2.0.0
tensorflow_core.keras 2.2.4-tf
t = tf. constant( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( t)
print ( t[ : , 1 : ] )
print ( t[ : , 1 ] )
t_1 = tf. constant( [ [ 1 . , 2 . , 3 . ] , [ 4 . , 5 . , 6 . ] ] )
print ( t_1)
print ( t_1[ : , 1 : ] )
print ( t_1[ : , 1 ] )
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[2 3]
[5 6]], shape=(2, 2), dtype=int32)
tf.Tensor([2 5], shape=(2,), dtype=int32)
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[2. 3.]
[5. 6.]], shape=(2, 2), dtype=float32)
tf.Tensor([2. 5.], shape=(2,), dtype=float32)
print ( t+ 10 )
print ( tf. square( t) )
print ( t @ tf. transpose( t) )
tf.Tensor(
[[11 12 13]
[14 15 16]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[ 1 4 9]
[16 25 36]], shape=(2, 3), dtype=int32)
tf.Tensor(
[[14 32]
[32 77]], shape=(2, 2), dtype=int32)
print ( t. numpy( ) )
print ( np. square( t) )
np_t = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
print ( tf. constant( np_t) )
[[1 2 3]
[4 5 6]]
[[ 1 4 9]
[16 25 36]]
tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
t = tf. constant( 2.63554 )
print ( t. numpy( ) )
print ( t. shape)
2.63554
()
t = tf. constant( "cafe" )
print ( t)
print ( tf. strings. length( t) )
print ( tf. strings. length( t, unit= "UTF8_CHAR" ) )
print ( tf. strings. unicode_decode( t, "utf8" ) )
tf.Tensor(b'cafe', shape=(), dtype=string)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([ 99 97 102 101], shape=(4,), dtype=int32)
t = tf. constant( [ "cafe" , "coffee" , "咖啡" ] )
print ( tf. strings. length( t, unit= "UTF8_CHAR" ) )
r = tf. strings. unicode_decode( t, "utf8" )
print ( r)
tf.Tensor([4 6 2], shape=(3,), dtype=int32)
<tf.RaggedTensor [[99, 97, 102, 101], [99, 111, 102, 102, 101, 101], [21654, 21857]]>
r = tf. ragged. constant( [ [ 11 , 22 ] , [ 21 , 22 , 33 ] , [ 1 ] , [ 4 , 5 , 6 , 9 , 3 ] ] )
print ( r)
print ( r[ 1 ] )
print ( r[ 1 : 3 ] )
<tf.RaggedTensor [[11, 22], [21, 22, 33], [1], [4, 5, 6, 9, 3]]>
tf.Tensor([21 22 33], shape=(3,), dtype=int32)
<tf.RaggedTensor [[21, 22, 33], [1]]>
r2 = tf. ragged. constant( [ [ 51 , 52 ] , [ ] , [ 77 ] ] )
print ( tf. concat( [ r, r2] , axis = 0 ) )
r3 = tf. ragged. constant( [ [ 1 , 2 ] , [ 2 , 22 , 33 ] , [ ] , [ 4 , 9 , 3 ] ] )
print ( tf. concat( [ r, r3] , axis = 1 ) )
<tf.RaggedTensor [[11, 22], [21, 22, 33], [1], [4, 5, 6, 9, 3], [51, 52], [], [77]]>
<tf.RaggedTensor [[11, 22, 1, 2], [21, 22, 33, 2, 22, 33], [1], [4, 5, 6, 9, 3, 4, 9, 3]]>
print ( r. to_tensor( ) )
tf.Tensor(
[[11 22 0 0 0]
[21 22 33 0 0]
[ 1 0 0 0 0]
[ 4 5 6 9 3]], shape=(4, 5), dtype=int32)
s = tf. SparseTensor( indices = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 2 , 3 ] ] ,
values = [ 1 . , 2 . , 3 . ] ,
dense_shape = [ 3 , 4 ] )
print ( s)
print ( tf. sparse. to_dense( s) )
SparseTensor(indices=tf.Tensor(
[[0 1]
[1 0]
[2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 1. 0. 0.]
[2. 0. 0. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
s2 = s * 2.0
print ( s2)
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) )
SparseTensor(indices=tf.Tensor(
[[0 1]
[1 0]
[2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([2. 4. 6.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
unsupported operand type(s) for +: 'SparseTensor' and 'int'
tf.Tensor(
[[ 30. 40.]
[ 20. 40.]
[210. 240.]], shape=(3, 2), dtype=float32)
s5 = tf. SparseTensor( indices = [ [ 0 , 2 ] , [ 0 , 1 ] , [ 2 , 3 ] ] ,
values = [ 1 . , 2 . , 3 . ] ,
dense_shape = [ 3 , 4 ] )
print ( s5)
s6 = tf. sparse. reorder( s5)
print ( tf. sparse. to_dense( s6) )
SparseTensor(indices=tf.Tensor(
[[0 2]
[0 1]
[2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 2. 1. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
v = tf. Variable( [ [ 1 . , 2 . , 3 . ] , [ 4 . , 5 . , 6 . , ] ] )
print ( v)
print ( v. value( ) )
print ( v. numpy( ) )
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)>
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
[[1. 2. 3.]
[4. 5. 6.]]
v. assign( 2 * v)
print ( v. numpy( ) )
v[ 0 , 1 ] . assign( 42 )
print ( v. numpy( ) )
v[ 1 ] . assign( [ 7 . , 8 . , 9 . ] )
print ( v. numpy( ) )
[[ 2. 4. 6.]
[ 8. 10. 12.]]
[[ 2. 42. 6.]
[ 8. 10. 12.]]
[[ 2. 42. 6.]
[ 7. 8. 9.]]
try :
v[ 1 ] = [ 7 . , 8 . , 9 . ]
except TypeError as ex:
print ( ex)
'ResourceVariable' object does not support item assignment