import tensorflow as tf a = tf.ones([1, 5, 5, 3]) a.shape a[0][0] numpy : 索引 a = tf.random.normal([4, 28, 28, 3]) a.shape a[1].shape a[1, 2].shape a[1][2][3].shape a[1, 2, 3, 2].shape 一维切片 a = tf.range(10) a a[-1:] a[-2:] a[:2] a[:-1] 多维切片 a = tf.ran…
import tensorflow as tf a = tf.random.normal([3, 3]) a mask = a > 0 mask # 为True元素,即>0的元素的索引 indices = tf.where(mask) indices # 取回>0的值 tf.gather_nd(a, indices) A = tf.ones([3, 3]) B = tf.zeros([3, 3]) # True的元素会从A中选值,False的元素会从B中选值 tf.where(mask,…
import tensorflow as tf a = tf.random.shuffle(tf.range(5)) a tf.sort(a, direction='DESCENDING') # 返回索引 tf.argsort(a, direction='DESCENDING') idx = tf.argsort(a, direction='DESCENDING') tf.gather(a, idx) idx = tf.argsort(a, direction='DESCENDING') tf.…
import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tensor(np.zeros([2, 3])) list tf.convert_to_tensor([1, 2]) tf.convert_to_tensor([1, 2.]) tf.convert_to_tensor([[1], [2.]]) zeros tf.zeros([]) tf.zeros([1])…
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himmeblau(x): return (x[0]**2 + x[1] - 11)**2 + (x[0] + x[1]**2 - 7)**2 x = np.arange(-6, 6, 0.1) y = np.arange(-6, 6, 0.1) print(f'x_shape: {x.shape},y_sh…
import tensorflow as tf a = tf.ones([2, 2]) a tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) a = tf.ones([4, 28, 28, 3]) a.shape tf.norm(a) tf.sqrt(tf.reduce_sum(tf.square(a))) b = tf.ones([2, 2]) tf.norm(b) tf.norm(b, ord=2, axis=1) tf.norm(b, ord=…
图片视图 [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏 [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片数据的细节 [b, 2, 14*28] # 保存b张图片,把图片分为上下两个部分,两个部分具体多少行是不清楚的 [b, 28, 28, 1] # 保存b张图片,28行,28列,1个通道 First Reshape(重塑视图) import tensorflow as tf a = tf.random…
list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补numpy的缺点,更多的是为了深度学习而生 tensor数据存储类型 scalar:标量,1.1 vector:向量,[1.1],[1.1,2.2,...] matrix: 矩阵,[[1.1,2.2],[3.3,4.4]] tensor:rank>2 数据类型: Int, float, double boo…
import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, optimizers, metrics def preprocess(x, y): """数据处理函数""" x = tf.cast(x, dtype=tf.float32) / 255. y = tf.cast(y, dtype=tf.int32)…