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
# index
"""
t = tf.constant([[1.,2.,3.],[4.,5.,6.]])
print(t)
print(t[:,1:])
print(t[:,1])
# ops
print(t+10)
print(tf.square(t))
# 转置
print(t@tf.transpose(t))
# numpy转化
print(t.numpy())
print(np.square(t))
np_t = (np.array([[1.,2.,3.],[4.,5.,6.]]))
print(tf.constant(np_t))
# Scalars
t = tf.constant(2.78)
print(t.numpy())
print(t.shape)
# string
t = tf.constant("cafe6")
print(t)
print(tf.strings.length(t))
print(tf.strings.length(t,unit="UTF8_CHAR"))
print(tf.strings.unicode_decode(t,"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
print(r)
print(r[1])
print(r[1:3])
#ops on raged tensor
r2 = tf.ragged.constant([[51,52],[],[62,89],[71]])
# axis用于定义放置的位置
print(tf.concat([r,r2],axis=1))
#r.to_tensor不适合希望补0的存储
print(r.to_tensor())
# sparse tensor indices需要排好序
s = tf.SparseTensor(indices=[[0,1],[1,0],[2,3]],
values = [1.,2.,3.],
dense_shape = [3,4])
print(s)
# 如果没排好序,使用tf.sparse.reorder()
print(tf.sparse.to_dense(s))
#ops on sparse tensors
s2= s*2.0
print("s2=",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))
"""
# varible
v = tf.Variable([[1.,2.,3.],[4.,5.,6.]])
# print(v)
# print(v.value())
# print(v.numpy())
# assign value
v.assign(2*v)
print(v.numpy())
v[0,1].assign(42)
print(v.numpy())
v[1].assign([7.,8.,9.])
print(v.numpy())
11-25
183
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)