import tensorflow as tf from tensorflow import keras from tensorflow.keras import datasets import os # do not print irrelevant information # os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # x: [60k,28,28], [10,28,28] # y: [60k], [10k] (x, y), (x_test, y_te…
手写数字识别流程 MNIST手写数字集7000*10张图片 60k张图片训练,10k张图片测试 每张图片是28*28,如果是彩色图片是28*28*3-255表示图片的灰度值,0表示纯白,255表示纯黑 打平28*28的矩阵,得到28*28=784的向量 对于b张图片得到[b,784];然后对于b张图片可以给定编码 把上述的普通编码给定成独热编码,但是独热编码都是概率值,并且概率值相加为1,类似于softmax回归 套用线性回归公式 X[b,784] W[784,10] b[10] 得到 [b,1…
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.…
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x = tf.random.normal([4,32,32,3]) x.shape (x+tf.random.normal([3])).shape (x+tf.random.normal([32,32,1])).shape (x+tf.random.normal([4,1,1,1])).shape tr…
图片视图 [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…
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)…
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 x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.constant(2.) b2 = tf.constant(1.) with tf.GradientTape(persistent=True) as tape: tape.watch([w1, b1, w2, b2]) y1 = x * w1 + b1 y2 = y1 * w2 + b2 dy2_dy1 = t…
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) y = tf.constant([2, 0]) with tf.GradientTape() as tape: tape.watch([w, b]) # axis=1,表示结果[b,3]中的3这个维度为概率 prob = tf.nn.softmax(x @ w + b, axis=1) # 2 --…