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 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 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…
import tensorflow as tf # 6个班级的学生分数情况 a = tf.ones([4, 35, 8]) b = tf.ones([2, 35, 8]) c = tf.concat([a, b], axis=0) c.shape # 3个学生学生补考 a = tf.ones([4, 32, 8]) b = tf.ones([4, 3, 8]) tf.concat([a, b], axis=1).shape a = tf.ones([4, 35, 8]) b = tf.ones(…
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 --…
import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.constant([1]) with tf.GradientTape() as tape: tape.watch([w, b]) prob = tf.sigmoid(x @ w + b) loss = tf.reduce_mean(tf.losses.MSE(y, prob)) grads = tape.…