当前位置:   article > 正文

深度学习tensorflow的基本内容

深度学习tensorflow的基本内容

Terson类型:
    .int(整型) float(浮点型) double(双浮点型)
    .bool(布尔型)
    .string(浮点型)


创建Tensor的几种方法:
1、将numpy, list类型进行转化成tensor用convert_to_tensor
2、zeros, ones,, zeros_like, ones_like
3、fill ——> 填充
4、random -->normal创建一个正态分布的数据,truncated_normal截断一部分数据, uniform创建一个均匀分布的数据, shuffle随机打散
5、constant--> 创建一个常量


Terson常量的方法:

numpy(): 查看常量的数据, dtype: 查看常量的的类型, shape查看常量的形状, ndim,  rank: 查看常量的维度
device: 当前tensor所在的设备的名字
判断是否是Tensor类型的方法: isinstance, is_tensor
把其他类型转化为tensor类型: convert_to_tensor
数据类型的转化: cast
将Tensor常量转化为能求导的Tensor : Variable --> trainable查看是否可求导, name

  1. import numpy as np
  2. import tensorflow as tf
  3. with tf.device("cpu"):
  4. a = tf.constant([1]) #在cpu环境下创建一个Tensor
  5. a.device
  6. with tf.device("gpu"):
  7. b = tf.constant([1])
  8. b.device
  9. aa = a.gpu
  10. bb = b.cpu
  1. # numpy
  2. tf.convert_to_tensor(np.ones(shape = [3, 3]))
  3. tf.convert_to_tensor(np.eye(3))
  4. tf.convert_to_tensor(np.linspace(1, 10, 10))
  5. tf.convert_to_tensor(np.random.randint(1, 10, size = (2, 4)))
  6. # list
  7. tf.convert_to_tensor([1, 2, 3, 4, 5, 6, 7])
  8. # tf.ones(shape, dtype)
  9. tf.ones(shape = (3, 4), dtype = tf.float32)
  10. a = tf.constant([1, 2, 3, 4, 5, 6, 6])
  11. tf.ones_like(a) # 计算a的shape然后按照这个shape创建一个ones
  12. # tf.zeros(shape, dtype)
  13. tf.zeros(shape = (3, 4), dtype = tf.float32)
  14. a = tf.constant([1, 2, 3, 4, 5, 6, 6])
  15. tf.ones_like(a) # 计算a的shape然后按照这个shape创建一个zeros
  16. #tf.fill(dims, value)
  17. tf.fill(dims = (3, 4), value = 3)
  18. # tf.random.normal(shape, mean, stddev, dtype)
  19. tf.random.normal(shape = (3, 4), mean = 0, stddev = 0.4, dtype = tf.float32)
  20. # tf.random.uniform(shape, minval, maxval, dtype)
  21. tf.random.uniform(shape = (3, 4), minval = 0, maxval = 10, dtype = tf.float32)
  22. # tf.random.shuffle(value)
  23. a = np.arange(10)
  24. tf.random.shuffle(a)
  25. # constant
  26. tf.constant(1.) # <tf.Tensor: shape=(), dtype=float32, numpy=1.0>
  27. tf.constant([True, False]) # <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>
  28. tf.constant(2) # <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>
  29. tf.constant("hello world") # <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>
  1. a = tf.random.normal(shape = (4, 5), mean = 0, stddev = 0.3, dtype = tf.float32)
  2. a.numpy(), a.dtype, a.shape, a.ndim, tf.rank(a)
  3. isinstance(a, tf.Tensor), tf.is_tensor(a)
  4. tf.cast(a, dtype = tf.float16)
  5. b = tf.Variable(a, name = "input_data")
  6. b.trainable, b.name
  7. a.dtype == tf.float32 # 判断两个类型相同
  1. # loss的例子
  2. out = tf.random.uniform([4, 10])
  3. out = tf.nn.softmax(out) # softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis, keepdims=True)
  4. y = tf.range(4)
  5. y = tf.one_hot(y, depth = 10)
  6. loss = tf.keras.losses.MSE(y, out) # loss = mean(square(y_true - y_pred), axis=-1)
  7. loss = tf.reduce_mean(loss)
  8. loss
  1. """
  2. .Bias
  3. .[out_dim]
  4. """
  5. net = tf.keras.layers.Dense(10) # 全连接神经网络
  6. net.build(input_shape = (4, 10)) # 建立网络输入数据
  7. net.kernel # 网络中参数w
  8. net.bias # 网络中偏置b
  1. """
  2. matrix(矩阵)
  3. .input x: [b, vec_dim] b: 多少张照片, vec_dim: 照片的长宽
  4. .weight:[input_dim, out_dim] # w权值的
  5. """
  6. x = tf.random.normal([4, 784])
  7. net = tf.layers.Dense(10)
  8. net.build(input_shape = [4, 784])
  9. net(x).shape
  10. net.kernel.shape, net.bias.shape
  1. """
  2. Dim = 3 Tensor
  3. x: [b, seq_dim, word_dim] # b:多少句子, seq_dim: 单词数量, word_dim: 编码长度
  4. """
  5. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data(num_words = 10000)
  6. # num_words 最多考虑的单词的数量 设置为10000表示只关注最常用的前10000个词汇,其他较少使用的忽略
  7. x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen = 80) #maxlen每一个句子最长有多少单词,然后其他单词截断
  8. emb = tf.keras.layers.Embedding(input_dim=10000, output_dim=100, input_length=80)
  9. """
  10. input_dim=10000  表示输入数据中词汇表的大小,即总共有 10000 个不同的元素(例如 10000 个不同的单词)。
  11. output_dim=100  表示每个输入元素被映射到的嵌入向量的维度大小,即生成的嵌入向量长度为 100。
  12. input_length=80  表示输入序列的长度,即每个输入样本中包含的元素个数为 80。
  13. """
  14. out = tf.keras.layers.SimpleRNNCell(units, dropout) #  units 表示输出空间的维度,即隐藏层神经元的数量;
  1. """
  2. Dim = 4 Tensor
  3. .image [b, h, w, 3]
  4. .feature maps(特征图): [b, h, w, c]
  5. """
  6. x = tf.random.normal([4, 32, 32, 3])
  7. net = tf.keras.layers.Conv2D(filters, kernel_size, padding, activation) #filters: 卷积核, kernel_size: 滤波矩阵, padding: 填充, activation 激活函数
  8. net(x) #调用这个网络
  9. """
  10. Dim = 5 Tensor
  11. .single task [b, h, w, 3]
  12. .[task_b, b, h, w, 3]
  13. """
  1. # 索引
  2. a = tf.random.truncated_normal(shape = [4, 32, 32, 3], mean = 0, stddev = 0.8)
  3. a[0][0].shape, a[0][0][0].shape, a[0][0][0][2]
  4. a[1, 2, 3, 2], a[1, 2].shape, a[1, 3, 4].shape
  5. a[1, ..., 2].shape,
  6. # 切片 start: end: step
  7. a[-1:].shape, a[1:3, 0:14, 1:28:2, 0:2].shape, a[0, 1, :, :].shape
  8. a[0:2, :, :, 2].shape
  9. a[:, ::2, ::2, :].shape
  10. a[::-1].shape
  1. """
  2. tf.gather(params, indices, axis) 根据提供的indices(索引)从axis(维度)收集元素
  3. tf.gather_nd(params, indices) 利用indices 收集元素
  4. tf.boolean_mask(tensor, mask, axis=None) 利用mask(布尔值)从axis(维度)收集元素
  5. """
  6. tf.gather(a, axis = 0, indices = [2, 3]).shape
  7. tf.gather(a, axis = 0, indices = [2, 1, 3, 0]).shape
  8. tf.gather(a, axis = 1, indices = [2, 3, 4, 5, 7, 9, 12]).shape
  9. tf.gather_nd(a, indices = [0, 1]).shape
  10. tf.gather_nd(a, indices = [0, 1, 2])
  11. tf.gather_nd(a, indices = [[0, 0], [1, 1]]).shape
  12. tf.gather_nd(a, indices = [[0, 0], [1, 1], [2, 2]]).shape
  13. tf.gather_nd(a, indices = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]).shape
  14. tf.boolean_mask(a, mask = [True, True, False, False], axis = 0).shape
  15. tf.boolean_mask(a, mask = [True, False, False], axis = 3).shape
  1. """
  2. 维度变换:
  3. .shape 形状
  4. .ndim 维度
  5. .reshape(tensor, shape) 改变形状
  6. .expand_dims(input, axis)/squeeze(input, axis) 扩展/压缩维度
  7. .transpose(a, perm)
  8. Broadcasting(广播机制)
  9. .broadcast_to
  10. .expand
  11. .expand_dims
  12. .without copying data
  13. .tf.tile
  14. """
  15. # shape, ndim
  16. a1 = tf.random.truncated_normal(shape = (4, 28, 28, 3), mean = 0, stddev = 1)
  17. a1.shape, a1.ndim
  18. # reshape
  19. tf.reshape(a1, shape = (4, 28 * 28, 3)).shape
  20. #expand_dims/squeeze
  21. tf.expand_dims(a1, axis=0).shape,
  22. a2 = tf.expand_dims(a1, axis = 4)
  23. tf.squeeze(a2).shape
  24. #transpose
  25. tf.transpose(a1, perm = [0, 2, 3, 1])
  26. #Broadcast
  27. b = tf.ones(shape = (3, 3), dtype = tf.float32)
  28. b = tf.expand_dims(b, axis = 2)
  29. b = tf.tile(b, [1, 1, 3])
  30. b.shape
  31. tf.broadcast_to(tf.random.normal([4, 1, 1, 1]), [4, 32, 32, 3]).shape
  32. x = tf.random.uniform([4, 32, 32, 3])
  33. (x + tf.random.uniform([3])).shape
  34. (x + tf.random.uniform([32, 32, 1])).shape
  35. (x + tf.random.uniform([4, 1, 1, 1])).shape
  1. """
  2. 数学运算
  3. . +, -, *, / --> element_wise
  4. . **, pow, square
  5. .sqrt
  6. .//, %
  7. . exp, math.log
  8. . @, matmul
  9. .layers-->Dense, Conv2D, MaxPooling2D, Embedding, SimpleRNNCell, LSTM
  10. .losses--> MSE, categorical_crassentropy
  11. dim_wise
  12. .reduce_mean/max/min/sum/variance
  13. """
  14. a = tf.random.normal(shape = (4, 3))
  15. b = tf.random.normal(shape = (4, 3))
  16. b - a, b + a, b * a, b / a, b ** a, tf.pow(a, 3), a ** 3, tf.square(a), tf.sqrt(a)
  17. tf.exp(a), tf.math.log(a) # 以e为底
  18. tf.math.log(8.) / tf.math.log(2.) #loge^8/loge^2
  19. tf.math.log(100.) / tf.math.log(10.) # loge^100/loge^10
  20. tf.transpose(a) @ b
  1. """
  2. 数学运算
  3. . +, -, *, / --> element_wise
  4. . **, pow, square
  5. .sqrt
  6. .//, %
  7. . exp, math.log
  8. . @, matmul
  9. .layers-->Dense, Conv2D, MaxPooling2D, Embedding, SimpleRNNCell, LSTM
  10. .losses--> MSE, categorical_crassentropy
  11. dim_wise
  12. .reduce_mean/max/min/sum/variance
  13. """
  14. a = tf.random.normal(shape = (4, 3))
  15. b = tf.random.normal(shape = (4, 3))
  16. b - a, b + a, b * a, b / a, b ** a, tf.pow(a, 3), a ** 3, tf.square(a), tf.sqrt(a)
  17. tf.exp(a), tf.math.log(a) # 以e为底
  18. tf.math.log(8.) / tf.math.log(2.) #loge^8/loge^2
  19. tf.math.log(100.) / tf.math.log(10.) # loge^100/loge^10
  20. tf.transpose(a) @ b
  1. """
  2. .pad(tensor, paddings, constant_values) paddings对左右上下进行填充[[上行, 下行], [左列, 右列]] constant_values填充的数字
  3. .expand_dims, tile
  4. .broadcast_to
  5. .where(condition, x, y)
  6. .scatter_nd(indices, updates, shape)
  7. .meshgrid
  8. 张量限幅:
  9. tf.clip_by_value(t, clip_value_min, clip_value_max)
  10. tf.nn.relu max(0, a)
  11. clip_by_norm(t, clip_norm) (clip_norm * t) / ||t||
  12. maximum
  13. minimum
  14. Gradient clipping : clip_by_global_norm(t_list, clip_norm) t_list[i] * clip_norm / max(global_norm, clip_norm)
  15. tf.one_hot
  16. """
  17. a = tf.reshape(tf.range(9), shape = (3, 3))
  18. a = tf.cast(tf.convert_to_tensor(a), dtype = tf.float32)
  19. tf.pad(a, [[1, 0], [1, 1]], constant_values = 1)
  20. aa = tf.expand_dims(a, axis = 0)
  21. tf.tile(aa, [4, 1, 1])
  22. tf.broadcast_to(a, [4, 3, 3])
  23. tf.where(a > 4, 0, 1)
  24. tf.maximum(a, 4) # 小于四的全部用四代替
  25. tf.minimum(a, 4) # 大于四的全部用四代替
  26. tf.clip_by_value(a, 2, 6) # 小于2用2代替大于6用6代替
  27. tf.nn.relu(a) # max(0, a)
  28. tf.clip_by_norm(t = a, clip_norm = 5).numpy()
  29. clipped_tensors, global_norm = tf.clip_by_global_norm(t_list = [tf.cast(tf.convert_to_tensor(tf.range(9)), dtype=tf.float32)], clip_norm=4)
  30. clipped_tensors, global_norm
  31. updates = tf.constant([9, 10, 11, 12])
  32. indices = tf.constant([[4], [2], [1], [6]])
  33. shape = tf.constant([8])
  34. tf.scatter_nd(indices, updates, shape)
  35. indices = tf.constant([[0], [2]])
  36. updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]], [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]]])
  37. shape = tf.constant([4, 4, 4])
  38. tf.scatter_nd(indices, updates, shape)
  39. x = tf.range(5)
  40. y = tf.range(6)
  41. points_x, points_y = tf.meshgrid(x, y)
  42. points = tf.stack([points_x, points_y], axis = 2)
  43. points
  1. """
  2. 数据加载
  3. outline:
  4. .keras.datasets
  5. .boston_housing.load_data() 波斯顿房价
  6. .mnist/fashion_mnist.load_data() 手写体/流行照片
  7. .cifar10/100.load_data() 模糊图像
  8. .imdb.load_data() 情感分类
  9. .tf.data.Dataset.from_tensor_slices 将输入的张量按照指定的维度进行切片,并将每个切片作为数据集中的一个元素。
  10. .shuffle
  11. .map
  12. .batch
  13. .repeat
  14. 1.  shuffle(10000) : 对数据集进行随机打乱,缓冲区大小为 10000。这有助于在训练过程中引入随机性,减少数据的顺序相关性。
  15. 2.  map(proprecession) : 对数据集中的每个元素应用自定义的预处理函数  proprecession  ,以进行数据的预处理操作,例如数据增强、归一化等。
  16. 3.  batch(128) : 将数据集分成大小为 128 的批次。这意味着模型在训练时每次处理 128 个样本。
  17. 4.  repeat(2) : 重复整个数据集 2 次。这在训练时可以让数据集被多次遍历。
  18. """
  19. # 数据预处理的方法
  20. import tensorflow as tf
  21. (x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
  22. def proprecession(x, y):
  23. x = tf.cast((x - tf.reduce_min(x)) / (tf.reduce_max(x) - tf.reduce_min(x)), dtype = tf.float32)
  24. y = tf.cast(y, dtype = tf.int32)
  25. return x, y
  26. train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train))
  27. train_db = train_db.shuffle(10000).map(proprecession).batch(128).repeat(2)
  28. test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
  29. test_db = test_db.shuffle(10000).map(proprecession).batch(128)

反向传播算法 

  1. import tensorflow as tf
  2. from tensorflow import keras
  3. from tensorflow.keras import datasets
  4. (x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
  5. tf.reduce_max(x_train), tf.reduce_min(x_train), x_train.shape, x_test.shape, tf.reduce_max(x_test), tf.reduce_min(x_test)
  6. # 预处理
  7. x_train = tf.convert_to_tensor(x_train, dtype = tf.float32)
  8. y_train = tf.convert_to_tensor(y_train, dtype = tf.int32)
  9. x_test = tf.convert_to_tensor(x_test, dtype = tf.float32)
  10. y_test = tf.convert_to_tensor(y_test, dtype = tf.int32)
  11. x_train = (x_train - tf.reduce_min(x_train)) / (tf.reduce_max(x_train) - tf.reduce_min(x_train)) # 最大最小值归一化
  12. x_test = (x_test - tf.reduce_mean(x_test)) / tf.sqrt(tf.math.reduce_variance(x_test)) #零均值归一化
  13. # y_test = tf.one_hot(y_test, depth = 10)
  14. train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(128)
  15. test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).shuffle(10000).batch(128)
  16. sample = next(iter(train_db))
  17. print("batch:", sample[0].shape, sample[1].shape)
  18. lr = 0.003
  19. w1 = tf.Variable(tf.random.truncated_normal([784, 256], mean = 0, stddev = 0.1))
  20. b1 = tf.Variable(tf.zeros([256]))
  21. w2 = tf.Variable(tf.random.truncated_normal([256, 128], mean = 0, stddev = 0.1))
  22. b2 = tf.Variable(tf.zeros([128]))
  23. w3 = tf.Variable(tf.random.truncated_normal([128, 10], mean = 0, stddev = 0.1))
  24. b3 = tf.Variable(tf.zeros([10]))
  25. total_correct = 0
  26. total_num = 0
  27. for epoch in range(10):
  28. for step, (x, y) in enumerate(train_db):
  29. x = tf.reshape(x, [-1, 28*28])
  30. with tf.GradientTape() as tape:
  31. h1 = x @ w1 + tf.broadcast_to(b1, [x.shape[0], 256]) # [128, 256] + [128, 256]
  32. h1 = tf.nn.relu(h1)
  33. h2 = h1 @ w2 + b2
  34. h2 = tf.nn.relu(h2)
  35. out = h2 @ w3 + b3
  36. y_onehot = tf.one_hot(y, depth = 10)
  37. # mean(sum((y - out) ** 2))
  38. loss = tf.square(y_onehot - out)
  39. loss = tf.reduce_mean(loss)
  40. grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
  41. w1.assign_sub(lr * grads[0]) # w1 - lr * grads[n]
  42. b1.assign_sub(lr * grads[1])
  43. w2.assign_sub(lr * grads[2])
  44. b2.assign_sub(lr * grads[3])
  45. w3.assign_sub(lr * grads[4])
  46. b3.assign_sub(lr * grads[5])
  47. if step % 100 == 0:
  48. print("epoch = ", epoch, "step = ", step, "loss:", float(loss))
  49. for step, (x, y) in enumerate(test_db):
  50. x = tf.reshape(x, [-1, 28 * 28])
  51. h1 = tf.nn.relu(x @ w1 + b1)
  52. h2 = tf.nn.relu(h1 @ w2 + b2)
  53. out = h2 @ w3 + b3
  54. prob = tf.nn.softmax(out, axis = 1)
  55. prob = tf.argmax(prob, axis = 1)
  56. prob = tf.cast(prob, dtype = tf.int32)
  57. correct = tf.cast(tf.equal(prob, y), dtype = tf.int32)
  58. correct = tf.reduce_sum(correct)
  59. total_correct += int(correct)
  60. total_num += x.shape[0]
  61. acc = total_correct / total_num
  62. print("test acc:", acc)

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/896706
推荐阅读
相关标签
  

闽ICP备14008679号