├── CNN ├── DCGAN ├── GPU模型 ├── Python, tensorflow 环境搭建 ├── RNN网络搭建 ├── Tensor2Tensor模型 ├── gym强化学习环境 ├── minst逻辑回归(keras) ├── tensorflow占位符 ├── tensorflow简介,常量,变量用法 ├── tf.data简介 ├── tf2.0 ├── tf2.0-keras ├── 保存和读取权重 ├── 共享名称,变量 ├── 卷积层 ├── 变分自动编码器 ├── 图像分类项目 ├── 图像数据增强 ├── 在移动设备上部署机器学习模型 ├── 基于注意力的翻译 ├── 强化学习 ├── 快速执行 ├── 池化层和全连接层 ├── 线性模型 ├── 自定义模型 ├── 词向量 ├── 迁移学习 └── 风格迁移 /CNN: -------------------------------------------------------------------------------- 1 | #请使用三种不同接口构建如下神经网络 2 | minst数据集 3 | 1.卷积层,5*5,输出通道:64 4 | 2.最大池化层,2*2 5 | 3.卷积层,3*3,输出通道:128 6 | 4.平均池化层,2*2 7 | 5.全连接层输出通道512 8 | 6.全连接层输出通道10 9 | 7.softmax层 10 | 11 | #1.keras系列 12 | import tensorflow as tf 13 | import time 14 | mnist = tf.keras.datasets.mnist 15 | 16 | #读取数据 17 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 18 | #归一化 19 | x_train, x_test = x_train / 255.0, x_test / 255.0 20 | 21 | #模型 22 | #1.keras.layers中函数的每个词首字母要大写,2D要大写 23 | #2.MaxPool2D 和 AveragePooling2D的区别 24 | #3.利用keras.models.Sequential,不需要在函数中加入输入 25 | #4.利用keras.models.Sequential,每一层之后要加逗号 26 | model = tf.keras.models.Sequential([ 27 | tf.keras.layers.Reshape((28,28,1)), 28 | tf.keras.layers.Conv2D(64, (5,5), activation=tf.keras.activations.relu), 29 | tf.keras.layers.MaxPool2D((2,2)), 30 | tf.keras.layers.Conv2D(128, (3,3), activation=tf.keras.activations.relu), 31 | tf.keras.layers.AveragePooling2D((2,2)), 32 | tf.keras.layers.Flatten(), 33 | tf.keras.layers.Dense(512, activation=tf.keras.activations.relu), 34 | tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax) 35 | ]) 36 | 37 | 38 | #选取训练参数 39 | model.compile(optimizer='SGD', 40 | loss='sparse_categorical_crossentropy', 41 | metrics=['accuracy']) 42 | #训练 43 | now=time.time() 44 | model.fit(x_train, y_train, epochs=5) 45 | #测试 46 | model.evaluate(x_test, y_test) 47 | print(time.time()-now) 48 | 49 | #tf.nn 50 | from tensorflow.examples.tutorials.mnist import input_data 51 | import numpy as np 52 | import tensorflow as tf 53 | import time 54 | 55 | data_dir = './data/' 56 | mnist = input_data.read_data_sets(data_dir, one_hot=True) 57 | 58 | #每批次数据集的大小 59 | #5epoch=5*60000/100=3000steps 60 | BATCH_SIZE = 100 61 | 62 | #学习率 63 | LEARNING_RATE_INIT = 1e-2 64 | x = tf.placeholder(tf.float32, [None, 784]) 65 | y_ = tf.placeholder(tf.float32, [None, 10]) 66 | 67 | #对输入向量x转换成图像矩阵形式 68 | with tf.variable_scope('reshape'): 69 | x_image = tf.reshape(x, [-1, 28, 28, 1]) 70 | 71 | #模型: 72 | #1.需要自定义变量,初始化 73 | #2.重要参数不可省略 74 | #3.激活函数单独一层 75 | #卷积层1 76 | with tf.variable_scope('conv1'): 77 | conv_1_w = tf.Variable(tf.truncated_normal([5,5,1,64], stddev=0.1)) 78 | conv_1_b = tf.Variable(tf.constant(0.1, shape=[64])) 79 | conv_1_l = tf.nn.conv2d(x_image, conv_1_w, strides=[1,1,1,1], 80 | padding='SAME') + conv_1_b 81 | conv_1_h = tf.nn.relu(conv_1_l) 82 | 83 | #池化层1 84 | with tf.variable_scope('pool1'): 85 | pool_1_h = tf.nn.max_pool(conv_1_h, ksize=[1,2,2,1], strides=[1,2,2,1], 86 | padding='SAME') 87 | 88 | #卷积层2 89 | with tf.variable_scope('conv2'): 90 | conv_2_w = tf.Variable(tf.truncated_normal([3,3,64,128], stddev=0.1)) 91 | conv_2_b = tf.Variable(tf.constant(0.1, shape=[128])) 92 | conv_2_l = tf.nn.conv2d(pool_1_h, conv_2_w, strides=[1,1,1,1], 93 | padding='SAME') + conv_2_b 94 | conv_2_h = tf.nn.relu(conv_2_l) 95 | 96 | #池化层2 97 | with tf.name_scope('pool2'): 98 | pool_2_h = tf.nn.avg_pool(conv_2_h, ksize=[1,2,2,1], strides=[1,2,2,1], 99 | padding='SAME') 100 | 101 | #全连接层1 102 | with tf.name_scope('fc1'): 103 | fc_1_w = tf.Variable(tf.truncated_normal([7*7*128, 512], stddev=0.1)) 104 | fc_1_b = tf.Variable(tf.constant(0.1, shape=[512])) 105 | #全连接层的输入为向量,而池化层2的输出为7x7x128的矩阵,所以这里要将矩阵转化成一个向量 106 | pool_2_h_flat = tf.reshape(pool_2_h, [-1,7*7*128]) 107 | fc_1_h = tf.nn.relu(tf.matmul(pool_2_h_flat, fc_1_w) + fc_1_b) 108 | 109 | #全连接层2 And 输出层 110 | with tf.name_scope('fc2'): 111 | fc_2_w = tf.Variable(tf.truncated_normal([512,10], stddev=0.1)) 112 | fc_2_b = tf.Variable(tf.constant(0.1, shape=[10])) 113 | y = tf.matmul(fc_1_h, fc_2_w) + fc_2_b 114 | 115 | #交叉熵 116 | cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2 117 | (labels=y_, logits=y)) 118 | 119 | #代价函数 120 | total_loss = cross_entropy 121 | 122 | #定义优化器 123 | opt=tf.train.GradientDescentOptimizer(LEARNING_RATE_INIT) 124 | train_step = opt.minimize(total_loss) 125 | 126 | with tf.Session() as sess: 127 | now = time.time() 128 | init_op = tf.global_variables_initializer() 129 | sess.run(init_op) 130 | #训练 131 | for step in range(3000): 132 | batch_xs, batch_ys = mnist.train.next_batch(BATCH_SIZE) 133 | _, loss = sess.run([train_step, total_loss], 134 | feed_dict={x: batch_xs, y_: batch_ys}) 135 | if step%600==0: 136 | print(step, ':', loss) 137 | 138 | #预测 139 | correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 140 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 141 | test_accuracy = accuracy.eval( 142 | feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 143 | print(test_accuracy) 144 | print(time.time() - now) 145 | 146 | #3.contrib系列 147 | from tensorflow.examples.tutorials.mnist import input_data 148 | import numpy as np 149 | import tensorflow as tf 150 | import time 151 | 152 | data_dir = './data/' 153 | mnist = input_data.read_data_sets(data_dir, one_hot=True) 154 | 155 | #每批次数据集的大小 156 | #5epoch=5*60000/100=3000steps 157 | BATCH_SIZE = 100 158 | 159 | #学习率 160 | LEARNING_RATE_INIT = 1e-2 161 | x = tf.placeholder(tf.float32, [None, 784]) 162 | y_ = tf.placeholder(tf.float32, [None, 10]) 163 | 164 | # 模型 165 | #1.注意池化层默认padding为‘valid’ 166 | #2.默认激活函数为relu 167 | 168 | def fcn(images, is_training=True): 169 | _ = tf.reshape(images, [-1,28, 28, 1]) 170 | _ = tf.contrib.layers.conv2d(_, 64, (5, 5)) 171 | _ = tf.contrib.layers.max_pool2d(_, (2, 2), 2, 'SAME') 172 | _ = tf.contrib.layers.conv2d(_, 128, (3, 3)) 173 | _ = tf.contrib.layers.avg_pool2d(_, (2, 2), 2, 'SAME') 174 | _ = tf.reshape(_,[-1, 7*7*128]) 175 | _ = tf.contrib.layers.fully_connected(_, 512) 176 | _ = tf.contrib.layers.fully_connected(_, 10, activation_fn=None) 177 | return _ 178 | 179 | y=fcn(x) 180 | #交叉熵 181 | cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2 182 | (labels=y_, logits=y)) 183 | 184 | #代价函数 185 | total_loss = cross_entropy 186 | 187 | #定义优化器 188 | opt=tf.train.GradientDescentOptimizer(LEARNING_RATE_INIT) 189 | train_step = opt.minimize(total_loss) 190 | 191 | 192 | with tf.Session() as sess: 193 | now=time.time() 194 | init_op = tf.global_variables_initializer() 195 | sess.run(init_op) 196 | #训练 197 | for step in range(3000): 198 | batch_xs, batch_ys = mnist.train.next_batch(BATCH_SIZE) 199 | _, loss = sess.run([train_step, total_loss], 200 | feed_dict={x: batch_xs, y_: batch_ys}) 201 | if step%600==0: 202 | print(step,':',loss) 203 | 204 | #预测 205 | correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 206 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 207 | test_accuracy = accuracy.eval( 208 | feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 209 | print(test_accuracy) 210 | print(time.time() - now) 211 | -------------------------------------------------------------------------------- /DCGAN: -------------------------------------------------------------------------------- 1 | import glob 2 | import imageio 3 | import matplotlib.pyplot as plt 4 | import numpy as np 5 | import os 6 | import PIL 7 | from tensorflow.keras import layers 8 | import time 9 | 10 | from IPython import display 11 | 12 | (train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data() 13 | 14 | train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32') 15 | train_images = (train_images - 127.5) / 127.5 # 将图片标准化到 [-1, 1] 区间内 16 | 17 | BUFFER_SIZE = 60000 18 | BATCH_SIZE = 256 19 | 20 | # 批量化和打乱数据 21 | train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(BUFFER_SIZE).batch(BATCH_SIZE) 22 | 23 | #生成器 24 | def make_generator_model(): 25 | model = tf.keras.Sequential() 26 | model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,))) 27 | model.add(layers.BatchNormalization()) 28 | model.add(layers.LeakyReLU()) 29 | 30 | model.add(layers.Reshape((7, 7, 256))) 31 | assert model.output_shape == (None, 7, 7, 256) # 注意:batch size 没有限制 32 | 33 | model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False)) 34 | assert model.output_shape == (None, 7, 7, 128) 35 | model.add(layers.BatchNormalization()) 36 | model.add(layers.LeakyReLU()) 37 | 38 | model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False)) 39 | assert model.output_shape == (None, 14, 14, 64) 40 | model.add(layers.BatchNormalization()) 41 | model.add(layers.LeakyReLU()) 42 | 43 | model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh')) 44 | assert model.output_shape == (None, 28, 28, 1) 45 | 46 | return model 47 | 48 | generator = make_generator_model() 49 | 50 | noise = tf.random.normal([1, 100]) 51 | generated_image = generator(noise, training=False) 52 | 53 | plt.imshow(generated_image[0, :, :, 0], cmap='gray') 54 | 55 | #判别器 56 | def make_discriminator_model(): 57 | model = tf.keras.Sequential() 58 | model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', 59 | input_shape=[28, 28, 1])) 60 | model.add(layers.LeakyReLU()) 61 | model.add(layers.Dropout(0.3)) 62 | 63 | model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same')) 64 | model.add(layers.LeakyReLU()) 65 | model.add(layers.Dropout(0.3)) 66 | 67 | model.add(layers.Flatten()) 68 | model.add(layers.Dense(1)) 69 | 70 | return model 71 | 72 | discriminator = make_discriminator_model() 73 | decision = discriminator(generated_image) 74 | print (decision) 75 | 76 | # 该方法返回计算交叉熵损失的辅助函数 77 | cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True) 78 | 79 | #生成器损失 80 | def generator_loss(fake_output): 81 | return cross_entropy(tf.ones_like(fake_output), fake_output) 82 | 83 | #判别器损失 84 | def discriminator_loss(real_output, fake_output): 85 | real_loss = cross_entropy(tf.ones_like(real_output), real_output) 86 | fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output) 87 | total_loss = real_loss + fake_loss 88 | return total_loss 89 | 90 | generator_optimizer = tf.keras.optimizers.Adam(1e-4) 91 | discriminator_optimizer = tf.keras.optimizers.Adam(1e-4) 92 | 93 | checkpoint_dir = './training_checkpoints' 94 | checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt") 95 | checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer, 96 | discriminator_optimizer=discriminator_optimizer, 97 | generator=generator, 98 | discriminator=discriminator) 99 | 100 | EPOCHS = 50 101 | noise_dim = 100 102 | num_examples_to_generate = 16 103 | 104 | def train_step(images): 105 | noise = tf.random.normal([BATCH_SIZE, noise_dim]) 106 | 107 | with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape: 108 | generated_images = generator(noise, training=True) 109 | 110 | real_output = discriminator(images, training=True) 111 | fake_output = discriminator(generated_images, training=True) 112 | 113 | gen_loss = generator_loss(fake_output) 114 | disc_loss = discriminator_loss(real_output, fake_output) 115 | 116 | gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables) 117 | gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables) 118 | 119 | generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables)) 120 | discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables)) 121 | 122 | def train(dataset, epochs): 123 | for epoch in range(epochs): 124 | start = time.time() 125 | 126 | for image_batch in dataset: 127 | train_step(image_batch) 128 | 129 | # 每 15 个 epoch 保存一次模型 130 | if (epoch + 1) % 15 == 0: 131 | checkpoint.save(file_prefix = checkpoint_prefix) 132 | 133 | print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start)) 134 | 135 | # 最后一个 epoch 结束后生成图片 136 | display.clear_output(wait=True) 137 | generate_and_save_images(generator, 138 | epochs, 139 | seed) 140 | def generate_and_save_images(model, epoch, test_input): 141 | # 注意 training` 设定为 False 142 | # 因此,所有层都在推理模式下运行(batchnorm)。 143 | predictions = model(test_input, training=False) 144 | 145 | fig = plt.figure(figsize=(4,4)) 146 | 147 | for i in range(predictions.shape[0]): 148 | plt.subplot(4, 4, i+1) 149 | plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray') 150 | plt.axis('off') 151 | 152 | plt.savefig('image_at_epoch_{:04d}.png'.format(epoch)) 153 | plt.show() 154 | 155 | train(train_dataset, EPOCHS) 156 | 157 | -------------------------------------------------------------------------------- /GPU模型: -------------------------------------------------------------------------------- 1 | GPU支持 2 | 多GPU模型 3 | 4 | 硬件要求: 5 | 英伟达显卡 6 | https://developer.nvidia.com/cuda-gpus 7 | 软件要求: 8 | NVIDIA GPU驱动程序 9 | https://www.nvidia.com/Download/index.aspx?lang=en-us 10 | CUDA 11 | 对通用GPU计算操作做了封装,方便其他组件调用 12 | https://developer.nvidia.com/cuda-zone 13 | 14 | 15 | !pip install tensorflow-gpu 16 | 17 | 18 | #查看gpu 19 | import tensorflow as tf 20 | gpu_device_name = tf.test.gpu_device_name() 21 | print(gpu_device_name) 22 | 23 | #查看可用的gpu数量 24 | from __future__ import absolute_import, division, print_function, unicode_literals 25 | 26 | import tensorflow as tf 27 | print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) 28 | 29 | from tensorflow.python.client import device_lib 30 | 31 | # 列出所有的本地机器设备 32 | local_device_protos = device_lib.list_local_devices() 33 | print(local_device_protos) 34 | 35 | #限制显存使用 36 | import tensorflow as tf 37 | 38 | # 设置显存使用上限 39 | gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7) 8G*0.7=5.6G 40 | sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) 41 | 42 | # 按需取用 43 | gpu_options = tf.GPUOptions(allow_growth=True) 4G 6G 44 | sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) 45 | 46 | 47 | #keras 48 | import tensorflow as tf 49 | from keras.applications import Xception 50 | from keras.utils import multi_gpu_model 51 | 52 | #cpu 53 | with tf.device('/device:CPU:0'): 54 | model= Xception(weights=None, input_shape=(w,h,3), classes=100) 55 | 56 | #单个gpu 57 | with tf.device('/device:GPU:0'): 58 | model= Xception(weights=None, input_shape=(w,h,3), classes=100) 59 | 60 | #多个gpu 61 | model1=multi_gpu_model(model , gpus=8) 62 | model1.compile(loss='crossentropy',optimizer='adam') 63 | 64 | #训练 65 | model1.fit(x,y,batch_size=256) 66 | #单个gpu的batch_size=256/8=32 67 | 68 | -------------------------------------------------------------------------------- /Python, tensorflow 环境搭建: -------------------------------------------------------------------------------- 1 | windows 2 | 1.打开Python官网,点download中的windows 打开WEB浏览器访问http://www.python.org/download/ 3 | 2.下载exe后缀的可执行文件,根据自己系统选择32位还是64位 4 | 3. 安装时要记得勾上add python to Path 选项,意思是把Python的安装路径添加到系统环境变量的Path变量中 5 | 4.根据需求安装 6 | 7 | 8 | 环境变量配置 9 | 一般装完直接可以从cmd中访问 10 | 11 | 如果没有: 12 | 首先需要在系统中注册python环境变量: 13 | 假设python的安装路径为c:\python3.6, 14 | 则修改我的电脑->属性->高级->环境变量->系统变量中的PATH为: 15 | PATH=PATH;c:\python36 16 | 17 | 安装tensorflow 18 | pip install --upgrade --ignore-installed tensorflow 19 | pip install --upgrade --ignore-installed tensorflow-gpu 20 | python –m pip install --upgrade --ignore-installed tensorflow 21 | 22 | 23 | linux 24 | 1. 安装依赖环境 25 | # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel 26 | 2.下载Python3 27 | 官网 28 | # wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz 29 | 3. 安装python3 30 | # mkdir -p /usr/local/python3 31 | # tar -zxvf Python-3.6.1.tgz 32 | # cd Python-3.6.1 33 | # ./configure --prefix=/usr/local/python3 34 | # make 35 | #make install 36 | 5.建立python3的软链 37 | # ln -s /usr/local/python3/bin/python3 /usr/bin/python3 38 | 6.将/usr/local/python3/bin加入PATH 39 | # vim ~/.bash_profile 40 | # .bash_profile 41 | # Get the aliases and functions 42 | if [ -f ~/.bashrc ]; then 43 | . ~/.bashrc 44 | Fi 45 | # User specific environment and startup programs 46 | PATH=$PATH:$HOME/bin:/usr/local/python3/bin 47 | export PATH 48 | 按ESC,输入:wq 回车退出。 49 | # source ~/.bash_profile 50 | 安装pip 51 | 1、安装pip前需要前置安装setuptools 52 | wget --no-check-certificate   53 | https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26 54 | tar -zxvf setuptools-19.6.tar.gz 55 | cd setuptools-19.6 56 | python3 setup.py build 57 | python3 setup.py install 58 | 2.安装pip 59 | wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb 60 | tar -zxvf pip-8.0.2.tar.gz 61 | cd pip-8.0.2 62 | python3 setup.py build 63 | python3 setup.py install 64 | 安装tensorflow 65 | 66 | 最新版本的二进制文件链接地址,可以通过TensorFlow官网获取 67 | https://www.tensorflow.org/install/install_linux 68 | 2. Pip 安装 69 | pip install tensorflow==2.0.0-alpha0 70 | ip install tensorflow 71 | 72 | Python 编译器 73 | 74 | 1.Cmd 75 | 76 | 2. Vim 77 | 78 | 3. pycharm python集成开发环境, 有免费版 79 | 80 | 4. spyder3 81 | 82 | 5.colab 83 | 84 | 85 | -------------------------------------------------------------------------------- /RNN网络搭建: -------------------------------------------------------------------------------- 1 | #rnn网络单元 2 | tf.nn.rnn_cell 3 | tf.keras.layers.RNN 4 | 5 | tf.nn.rnn_cell.BasicRNNCell#最基本的rnn单元 6 | ( 7 | num_units, 8 | activation=None,#默认tanh,使用tf.nn.relu效果更好 9 | reuse=None, 10 | name=None, 11 | dtype=None, 12 | **kwargs 13 | ) 14 | tf.nn.rnn_cell.RNNCell#抽象rnn单元 15 | ( 16 | trainable=True, 17 | name=None, 18 | dtype=None, 19 | **kwargs 20 | ) 21 | tf.nn.rnn_cell.LSTMCell#LSTM单元 22 | ( 23 | num_units, 24 | use_peepholes=False,#是否使用窥视孔 25 | cell_clip=None,#浮点值,对输出进行修剪 26 | initializer=None, 27 | num_proj=None,#整型,投影矩阵的输出维数 28 | proj_clip=None, 29 | num_unit_shards=None,#已被移除 30 | num_proj_shards=None,#已被移除 31 | forget_bias=1.0, 32 | state_is_tuple=True, 33 | activation=None, 34 | reuse=None, 35 | name=None, 36 | dtype=None, 37 | **kwargs 38 | ) 39 | tf.nn.rnn_cell.GRUCell#GRU单元 40 | ( 41 | num_units, 42 | activation=None, 43 | reuse=None, 44 | kernel_initializer=None, 45 | bias_initializer=None, 46 | name=None, 47 | dtype=None, 48 | **kwargs 49 | ) 50 | 51 | #多个单元 52 | layers = [tf.nn.rnn_cell.GRUCell(size) for size in hidden_sizes] 53 | cells = tf.nn.rnn_cell.MultiRNNCell(layers) 54 | 55 | #搭建rnn 56 | f.nn.dynamic_rnn 57 | tf.nn.bidirectional_dynamic_rnn 58 | 59 | #整体过程 60 | hidden_sizes=[] 61 | layers = [tf.nn.rnn_cell.GRUCell(size) for size in hidden_sizes] 62 | cells = tf.nn.rnn_cell.MultiRNNCell(layers) 63 | output, out_state = tf.nn.dynamic_rnn(cell, seq, length, initial_state) 64 | 65 | #代码 66 | import tensorflow as tf 67 | 68 | batch_size=64 69 | hidden_size=64 70 | max_time=50 71 | input_data=tf.placeholder(tf.float32,shape=(batch_size,max_time,64)) 72 | # 创建单元 73 | rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size) 74 | 75 | # 初始化 76 | initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32) 77 | 78 | # 'state':[batch_size, cell_state_size] 79 | # ‘outputs’:[batch_size, max_time, cell_state_size] 80 | outputs, state = tf.nn.dynamic_rnn(rnn_cell, input_data, 81 | initial_state=initial_state, 82 | dtype=tf.float32) 83 | -------------------------------------------------------------------------------- /Tensor2Tensor模型: -------------------------------------------------------------------------------- 1 | Tensor2Tensor 2 | 3 | 1.模型易于更改,泛化性高 4 | 2.利于GPU、TPU加速,分布式计算 5 | 6 | 应用: 7 | 翻译 8 | 图像分类 9 | 语音识别 10 | 11 | 以下均为命令行命令 12 | #普通模式 13 | !pip install tensor2tensor 14 | #gpu 模式 15 | !pip install tensor2tensor[tensorflow_gpu] 16 | 17 | #需要注意 tensor2tensor 与 tensorflow的版本兼容性 18 | #一般需要在tensorflow 1.6.0以前 19 | #获取数据 20 | !t2t-datagen \ 21 | --data_dir=/t2t_data \ 22 | --tmp_dir=/tmp/t2t_datagen \ 23 | --problem=translate_enzh_wmt32k 24 | 25 | # 训练 26 | !t2t-trainer \ 27 | --data_dir=/t2t_data \ 28 | --problem=translate_enzh_wmt32k \ 29 | --model=transformer \ 30 | --hparams_set=transformer_base_single_gpu \ 31 | --output_dir=/t2t_train 32 | 33 | # 解码 34 | 35 | !DECODE_FILE=/t2t_data/decode_this.txt 36 | !echo "Hello world" >> $DECODE_FILE 37 | !echo "Goodbye world" >> $DECODE_FILE 38 | !echo -e 'Hallo Welt\nAuf Wiedersehen Welt' > ref-translation.de 39 | 40 | !BEAM_SIZE=4 41 | !ALPHA=0.6 42 | 43 | !t2t-decoder \ 44 | --data_dir=/t2t_data \ 45 | --problem=translate_enzh_wmt32k \ 46 | --model=transformer \ 47 | --hparams_set=transformer_base_single_gpu \ 48 | --output_dir=/t2t_train \ 49 | --decode_hparams="beam_size=$BEAM_SIZE,alpha=$ALPHA" \ 50 | --decode_from_file=$DECODE_FILE \ 51 | --decode_to_file=translation.en 52 | 53 | #自定义问题 54 | from tensor2tensor.utils import registry 55 | from tensor2tensor.data_generators import problem, text_problems 56 | 57 | #自定义的problem一定要加该装饰器,装饰器将方法变为属性,使其可以直接赋值 58 | @registry.register_problem 59 | class MyProblem(text_problems.Text2TextProblem): 60 | @property 61 | def approx_vocab_size(self): 62 | return 100 63 | 64 | @property 65 | def is_generate_per_split(self): 66 | return True 67 | 68 | @property 69 | def dataset_splits(self): 70 | return [{ 71 | "split": problem.DatasetSplit.TRAIN, 72 | "shards": 5, 73 | }, { 74 | "split": problem.DatasetSplit.EVAL, 75 | "shards": 5, 76 | }] 77 | 78 | def generate_samples(self, data_dir, tmp_dir, dataset_split): 79 | del data_dir 80 | del tmp_dir 81 | del dataset_split 82 | #读取原始的训练样本数据 83 | questions = open("./rawdata/questions.txt", "r") 84 | anwsers = open("./rawdata/anwsers.txt", "r") 85 | 86 | comment_list = questions.readlines() 87 | tag_list = anwsers.readlines() 88 | questions.close() 89 | anwsers.close() 90 | for comment, tag in zip(comment_list, tag_list): 91 | comment = comment.strip() 92 | tag = tag.strip() 93 | yield { 94 | "inputs": comment, 95 | "targets": tag 96 | } 97 | -------------------------------------------------------------------------------- /gym强化学习环境: -------------------------------------------------------------------------------- 1 | Gym构建自定义强化学习环境 2 | 3 | Gym开源库:测试问题的集合。 4 | Gym服务:提供一个站点和api,允许用户对他们的测试结果进行比较。 5 | 6 | Gym的核心接口是Env,包含下面几个核心方法: 7 | reset(self):重置环境的状态,返回观察。 8 | step(self, action):推进一个时间步长,返回observation,reward 9 | render(self, mode=’’, close=False):重绘环境的一帧。 10 | 11 | #gym安装,至少需要python 2.7 或者 python 3.5 版本 12 | !pip install gym 13 | 14 | import random 15 | import gym 16 | 17 | class Env1(gym.Env): 18 | metadata = { 19 | 'render.modes': ['human', 'rgb_array'], 20 | 'video.frames_per_second': 2 21 | } 22 | 23 | def __init__(self): 24 | #状态空间 25 | self.states = [[0,0],[0,1],[1,0],[1,1]] 26 | 27 | #终止状态为字典格式 28 | self.terminate_states = dict() 29 | self.terminate_states[0] = 1 30 | 31 | #动作空间 32 | self.actions = ['n','e','s','w'] 33 | 34 | #回报的数据结构为字典 35 | self.rewards = dict(); 36 | self.rewards['0'] = 1.0 37 | 38 | #状态转移的数据格式为字典 39 | self.t = dict(); 40 | self.t['0_s'] = 1 41 | self.t['0_e'] = 1 42 | self.t['1_w'] = 1 43 | self.t['1_s'] = 2 44 | self.t['2_n'] = 1 45 | self.t['2_e'] = 3 46 | self.t['3_n'] = 1 47 | self.t['3_w'] = 1 48 | 49 | #折扣因子 50 | self.gamma = 0.9 51 | 52 | self.viewer = None 53 | self.state = None 54 | 55 | def _seed(self, seed=None): 56 | self.np_random, seed = random.seeding.np_random(seed) 57 | return [seed] 58 | 59 | def getTerminal(self): 60 | return self.terminate_states 61 | 62 | def getGamma(self): 63 | return self.gamma 64 | 65 | def getStates(self): 66 | return self.states 67 | 68 | def getAction(self): 69 | return self.actions 70 | 71 | def getTerminate_states(self): 72 | return self.terminate_states 73 | 74 | def setAction(self,s): 75 | self.state=s 76 | 77 | def step(self, action): 78 | #系统当前状态 79 | state = self.state 80 | if state in self.terminate_states: 81 | return state, 0, True, {} 82 | 83 | #状态转移 84 | if key in self.t: 85 | next_state = self.t[key] 86 | else: 87 | next_state = state 88 | self.state = next_state 89 | 90 | is_terminal = False 91 | 92 | if next_state in self.terminate_states: 93 | is_terminal = True 94 | 95 | if key not in self.rewards: 96 | r = 0.0 97 | else: 98 | r = self.rewards[key] 99 | 100 | return next_state, r, is_terminal,{} 101 | 102 | def reset(self): 103 | self.state = self.states[int(random.random() * len(self.states))] 104 | return self.state 105 | 106 | def render(self, mode='human'): 107 | from gym.envs.classic_control import rendering 108 | screen_width = 400 109 | screen_height = 400 110 | 111 | if self.viewer is None: 112 | 113 | self.viewer = rendering.Viewer(screen_width, screen_height) 114 | 115 | #创建网格世界 116 | self.line1 = rendering.Line((100,100),(300,100)) 117 | self.line2 = rendering.Line((100, 200), (300, 200)) 118 | self.line3 = rendering.Line((100, 300), (300, 300)) 119 | self.line4 = rendering.Line((100, 100), (100, 300)) 120 | self.line5 = rendering.Line((200, 100), (200, 300)) 121 | self.line6 = rendering.Line((300, 100), (300, 300)) 122 | 123 | #创建机器人 124 | self.robot= rendering.make_circle(30) 125 | self.robotrans = rendering.Transform() 126 | self.robot.add_attr(self.robotrans) 127 | self.robot.set_color(0, 1, 0) 128 | 129 | #加入viewer 130 | self.viewer.add_geom(self.line1) 131 | self.viewer.add_geom(self.line2) 132 | self.viewer.add_geom(self.line3) 133 | self.viewer.add_geom(self.line4) 134 | self.viewer.add_geom(self.line5) 135 | self.viewer.add_geom(self.line6) 136 | self.viewer.add_geom(self.robot) 137 | 138 | if self.state is None: 139 | return None 140 | 141 | self.robotrans.set_translation(self.state) 142 | 143 | return self.viewer.render(return_rgb_array=mode == 'rgb_array') 144 | 145 | def close(self): 146 | if self.viewer: 147 | self.viewer.close() 148 | 149 | #对环境进行注册 150 | from gym.envs.user.grid_mdp_v1 import Env1 151 | register( 152 | id='v1', 153 | entry_point='gym.envs.user:Env1', 154 | max_episode_steps=200, 155 | reward_threshold=100.0, 156 | ) 157 | 158 | #对环境进行使用 159 | import gym 160 | 161 | env = gym.make('v1') 162 | env.reset() 163 | env.render() 164 | env.close() 165 | -------------------------------------------------------------------------------- /minst逻辑回归(keras): -------------------------------------------------------------------------------- 1 | #MNIST Database 2 | #手写数字0—9 3 | #每张图是28*28黑白图 4 | #训练集: 55,000张 5 | #验证集: 5,000张 6 | #测试集: 10,000 张 7 | 8 | 9 | import tensorflow as tf 10 | mnist = tf.keras.datasets.mnist 11 | 12 | #读取数据 13 | (x_train, y_train),(x_test, y_test) = mnist.load_data() 14 | #归一化 15 | x_train, x_test = x_train / 255.0, x_test / 255.0 16 | 17 | #单层逻辑回归模型y=softmax(wx+b) 18 | model = tf.keras.models.Sequential([ 19 | tf.keras.layers.Flatten(input_shape=(28, 28)), 20 | tf.keras.layers.Dense(10, activation=tf.nn.softmax) 21 | ]) 22 | #优化器:adam 23 | #损失函数:交叉熵 24 | model.compile(optimizer='adam', 25 | loss='sparse_categorical_crossentropy', 26 | metrics=['accuracy']) 27 | #模型训练 28 | model.fit(x_train, y_train, epochs=5) 29 | #模型测试 30 | model.evaluate(x_test, y_test) 31 | -------------------------------------------------------------------------------- /tensorflow占位符: -------------------------------------------------------------------------------- 1 | Placeholders占位符 2 | 在不知道变量值的情况下,提前设置graph 3 | 4 | tf.placeholder(dtype, shape=None, name=None) 5 | 对比 6 | tf.constant( 7 |    value, 8 |    dtype=None, 9 |    shape=None, 10 |    name='Const' 11 | ) 12 | 13 | #如何在占位符输入值 14 | #用一个字典 15 | #feed_dict={key:value} 16 | 17 | a = tf.placeholder(tf.float32, shape=[3]) 18 | 19 | b = tf.constant([5, 5, 5], tf.float32) 20 | 21 | c = a + b 22 | 23 | with tf.Session() as sess: 24 | print(sess.run(c, feed_dict={a: [1, 2, 3]})) 25 | #第二种方法 26 | with tf.Session() as sess: 27 | print(sess.run(c, {a: [1, 2, 3]})) 28 | 29 | 如何输入多个数据点? 30 | 31 | with tf.Session() as sess: 32 | for a_value in list_of_values_for_a: 33 | print(sess.run(c, {a: a_value})) 34 | 35 | 不只针对变量,在其他函数中也可以feed数据 36 | a = tf.add(2, 5) 37 | b = tf.multiply(a, 3) 38 | 39 | with tf.Session() as sess: 40 | print(sess.run(b, feed_dict={a: 15})) 41 | 42 | -------------------------------------------------------------------------------- /tensorflow简介,常量,变量用法: -------------------------------------------------------------------------------- 1 | TensorFlow是一个基于数据流编程(dataflow programming)的符号数学系统,被广泛应用于各类机器学习(machine learning)算法的编程实现,其前身是谷歌的神经网络算法库DistBelief 。 2 | Tensorflow拥有多层级结构,可部署于各类服务器、PC终端和网页并支持GPU和TPU高性能数值计算,被广泛应用于谷歌内部的产品开发和各领域的科学研究 3 | TensorFlow由谷歌人工智能团队谷歌大脑(Google Brain)开发和维护,拥有包括TensorFlow Hub、TensorFlow Lite、TensorFlow Research Cloud在内的多个项目以及各类应用程序接口(Application Programming Interface, API) [2] 。自2015年11月9日起,TensorFlow依据阿帕奇授权协议(Apache 2.0 open source license)开放源代码 4 | 5 | 高阶接口 6 | Keras 7 | TFLearn 8 | Sonnet 9 | 10 | 计算图方法(graph) 11 | 第1阶段: 创建graph 12 | 第2阶段: 使用会话(session)在graph中执行操作 13 | 14 | 常量用法: 15 | import tensorflow as tf 16 | a = tf.constant(2) 17 | b = tf.constant(3) 18 | x = tf.add(a, b) 19 | with tf.Session() as sess: 20 | print(sess.run(x)) 21 | 22 | 保存图 23 | writer = tf.summary.FileWriter('./graphs', tf.get_default_graph()) 24 | with tf.Session() as sess: 25 | #第二种保存方法 26 | # writer = tf.summary.FileWriter('./graphs', sess.graph) 27 | print(sess.run(x)) 28 | #用完关闭 29 | writer.close() 30 | 31 | 利用tensorboard对上面保存的图进行可视化 32 | tensorboard --logdir="./graphs" --port 6006 33 | python –m tensorboard.main --logdir="./graphs" --port 6006 34 | 35 | 修改tensor名称 36 | a = tf.constant(2, name='a') 37 | b = tf.constant(3, name='b') 38 | x = tf.add(a, b, name='add') 39 | 40 | 变量定义方法(两种) 41 | s = tf.Variable(2, name="scalar") 42 | m = tf.Variable([[0, 1], [2, 3]], name="matrix") 43 | W = tf.Variable(tf.zeros([784,10])) 44 | 45 | s = tf.get_variable("scalar", initializer=tf.constant(2)) 46 | m = tf.get_variable("matrix", initializer=tf.constant([[0, 1], [2, 3]])) 47 | W = tf.get_variable("big_matrix", shape=(784, 10), initializer=tf.zeros_initializer()) 48 | 49 | 使用变量前先要初始化 50 | with tf.Session() as sess: 51 | sess.run(tf.global_variables_initializer())#所有变量都初始化 52 | 53 | W = tf.Variable(tf.zeros([784,10])) 54 | with tf.Session() as sess: 55 | sess.run(W.initializer)#只初始化某个变量 56 | print(W) 57 | 输出值 58 | print(W.eval()) 59 | 60 | 给变量赋值 61 | tf.Variable.assign() 62 | W = tf.Variable(10) 63 | assign_op = W.assign(100) 64 | with tf.Session() as sess: 65 | sess.run(W.initializer) 66 | print(W.eval()) 67 | sess.run(assign_op) 68 | print(W.eval()) 69 | 70 | #使用运算赋值 71 | 72 | import tensorflow as tf 73 | my_var = tf.Variable(2, name="my_var") 74 | 75 | # 乘2运算 76 | my_var_times_two = my_var.assign(2 * my_var) 77 | 78 | with tf.Session() as sess: 79 | sess.run(my_var.initializer) 80 | sess.run(my_var_times_two) # >> the value of my_var now is 4 81 | print(my_var.eval()) 82 | sess.run(my_var_times_two) # >> the value of my_var now is 8 83 | print(my_var.eval()) 84 | sess.run(my_var_times_two) # >> the value of my_var now is 16 85 | print(my_var.eval()) 86 | 87 | #每个会话独自保存计算结果 88 | 89 | import tensorflow as tf 90 | W = tf.Variable(10) 91 | 92 | sess1 = tf.Session() 93 | sess2 = tf.Session() 94 | 95 | sess1.run(W.initializer) 96 | sess2.run(W.initializer) 97 | 98 | print(sess1.run(W.assign_add(10))) # >> 20 99 | print(sess2.run(W.assign_sub(2))) # >> 8 100 | 101 | print(sess1.run(W.assign_add(100))) 102 | print(sess2.run(W.assign_sub(50))) 103 | 104 | sess1.close() 105 | sess2.close() 106 | 107 | 108 | 控制操作顺序 109 | tf.Graph.control_dependencies(control_inputs) 110 | 111 | g = tf.get_default_graph() 112 | #g中有a,b,c,de 113 | with g.control_dependencies([a, b, c]): 114 | d = ... 115 | e = … 116 | #先进行a,b,c操作 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /tf.data简介: -------------------------------------------------------------------------------- 1 | #tensorflow 数据处理 2 | #minist数据集进行逻辑回归 3 | 4 | #能否不用占位符(placeholder)喂数据 5 | tf.data 6 | tf.data.Dataset 7 | tf.data.Iterator 8 | #创建数据集 9 | tf.data.Dataset.from_tensors((features, labels)) 10 | tf.data.Dataset.from_tensor_slices((features, labels)) 11 | tf.data.Dataset.from_generator(gen, output_types, output_shapes) 12 | 13 | tf.data.TextLineDataset(filenames)#包含多个txt文件的行 14 | tf.data.FixedLengthRecordDataset(filenames)#来自一个或多个二进制文件的固定长度记录的数据集 15 | tf.data.TFRecordDataset(filenames)#包含多个TFRecord文件的记录 16 | 17 | #迭代器 18 | #只迭代一次 19 | iterator = dataset.make_one_shot_iterator() 20 | #任意多次(每次循环需要重新初始化) 21 | iterator = dataset.make_initializable_iterator() 22 | 23 | #单次迭代程序 24 | import tensorflow as tf 25 | 26 | x=[1,2,3] 27 | y=[1,2,3] 28 | 29 | dataset=tf.data.Dataset.from_tensor_slices((x, y)) 30 | 31 | iterator = dataset.make_one_shot_iterator() 32 | X, Y = iterator.get_next() 33 | 34 | with tf.Session() as sess: 35 | print(sess.run([X, Y])) 36 | print(sess.run([X, Y])) 37 | print(sess.run([X, Y])) 38 | #print(sess.run([X, Y]))第四次输出会越界 39 | 40 | #多次迭代程序 41 | import tensorflow as tf 42 | 43 | x=[1,2,3] 44 | y=[1,2,3] 45 | 46 | dataset=tf.data.Dataset.from_tensor_slices((x, y)) 47 | 48 | iterator = dataset.make_initializable_iterator() 49 | X, Y = iterator.get_next() 50 | 51 | with tf.Session() as sess: 52 | for i in range(3): 53 | sess.run(iterator.initializer) 54 | try: 55 | while True: 56 | print(sess.run([X, Y])) 57 | except tf.errors.OutOfRangeError: 58 | pass 59 | 60 | #实现随机batch 61 | x=[1,2,3,4,5,6,7,8,9,10] 62 | y=[1,2,3,4,5,6,7,8,9,10] 63 | 64 | dataset=tf.data.Dataset.from_tensor_slices((x, y)) 65 | 66 | dataset = dataset.repeat(5)#重复n次 67 | dataset = dataset.shuffle(50)#随机重排输入数据集,数值越大,混乱程度越大 68 | dataset = dataset.batch(5)#分成多份,每份n个 69 | 70 | iterator = dataset.make_initializable_iterator() 71 | X, Y = iterator.get_next() 72 | 73 | with tf.Session() as sess: 74 | for i in range(1): 75 | sess.run(iterator.initializer) 76 | try: 77 | while True: 78 | print(sess.run([X, Y])) 79 | except tf.errors.OutOfRangeError: 80 | pass 81 | -------------------------------------------------------------------------------- /tf2.0: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | tf.__version__ 3 | 4 | !pip install tensorflow 5 | 6 | #张量 7 | #常量 8 | a = tf.constant(2, name='a') 9 | b = tf.constant(3, name='b') 10 | x = tf.add(a, b) 11 | print(x) 12 | print(a+b) 13 | 14 | a.get_shape() 15 | 16 | a.numpy() 17 | 18 | #变量 19 | s = tf.Variable(2, name="scalar") 20 | m = tf.Variable([[0, 1], [2, 3]], name="matrix") 21 | W = tf.Variable(tf.zeros([784,10])) 22 | s.assign(3) 23 | s.assign_add(3) 24 | 25 | class MyModule(tf.Module): 26 | def __init__(self): 27 | self.v0 = tf.Variable(1.0) 28 | self.vs = [tf.Variable(x) for x in range(10)] 29 | 30 | m = MyModule() 31 | m.variables 32 | 33 | #tf.data 34 | dataset = tf.data.Dataset.from_tensors([1,2,3,4,5]) 35 | for element in dataset: 36 | print(element.numpy()) 37 | it = iter(dataset) 38 | print(next(it).numpy()) 39 | 40 | dataset = tf.data.Dataset.from_tensor_slices([1,2,3,4,5]) 41 | for element in dataset: 42 | print(element.numpy()) 43 | it = iter(dataset) 44 | print(next(it).numpy()) 45 | 46 | features = tf.data.Dataset.from_tensor_slices([1,2,3,4,5]) 47 | labels = tf.data.Dataset.from_tensor_slices([6,7,8,9,10]) 48 | dataset = tf.data.Dataset.zip((features,labels)) 49 | for element in dataset3: 50 | print(element) 51 | 52 | inc_dataset = tf.data.Dataset.range(100) 53 | dec_dataset = tf.data.Dataset.range(0, -100, -1) 54 | dataset = tf.data.Dataset.zip((inc_dataset, dec_dataset)) 55 | batched_dataset = dataset.batch(4) 56 | 57 | for batch in batched_dataset.take(4): 58 | print([arr.numpy() for arr in batch]) 59 | 60 | shuffle_dataset = dataset.shuffle(buffer_size=10) 61 | for element in shuffle_dataset: 62 | print(element) 63 | 64 | shuffle_dataset = dataset.shuffle(buffer_size=100) 65 | for element in shuffle_dataset: 66 | print(element) 67 | 68 | 69 | -------------------------------------------------------------------------------- /tf2.0-keras: -------------------------------------------------------------------------------- 1 | #读取模型 2 | fashion_mnist = tf.keras.datasets.fashion_mnist 3 | 4 | (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() 5 | 6 | #获得图片大小 7 | train_images.shape 8 | #打印图例 9 | import numpy as np 10 | import matplotlib.pyplot as plt 11 | def plotImages(images_arr): 12 | fig, axes = plt.subplots(1, 5, figsize=(10,10)) 13 | axes = axes.flatten() 14 | for img, ax in zip( images_arr, axes): 15 | ax.imshow(img) 16 | ax.axis('off') 17 | plt.tight_layout() 18 | plt.show() 19 | plotImages(train_images[:5]) 20 | 21 | #归一化 22 | train_images = train_images / 255.0 23 | test_images = test_images / 255.0 24 | 25 | #全链接层模型 26 | model = tf.keras.Sequential([ 27 | tf.keras.layers.Flatten(input_shape=(28, 28)), 28 | tf.keras.layers.Dense(128, activation='relu', bias=False, trainable=False), 29 | tf.keras.layers.Dense(10, activation='softmax') 30 | ]) 31 | #模型总结 32 | model.summary() 33 | #编译 34 | model.compile(optimizer='adam', 35 | loss='sparse_categorical_crossentropy', categorical_crossentropy->[1,0,0,0,0] 36 | metrics=['accuracy']) 37 | 38 | #训练 39 | model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels)) 40 | #模型权重 41 | model.variables 42 | 43 | # 保存权重 44 | model.save_weights('./fashion_mnist/my_checkpoint') 45 | 46 | # 恢复权重 47 | model.load_weights('./fashion_mnist/my_checkpoint') 48 | # model1.load_weights('./fashion_mnist/my_checkpoint') 49 | 50 | # 预测 51 | loss,acc = model.evaluate(test_images, test_labels, verbose=2) 52 | print("Restored model, accuracy: {:5.2f}%".format(100*acc)) 53 | 54 | #保存整个模型 55 | model.save('my_model.h5') 56 | new_model = tf.keras.models.load_model('my_model.h5') 57 | loss, acc = new_model.evaluate(test_images, test_labels, verbose=2) 58 | print("Restored model, accuracy: {:5.2f}%".format(100*acc)) 59 | 60 | # 在文件名中包含 epoch (使用 `str.format`) 61 | checkpoint_path = "fashion_mnist_1/cp-{epoch:04d}.ckpt" 62 | 63 | # 创建一个回调,每个epoch保存模型的权重 64 | cp_callback = tf.keras.callbacks.ModelCheckpoint( 65 | filepath=checkpoint_path, 66 | save_weights_only=True, 67 | period=1) #save_freq = ‘epoch'/n samples 60000 100 600 68 | 69 | # 使用 `checkpoint_path` 格式保存权重 70 | model.save_weights(checkpoint_path.format(epoch=0)) 71 | 72 | # 使用新的回调训练模型 73 | model.fit(train_images, 74 | train_labels, 75 | epochs=5, 76 | callbacks=[cp_callback], 77 | validation_data=(test_images,test_labels)) 78 | 79 | #CNN模型 80 | train_images = train_images[..., tf.newaxis] 81 | test_images = test_images[..., tf.newaxis] 82 | 83 | model1 = tf.keras.Sequential() 84 | model1.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) 85 | model1.add(tf.keras.layers.MaxPooling2D((2, 2))) 86 | model1.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu')) 87 | model1.add(tf.keras.layers.MaxPooling2D((2, 2))) 88 | model1.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu')) 89 | model1.add(tf.keras.layers.Flatten()) 90 | model1.add(tf.keras.layers.Dense(256, activation='relu')) 91 | model1.add(tf.keras.layers.Dense(10, activation='softmax')) 92 | 93 | model1.compile(optimizer='adam', 94 | loss='sparse_categorical_crossentropy', 95 | metrics=['accuracy']) 96 | 97 | model1.fit(train_images, train_labels, 98 | batchsize=64, 99 | epochs=10, validation_data=(test_images, test_labels)) 100 | 101 | #RNN 102 | model2 = tf.keras.Sequential() 103 | model2.add(tf.keras.layers.LSTM(128,input_shape=(None,28))) batchsize,28,28 104 | # model2.add(tf.keras.layers.LSTM(128, return_sequences=True)) 105 | #(hidden size * (hidden size + input_dim ) + hidden size) *4 106 | model2.add(tf.keras.layers.Dense(10, activation='softmax')) 107 | 108 | model2.compile(optimizer='adam', 109 | loss='sparse_categorical_crossentropy', 110 | metrics=['accuracy']) 111 | 112 | model2.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels)) 113 | -------------------------------------------------------------------------------- /保存和读取权重: -------------------------------------------------------------------------------- 1 | #保存和读取权重 2 | tf.train.Saver.save(sess, save_path) 3 | tf.train.Saver.restore(sess, save_path) 4 | 5 | #定义模型 6 | model = SkipGramModel(params) 7 | 8 | #优化器 9 | optimizer=tf.GradientDescentOptimizer(learning_rate=0.1) 10 | 11 | #创建保持器 12 | saver = tf.train.Saver() 13 | 14 | with tf.Session() as sess: 15 | for step in range(training_steps): 16 | sess.run([optimizer]) 17 | 18 | saver.save(sess, 'model_name') 19 | 20 | #四个文件 21 | checkpoint 保留至最新 22 | model.data-00000-of-00001 变量值 23 | model.index 变量目录 24 | model.meta 图结构 25 | 26 | #只保存某几个变量 27 | 28 | saver = tf.train.Saver({'v1': v1, 'v2': v2}) 29 | saver = tf.train.Saver([v1, v2]) 30 | saver = tf.train.Saver({v.op.name: v for v in [v1, v2]}) 31 | 32 | 33 | with tf.Session() as sess: 34 | saver.restore(sess, 'name_of_the_checkpoint') 35 | 36 | #检查路径是否有检查点 37 | ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/checkpoint')) 38 | #读取最新的保存 39 | if ckpt and ckpt.model_checkpoint_path: 40 | saver.restore(sess, ckpt.model_checkpoint_path) 41 | 42 | #全局步骤 43 | global_step = tf.Variable(0,trainable=False, name='global_step') 44 | 45 | optimizer.minimize(loss, global_step=global_step) 46 | 47 | saver.save(sess, 'model_name',global_step=global_step) 48 | -------------------------------------------------------------------------------- /共享名称,变量: -------------------------------------------------------------------------------- 1 | 共享组名: 2 | with tf.name_scope(name_of_that_scope): 3 | # declare op_1 4 | # declare op_2 5 | 6 | with tf.name_scope('data'): 7 | iterator = dataset.make_initializable_iterator() 8 | center_words, target_words = iterator.get_next() 9 | 10 | with tf.name_scope('embed'): 11 | embed_matrix = tf.get_variable('embed_matrix', 12 | shape=[VOCAB_SIZE, EMBED_SIZE], ...) 13 | embed = tf.nn.embedding_lookup(embed_matrix, center_words) 14 | 15 | with tf.name_scope('loss'): 16 | nce_weight = tf.get_variable('nce_weight', shape=[VOCAB_SIZE, EMBED_SIZE], ...) 17 | nce_bias = tf.get_variable('nce_bias', initializer=tf.zeros([VOCAB_SIZE])) 18 | loss = tf.reduce_mean(tf.nn.nce_loss(weights=nce_weight, biases=nce_bias, …) 19 | 20 | with tf.name_scope('optimizer'): 21 | optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss) 22 | 23 | #共享变量 24 | #无法共享的写法 25 | import tensorflow as tf 26 | 27 | def layers(x): 28 | w1 = tf.Variable(tf.random_normal([5]), name='h1_weights') 29 | b1 = tf.Variable(tf.zeros([5]), name='h1_biases') 30 | h1 = x*w1 + b1 31 | return h1 32 | 33 | h1=layers(tf.ones([5])) 34 | h2=layers(tf.ones([5])) 35 | 36 | with tf.Session() as sess: 37 | sess.run(tf.global_variables_initializer()) 38 | print (sess.run(h1)) 39 | print (sess.run(h2)) 40 | 41 | tf.get_variable(, , ) 42 | #如果一个变量已经存在,重新利用它 43 | #否则,初始化它 44 | 45 | import tensorflow as tf 46 | 47 | def layers(x): 48 | w1 = tf.get_variable("weights", [5], initializer=tf.random_normal_initializer()) 49 | b1 = tf.get_variable("biases", [5], initializer=tf.constant_initializer(0.0)) 50 | h1 = x*w1 + b1 51 | return h1 52 | 53 | #把需要共享参数的变量放在一个组里: 54 | with tf.variable_scope('layers') as scope: 55 | scope.reuse_variables() 56 | h1=layers(tf.ones([5])) 57 | scope.reuse_variables() 58 | h2=layers(tf.ones([5])) 59 | 60 | with tf.Session() as sess: 61 | sess.run(tf.global_variables_initializer()) 62 | print (sess.run(h1)) 63 | print (sess.run(h2)) 64 | 65 | -------------------------------------------------------------------------------- /卷积层: -------------------------------------------------------------------------------- 1 | 卷积层 2 | 3 | 卷积核,一般用5*5或3*3 4 | 5 | 输出通道一般为64或128 6 | 7 | #输入为3通道,输出为64通道,一共几个卷积核? 8 | 9 | tensorflow中卷积层函数 10 | 11 | 1.keras系列 12 | tf.layers.conv2d 13 | tf.keras.layers.Conv2D 14 | 15 | tf.layers.conv2d( 16 | inputs, 17 | filters,#输出通道数 64 18 | kernel_size,#(3,3) 19 | strides=(1, 1),#strides=1 20 | padding='valid',#‘same’ 21 | data_format='channels_last', 22 | dilation_rate=(1, 1),#如果你的strides不为1,dilation_rate暂不支持 23 | activation=None, 24 | #默认线性激活 25 | #tf.keras.activations.relu 26 | #tf.keras.activations.sigmoid 27 | #tf.keras.activations.softmax 28 | use_bias=True, 29 | kernel_initializer=None, 30 | #初始化不能为0 31 | #tf.initializers.random_normal 32 | #tf.initializers.random_uniform 33 | bias_initializer=tf.zeros_initializer(), 34 | kernel_regularizer=None, 35 | #tf.keras.regularizers.l1 36 | #tf.keras.regularizers.l2 37 | bias_regularizer=None, 38 | activity_regularizer=None, 39 | trainable=True, 40 | name=None, 41 | reuse=None 42 | ) 43 | #这个函数即将被删除 44 | 45 | tf.keras.layers.Conv2D 46 | 47 | 2.tf.nn(参数少) 48 | 需要自定义权重 49 | w=... 50 | tf.nn.conv2d( 51 | input, 52 | filter,#输入卷积核,形状为[高,宽,输入通道,输出通道] 53 | strides, 54 | padding, 55 | use_cudnn_on_gpu=True, 56 | data_format='NHWC', 57 | dilations=[1, 1, 1, 1],#通道数两个维度必须为1 58 | name=None 59 | ) 60 | #激活层 61 | tf.nn.relu 62 | tf.nn.softmax 63 | #正则化 64 | tf.nn.l2_loss 65 | 66 | 3.contrib系列(功能多) 67 | tf.contrib.layers.conv2d( 68 | inputs, 69 | num_outputs, 70 | kernel_size, 71 | stride=1, 72 | padding='SAME', 73 | data_format=None, 74 | rate=1,# dilation rate 75 | activation_fn=tf.nn.relu, 76 | #默认为relu 77 | #tf.contrib.layers.softmax 78 | normalizer_fn=None, 79 | #归一化,代替biase 80 | #tf.contrib.layers.batch_norm 81 | normalizer_params=None, 82 | weights_initializer=initializers.xavier_initializer(), 83 | weights_regularizer=None, 84 | #tf.contrib.layers.l1_regularizer 85 | #tf.contrib.layers.l2_regularizer 86 | biases_initializer=tf.zeros_initializer(), 87 | biases_regularizer=None, 88 | reuse=None, 89 | variables_collections=None,#把变量加入到某个集合 90 | outputs_collections=None,#把输出值加入到某个集合 91 | trainable=True, 92 | scope=None#name 93 | ) 94 | 95 | 小结: 96 | 对初学者,建议使用 97 | tf.keras.layers.Conv2D 98 | 简单,封装好的函数 99 | 想要功能多一点可以使用 100 | tf.contrib.layers.conv2d 101 | 102 | 想深入研究,特别是研究各网络参数的作用,自定义loss等 103 | 用tf.nn.conv2d 104 | -------------------------------------------------------------------------------- /变分自动编码器: -------------------------------------------------------------------------------- 1 | 变分自动编码器 2 | 3 | 对图片编码 4 | 对图片进行压缩,保留关键信息 5 | 对图片解码 6 | 学习训练图片之后,自动产生相似的图片 7 | 8 | #tensorflow 概率分布计算 9 | import tensorflow as tf 10 | a=[[1.1],[2.1],[3.2],[4.5],[5.0]] 11 | inputs=tf.Variable(a) 12 | hidden = tf.layers.dense(inputs, 100, tf.nn.relu) 13 | #均值 14 | mean = tf.layers.dense(hidden, 10, None) 15 | #标准差 16 | stddev = tf.layers.dense(hidden, 10, tf.nn.softplus) 17 | #分布 18 | tfd = tf.contrib.distributions 19 | #取样 20 | dist = tfd.MultivariateNormalDiag(mean, stddev) 21 | samples = dist.sample() 22 | dist.log_prob(samples) 23 | dist.prob(samples) 24 | 25 | #概率分布只能接受浮点运算类型 26 | dist = tfd.MultivariateNormalDiag([0.0,10.0], [1.0,5.0]) 27 | sess=tf.Session() 28 | for i in range(10): 29 | samples = dist.sample() 30 | print(sess.run(samples)) 31 | print(sess.run(dist.prob(samples))) 32 | 33 | #回归 34 | loss = -dist.log_prob(label) 35 | optimize = tf.train.AdamOptimizer().minimize(loss) 36 | #分类 37 | dist = tfd.Categorical(logit) 38 | loss = -dist.log_prob(label) 39 | optimize = tf.train.AdamOptimizer().minimize(loss) 40 | 41 | #tensorflow实现变分自动编码器 42 | import numpy as np 43 | import IPython.display as display 44 | import tensorflow as tf 45 | 46 | import matplotlib.pyplot as plt 47 | import matplotlib as mpl 48 | 49 | def make_prior(code_size=100): 50 | mean, stddev = tf.zeros([code_size]), tf.ones([code_size]) 51 | return tfd.MultivariateNormalDiag(mean, stddev) 52 | 53 | def make_encoder(images, code_size=100): 54 | images = tf.layers.flatten(images) 55 | hidden = tf.layers.dense(images, 128, tf.nn.relu) 56 | mean = tf.layers.dense(hidden, code_size) 57 | stddev = tf.layers.dense(hidden, code_size, tf.nn.softplus) 58 | return tfd.MultivariateNormalDiag(mean, stddev) 59 | 60 | def make_decoder(code, data_shape=[28, 28]): 61 | hidden = tf.layers.dense(code, 128, tf.nn.relu) 62 | logit = tf.layers.dense(hidden, np.prod(data_shape)) 63 | logit = tf.reshape(logit, [-1] + data_shape) 64 | return tfd.Independent(tfd.Bernoulli(logit), len(data_shape)) 65 | 66 | tfd = tf.contrib.distributions 67 | #用minist数据集 68 | #输入图片 69 | images = tf.placeholder(tf.float32, [None, 28, 28]) 70 | #初始先验概率 71 | prior = make_prior() 72 | #编码 73 | posterior = make_encoder(images) 74 | #解码 75 | dist = make_decoder(posterior.sample()) 76 | #取样 77 | samples = make_decoder(prior.sample(10)).mean() 78 | #损失函数 79 | elbo =tf.reduce_mean(dist.log_prob(images)) - tf.reduce_mean(tfd.kl_divergence(posterior, prior)) 80 | optimize = tf.train.AdamOptimizer(learning_rate=0.01).minimize(-elbo) 81 | 82 | mnist = tf.keras.datasets.mnist 83 | 84 | #读取数据 85 | (x_train, y_train),(x_test, y_test) = mnist.load_data() 86 | x_train=x_train/255.0 87 | 88 | sess=tf.Session() 89 | sess.run(tf.global_variables_initializer()) 90 | 91 | for i in range(10): 92 | _, loss=sess.run([optimize,-elbo], feed_dict={images:x_train}) 93 | print(loss) 94 | 95 | https://drive.google.com/open?id=1tDRo8ZGEqWNtXK516_8ZVB5wO_BrB8Ey 96 | -------------------------------------------------------------------------------- /图像分类项目: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, division, print_function, unicode_literals 2 | 3 | import tensorflow as tf 4 | 5 | from tensorflow.keras import datasets, layers, models 6 | import matplotlib.pyplot as plt 7 | 8 | #cifar100数据集 9 | (train_images, train_labels), (test_images, test_labels) = datasets.cifar100.load_data() 10 | 11 | #归一化 12 | train_images, test_images = train_images / 255.0, test_images / 255.0 13 | 14 | print(len(train_images)) 15 | print(len(test_images)) 16 | 17 | dic = {} 18 | for label in train_labels: 19 | x = int(label) 20 | if x not in dic: 21 | dic[x] = 1 22 | else: 23 | dic[x] += 1 24 | print(dic) 25 | 26 | print(train_images[0].shape) 27 | print(train_images[100].shape) 28 | print(train_images[10000].shape) 29 | 30 | plt.figure(figsize=(10,10)) 31 | for i in range(25): 32 | plt.subplot(5,5,i+1) 33 | plt.xticks([]) 34 | plt.yticks([]) 35 | plt.grid(False) 36 | plt.imshow(train_images[i], cmap=plt.cm.binary) 37 | plt.xlabel(train_labels[i][0]) 38 | plt.show() 39 | 40 | #搭建模型 41 | model = models.Sequential() 42 | model.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3))) 43 | model.add(layers.MaxPooling2D((2, 2))) 44 | model.add(layers.Conv2D(128, (3, 3), activation='relu')) 45 | model.add(layers.MaxPooling2D((2, 2))) 46 | model.add(layers.Conv2D(128, (3, 3), activation='relu')) 47 | model.add(layers.Flatten()) 48 | model.add(layers.Dense(512, activation='relu')) 49 | model.add(layers.Dense(100, activation='softmax')) 50 | 51 | #在小规模训练集上训练 52 | train_images1 = train_images[0:100] 53 | train_labels1 = train_labels[0:100] 54 | 55 | model.compile(optimizer='adam', 56 | loss='sparse_categorical_crossentropy', 57 | metrics=['accuracy']) 58 | 59 | history = model.fit(train_images1, train_labels1, epochs=100) 60 | 61 | model.compile(optimizer='adam', 62 | loss='sparse_categorical_crossentropy', 63 | metrics=['accuracy']) 64 | 65 | history = model.fit(train_images, train_labels, epochs=20, 66 | validation_data=(test_images, test_labels)) 67 | 68 | plt.plot(history.history['loss'], label='loss') 69 | plt.plot(history.history['val_loss'], label = 'val_loss') 70 | plt.xlabel('Epoch') 71 | plt.ylabel('loss') 72 | plt.legend(loc='lower right') 73 | 74 | #减小模型 75 | model1 = models.Sequential() 76 | model1.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) 77 | model1.add(layers.MaxPooling2D((2, 2))) 78 | model1.add(layers.Conv2D(32, (3, 3), activation='relu')) 79 | model1.add(layers.MaxPooling2D((2, 2))) 80 | model1.add(layers.Conv2D(32, (3, 3), activation='relu')) 81 | model1.add(layers.Flatten()) 82 | model1.add(layers.Dense(100, activation='softmax')) 83 | 84 | model1.compile(optimizer='adam', 85 | loss='sparse_categorical_crossentropy', 86 | metrics=['accuracy']) 87 | 88 | history1 = model1.fit(train_images, train_labels, epochs=20, 89 | validation_data=(test_images, test_labels)) 90 | 91 | plt.plot(history1.history['loss'], label='loss') 92 | plt.plot(history1.history['val_loss'], label = 'val_loss') 93 | plt.xlabel('Epoch') 94 | plt.ylabel('loss') 95 | plt.legend(loc='lower right') 96 | 97 | #正则化 98 | model2 = models.Sequential() 99 | model2.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3), kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 100 | model2.add(layers.MaxPooling2D((2, 2))) 101 | model2.add(layers.Conv2D(128, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 102 | model2.add(layers.MaxPooling2D((2, 2))) 103 | model2.add(layers.Conv2D(128, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 104 | model2.add(layers.Flatten()) 105 | model2.add(layers.Dense(512, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 106 | model2.add(layers.Dense(100, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 107 | 108 | model2.compile(optimizer='adam', 109 | loss='sparse_categorical_crossentropy', 110 | metrics=['accuracy']) 111 | 112 | history2 = model2.fit(train_images, train_labels, epochs=20, 113 | validation_data=(test_images, test_labels)) 114 | 115 | plt.plot(history2.history['loss'], label='loss') 116 | plt.plot(history2.history['val_loss'], label = 'val_loss') 117 | plt.xlabel('Epoch') 118 | plt.ylabel('loss') 119 | plt.legend(loc='lower right') 120 | 121 | 122 | #正则化 123 | model3 = models.Sequential() 124 | model3.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3), kernel_regularizer=tf.keras.regularizers.l2(l=0.005))) 125 | model3.add(layers.MaxPooling2D((2, 2))) 126 | model3.add(layers.Conv2D(128, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.005))) 127 | model3.add(layers.MaxPooling2D((2, 2))) 128 | model3.add(layers.Conv2D(128, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.005))) 129 | model3.add(layers.Flatten()) 130 | model3.add(layers.Dense(512, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.005))) 131 | model3.add(layers.Dense(100, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2(l=0.005))) 132 | 133 | model3.compile(optimizer='adam', 134 | loss='sparse_categorical_crossentropy', 135 | metrics=['accuracy']) 136 | 137 | history3 = model3.fit(train_images, train_labels, epochs=20, 138 | validation_data=(test_images, test_labels)) 139 | 140 | plt.plot(history3.history['loss'], label='loss') 141 | plt.plot(history3.history['val_loss'], label = 'val_loss') 142 | plt.xlabel('Epoch') 143 | plt.ylabel('loss') 144 | plt.legend(loc='lower right') 145 | 146 | #正则化+减小参数 147 | model4 = models.Sequential() 148 | model4.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3), kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 149 | model4.add(layers.MaxPooling2D((2, 2))) 150 | model4.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 151 | model4.add(layers.MaxPooling2D((2, 2))) 152 | model4.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 153 | model4.add(layers.Flatten()) 154 | model4.add(layers.Dense(256, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 155 | model4.add(layers.Dense(100, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 156 | 157 | model4.compile(optimizer='adam', 158 | loss='sparse_categorical_crossentropy', 159 | metrics=['accuracy']) 160 | 161 | history4 = model4.fit(train_images, train_labels, epochs=20, 162 | validation_data=(test_images, test_labels)) 163 | 164 | plt.plot(history4.history['loss'], label='loss') 165 | plt.plot(history4.history['val_loss'], label = 'val_loss') 166 | plt.xlabel('Epoch') 167 | plt.ylabel('loss') 168 | plt.legend(loc='lower right') 169 | 170 | #搭建模型,droupout 171 | model5 = models.Sequential() 172 | model5.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3))) 173 | model5.add(layers.MaxPooling2D((2, 2))) 174 | model5.add(layers.Dropout(0.2)) 175 | model5.add(layers.Conv2D(128, (3, 3), activation='relu')) 176 | model5.add(layers.MaxPooling2D((2, 2))) 177 | model5.add(layers.Dropout(0.2)) 178 | model5.add(layers.Conv2D(128, (3, 3), activation='relu')) 179 | model5.add(layers.Flatten()) 180 | model5.add(layers.Dense(512, activation='relu')) 181 | model5.add(layers.Dense(100, activation='softmax')) 182 | 183 | model5.compile(optimizer='adam', 184 | loss='sparse_categorical_crossentropy', 185 | metrics=['accuracy']) 186 | 187 | history5 = model5.fit(train_images, train_labels, epochs=20, 188 | validation_data=(test_images, test_labels)) 189 | 190 | plt.plot(history5.history['loss'], label='loss') 191 | plt.plot(history5.history['val_loss'], label = 'val_loss') 192 | plt.xlabel('Epoch') 193 | plt.ylabel('loss') 194 | plt.legend(loc='lower right') 195 | 196 | #搭建模型,droupout 197 | model6 = models.Sequential() 198 | model6.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3))) 199 | model6.add(layers.MaxPooling2D((2, 2))) 200 | model6.add(layers.Dropout(0.1)) 201 | model6.add(layers.Conv2D(128, (3, 3), activation='relu')) 202 | model6.add(layers.MaxPooling2D((2, 2))) 203 | model6.add(layers.Dropout(0.1)) 204 | model6.add(layers.Conv2D(128, (3, 3), activation='relu')) 205 | model6.add(layers.Flatten()) 206 | model6.add(layers.Dense(512, activation='relu')) 207 | model6.add(layers.Dense(100, activation='softmax')) 208 | 209 | model6.compile(optimizer='adam', 210 | loss='sparse_categorical_crossentropy', 211 | metrics=['accuracy']) 212 | 213 | history6 = model6.fit(train_images, train_labels, epochs=20, 214 | validation_data=(test_images, test_labels)) 215 | 216 | plt.plot(history6.history['loss'], label='loss') 217 | plt.plot(history6.history['val_loss'], label = 'val_loss') 218 | plt.xlabel('Epoch') 219 | plt.ylabel('loss') 220 | plt.legend(loc='lower right') 221 | 222 | #搭建模型,droupout 223 | model7 = models.Sequential() 224 | model7.add(layers.Conv2D(64, (3, 3), activation='relu', input_shape=(32, 32, 3))) 225 | model7.add(layers.MaxPooling2D((2, 2))) 226 | model7.add(layers.Conv2D(128, (3, 3), activation='relu')) 227 | model7.add(layers.MaxPooling2D((2, 2))) 228 | model7.add(layers.Conv2D(128, (3, 3), activation='relu')) 229 | model7.add(layers.Flatten()) 230 | model7.add(layers.Dense(512, activation='relu')) 231 | model7.add(layers.Dropout(0.2)) 232 | model7.add(layers.Dense(100, activation='softmax')) 233 | 234 | model7.compile(optimizer='adam', 235 | loss='sparse_categorical_crossentropy', 236 | metrics=['accuracy']) 237 | 238 | history7 = model7.fit(train_images, train_labels, epochs=20, 239 | validation_data=(test_images, test_labels)) 240 | 241 | plt.plot(history7.history['loss'], label='loss') 242 | plt.plot(history7.history['val_loss'], label = 'val_loss') 243 | plt.xlabel('Epoch') 244 | plt.ylabel('loss') 245 | plt.legend(loc='lower right') 246 | 247 | #正则化+减小参数 248 | model4 = models.Sequential() 249 | model4.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3), kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 250 | model4.add(layers.MaxPooling2D((2, 2))) 251 | model4.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 252 | model4.add(layers.MaxPooling2D((2, 2))) 253 | model4.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 254 | model4.add(layers.Flatten()) 255 | model4.add(layers.Dense(256, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 256 | model4.add(layers.Dense(100, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2(l=0.0005))) 257 | 258 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), 259 | loss='sparse_categorical_crossentropy', 260 | metrics=['accuracy']) 261 | 262 | history = model4.fit(train_images, train_labels, epochs=20, initial_epoch=0 263 | validation_data=(test_images, test_labels)) 264 | 265 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005), 266 | loss='sparse_categorical_crossentropy', 267 | metrics=['accuracy']) 268 | 269 | history = model4.fit(train_images, train_labels, epochs=40, 270 | validation_data=(test_images, test_labels)) 271 | 272 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.002), 273 | loss='sparse_categorical_crossentropy', 274 | metrics=['accuracy']) 275 | 276 | history = model4.fit(train_images, train_labels, epochs=20, 277 | validation_data=(test_images, test_labels)) 278 | 279 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), 280 | loss='sparse_categorical_crossentropy', 281 | metrics=['accuracy']) 282 | 283 | history = model4.fit(train_images, train_labels, epochs=20, initial_epoch=0, 284 | validation_data=(test_images, test_labels)) 285 | 286 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005), 287 | loss='sparse_categorical_crossentropy', 288 | metrics=['accuracy']) 289 | 290 | history = model4.fit(train_images, train_labels, epochs=40, 291 | validation_data=(test_images, test_labels)) 292 | 293 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005), 294 | loss='sparse_categorical_crossentropy', 295 | metrics=['accuracy']) 296 | 297 | history = model4.fit(train_images, train_labels, epochs=15, 298 | validation_data=(test_images, test_labels)) 299 | 300 | model4.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00005), 301 | loss='sparse_categorical_crossentropy', 302 | metrics=['accuracy']) 303 | 304 | history1 = model4.fit(train_images, train_labels, epochs=5, 305 | validation_data=(test_images, test_labels)) 306 | 307 | plt.plot(history.history['loss']+history1.history['loss'],label='loss') 308 | plt.plot(history.history['val_loss']+history1.history['val_loss'], label = 'val_loss') 309 | plt.xlabel('Epoch') 310 | plt.ylabel('loss') 311 | plt.legend(loc='lower right') 312 | -------------------------------------------------------------------------------- /图像数据增强: -------------------------------------------------------------------------------- 1 | from tensorflow.keras.models import Sequential 2 | from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D 3 | from tensorflow.keras.preprocessing.image import ImageDataGenerator 4 | 5 | import os 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | _URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip' 10 | 11 | path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True) 12 | 13 | PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered') 14 | 15 | train_dir = os.path.join(PATH, 'train') 16 | validation_dir = os.path.join(PATH, 'validation') 17 | 18 | train_cats_dir = os.path.join(train_dir, 'cats') # directory with our training cat pictures 19 | train_dogs_dir = os.path.join(train_dir, 'dogs') # directory with our training dog pictures 20 | validation_cats_dir = os.path.join(validation_dir, 'cats') # directory with our validation cat pictures 21 | validation_dogs_dir = os.path.join(validation_dir, 'dogs') # directory with our validation dog pictures 22 | 23 | num_cats_tr = len(os.listdir(train_cats_dir)) 24 | num_dogs_tr = len(os.listdir(train_dogs_dir)) 25 | 26 | num_cats_val = len(os.listdir(validation_cats_dir)) 27 | num_dogs_val = len(os.listdir(validation_dogs_dir)) 28 | 29 | total_train = num_cats_tr + num_dogs_tr 30 | total_val = num_cats_val + num_dogs_val 31 | 32 | print('total training cat images:', num_cats_tr) 33 | print('total training dog images:', num_dogs_tr) 34 | 35 | print('total validation cat images:', num_cats_val) 36 | print('total validation dog images:', num_dogs_val) 37 | print("--") 38 | print("Total training images:", total_train) 39 | print("Total validation images:", total_val) 40 | 41 | batch_size = 64 42 | epochs = 20 43 | IMG_HEIGHT = 150 44 | IMG_WIDTH = 150 45 | 46 | 47 | train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data 48 | validation_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data 49 | 50 | train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size, 51 | train_data,test_data 52 | shuffle=True, 53 | target_size=(IMG_HEIGHT, IMG_WIDTH), 54 | class_mode='binary') 55 | 56 | val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size, 57 | directory=validation_dir, 58 | target_size=(IMG_HEIGHT, IMG_WIDTH), 59 | class_mode='binary') 60 | 61 | sample_training_images, _ = next(train_data_gen) 62 | 63 | # This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column. 64 | def plotImages(images_arr): 65 | fig, axes = plt.subplots(1, 5, figsize=(10,10)) 66 | axes = axes.flatten() 67 | for img, ax in zip( images_arr, axes): 68 | ax.imshow(img) 69 | ax.axis('off') 70 | plt.tight_layout() 71 | plt.show() 72 | 73 | plotImages(sample_training_images[:5]) 74 | 75 | model = Sequential([ 76 | Conv2D(32, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)), 77 | MaxPooling2D(), 78 | Conv2D(64, 3, padding='same', activation='relu'), 79 | MaxPooling2D(), 80 | Conv2D(64, 3, padding='same', activation='relu'), 81 | MaxPooling2D(), 82 | Flatten(), 83 | Dense(256, activation='relu'), 84 | Dense(1, activation='sigmoid') 85 | ]) 86 | 87 | model.compile(optimizer='adam', 88 | loss='binary_crossentropy', 89 | metrics=['accuracy']) 90 | 91 | history = model.fit_generator( 92 | train_data_gen, 93 | steps_per_epoch=total_train // batch_size, 94 | epochs=epochs, 95 | validation_data=val_data_gen, 96 | validation_steps=total_val // batch_size 97 | ) 98 | 99 | acc = history.history['accuracy'] 100 | val_acc = history.history['val_accuracy'] 101 | 102 | loss = history.history['loss'] 103 | val_loss = history.history['val_loss'] 104 | 105 | epochs_range = range(epochs) 106 | 107 | plt.figure(figsize=(8, 8)) 108 | plt.subplot(1, 2, 1) 109 | plt.plot(epochs_range, acc, label='Training Accuracy') 110 | plt.plot(epochs_range, val_acc, label='Validation Accuracy') 111 | plt.legend(loc='lower right') 112 | plt.title('Training and Validation Accuracy') 113 | 114 | plt.subplot(1, 2, 2) 115 | plt.plot(epochs_range, loss, label='Training Loss') 116 | plt.plot(epochs_range, val_loss, label='Validation Loss') 117 | plt.legend(loc='upper right') 118 | plt.title('Training and Validation Loss') 119 | plt.show() 120 | 121 | #水平翻转 122 | image_gen = ImageDataGenerator(rescale=1./255, horizontal_flip=True) 123 | train_data_gen = image_gen.flow_from_directory(batch_size=batch_size, 124 | directory=train_dir, 125 | shuffle=True, 126 | target_size=(IMG_HEIGHT, IMG_WIDTH)) 127 | augmented_images = [train_data_gen[0][0][0] for i in range(5)] 128 | plotImages(augmented_images) 129 | 130 | #竖直翻转 131 | image_gen = ImageDataGenerator(rescale=1./255, vertical_flip=True) 132 | train_data_gen = image_gen.flow_from_directory(batch_size=batch_size, 133 | directory=train_dir, 134 | shuffle=True, 135 | target_size=(IMG_HEIGHT, IMG_WIDTH)) 136 | augmented_images = [train_data_gen[0][0][0] for i in range(5)] 137 | plotImages(augmented_images) 138 | 139 | #随机旋转 140 | image_gen = ImageDataGenerator(rescale=1./255, rotation_range=45) -45,0,45 141 | train_data_gen = image_gen.flow_from_directory(batch_size=batch_size, 142 | directory=train_dir, 143 | shuffle=True, 144 | target_size=(IMG_HEIGHT, IMG_WIDTH)) 145 | 146 | augmented_images = [train_data_gen[0][0][0] for i in range(5)] 147 | 148 | # 随机缩放 149 | # zoom_range from 0 - 1 150 | # If a float, [lower, upper] = [1-zoom_range, 1+zoom_range] 151 | image_gen = ImageDataGenerator(rescale=1./255, zoom_range=0.5) # 152 | train_data_gen = image_gen.flow_from_directory(batch_size=batch_size, 153 | directory=train_dir, 154 | shuffle=True, 155 | target_size=(IMG_HEIGHT, IMG_WIDTH)) 156 | 157 | augmented_images = [train_data_gen[0][0][0] for i in range(5)] 158 | 159 | #全部应用 160 | image_gen_train = ImageDataGenerator( 161 | rescale=1./255, 162 | rotation_range=45, 163 | width_shift_range=.15, 164 | height_shift_range=.15, 165 | horizontal_flip=True, 166 | vertical_flip=True, 167 | zoom_range=0.5 168 | ) 169 | train_data_gen = image_gen_train.flow_from_directory(batch_size=batch_size, 170 | directory=train_dir, 171 | shuffle=True, 172 | target_size=(IMG_HEIGHT, IMG_WIDTH), 173 | class_mode='binary') 174 | 175 | augmented_images = [train_data_gen[0][0][0] for i in range(5)] 176 | plotImages(augmented_images) 177 | 178 | model = Sequential([ 179 | Conv2D(32, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)), 180 | MaxPooling2D(), 181 | Conv2D(64, 3, padding='same', activation='relu'), 182 | MaxPooling2D(), 183 | Conv2D(64, 3, padding='same', activation='relu'), 184 | MaxPooling2D(), 185 | Flatten(), 186 | Dense(256, activation='relu'), 187 | Dense(1, activation='sigmoid') 188 | ]) 189 | 190 | model.compile(optimizer='adam', 191 | loss='binary_crossentropy', 192 | metrics=['accuracy']) 193 | 194 | history = model.fit_generator( 195 | train_data_gen, 196 | steps_per_epoch=total_train // batch_size, 197 | epochs=epochs, 198 | validation_data=val_data_gen, 199 | validation_steps=total_val // batch_size 200 | ) 201 | 202 | acc = history.history['accuracy'] 203 | val_acc = history.history['val_accuracy'] 204 | 205 | loss = history.history['loss'] 206 | val_loss = history.history['val_loss'] 207 | 208 | epochs_range = range(epochs) 209 | 210 | plt.figure(figsize=(8, 8)) 211 | plt.subplot(1, 2, 1) 212 | plt.plot(epochs_range, acc, label='Training Accuracy') 213 | plt.plot(epochs_range, val_acc, label='Validation Accuracy') 214 | plt.legend(loc='lower right') 215 | plt.title('Training and Validation Accuracy') 216 | 217 | plt.subplot(1, 2, 2) 218 | plt.plot(epochs_range, loss, label='Training Loss') 219 | plt.plot(epochs_range, val_loss, label='Validation Loss') 220 | plt.legend(loc='upper right') 221 | plt.title('Training and Validation Loss') 222 | plt.show() 223 | 224 | -------------------------------------------------------------------------------- /在移动设备上部署机器学习模型: -------------------------------------------------------------------------------- 1 | 在移动设备上部署机器学习模型 2 | 3 | 1.操作系统不同 4 | 2.存储容量不同 5 | 6 | 主要目的:减小模型文件大小,保持准确率 7 | 8 | #TensorFlow Lite 9 | #转化器 和 翻译器 10 | import tensorflow as tf 11 | 12 | #创建转化器(共三种方式) 13 | converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) 14 | converter = tf.lite.TFLiteConverter.from_keras_model(model) 15 | converter = tf.lite.TFLiteConverter.from_concrete_functions() 16 | #转化模型 17 | tflite_model = converter.convert() 18 | #保存文件 19 | open("converted_model.tflite", "wb").write(tflite_model) 20 | 21 | #在不同系统上,翻译器所用的语言不同 22 | #安卓:Java or C++ 23 | #苹果:Swift or Objective-C 24 | 25 | #加载并运行模型 26 | import numpy as np 27 | import tensorflow as tf 28 | 29 | #创建翻译器 30 | interpreter = tf.lite.Interpreter(model_path="converted_model.tflite") 31 | interpreter.allocate_tensors() 32 | 33 | #为输入输出分配张量 34 | input_details = interpreter.get_input_details() 35 | output_details = interpreter.get_output_details() 36 | 37 | #输入 38 | input_shape = input_details[0]['shape'] 39 | input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32) 40 | interpreter.set_tensor(input_details[0]['index'], input_data) 41 | #model(input_data) 42 | #计算 43 | interpreter.invoke() 44 | #输出 45 | output_data = interpreter.get_tensor(output_details[0]['index']) 46 | 47 | 48 | #量化模型 49 | import tensorflow as tf 50 | #创建转化器 51 | converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) 52 | #优化类型 53 | converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE] 54 | #转换 55 | tflite_quant_model = converter.convert() 56 | -------------------------------------------------------------------------------- /基于注意力的翻译: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, division, print_function, unicode_literals 2 | 3 | import tensorflow as tf 4 | 5 | import matplotlib.pyplot as plt 6 | import matplotlib.ticker as ticker 7 | from sklearn.model_selection import train_test_split 8 | 9 | import unicodedata 10 | import re 11 | import numpy as np 12 | import os 13 | import io 14 | import time 15 | 16 | #西班牙语翻译为英语 17 | # 下载文件 18 | path_to_zip = tf.keras.utils.get_file( 19 | 'spa-eng.zip', origin='http://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip', 20 | extract=True) 21 | 22 | path_to_file = os.path.dirname(path_to_zip)+"/spa-eng/spa.txt" 23 | ''' 24 | 数据预处理 25 | 26 | 1.给每个句子添加一个 开始 和一个 结束 标记(token)。 27 | 28 | 2.删除特殊字符以清理句子。 29 | 30 | 3.创建一个单词索引和一个反向单词索引(即一个从单词映射至 id 的词典和一个从 id 映射至单词的词典)。 31 | 32 | 4.将每个句子填充(pad)到最大长度。 33 | ''' 34 | 35 | # 将 unicode 文件转换为 ascii 36 | def unicode_to_ascii(s): 37 | return ''.join(c for c in unicodedata.normalize('NFD', s) 38 | if unicodedata.category(c) != 'Mn') 39 | 40 | 41 | def preprocess_sentence(w): 42 | w = unicode_to_ascii(w.lower().strip()) 43 | 44 | # 在单词与跟在其后的标点符号之间插入一个空格 45 | # 例如: "he is a boy." => "he is a boy ." 46 | w = re.sub(r"([?.!,¿])", r" \1 ", w) 47 | w = re.sub(r'[" "]+', " ", w) 48 | 49 | # 除了 (a-z, A-Z, ".", "?", "!", ","),将所有字符替换为空格 50 | w = re.sub(r"[^a-zA-Z?.!,¿]+", " ", w) 51 | 52 | w = w.rstrip().strip() 53 | 54 | # 给句子加上开始和结束标记 55 | # 以便模型知道何时开始和结束预测 56 | w = ' ' + w + ' ' 57 | return w 58 | 59 | en_sentence = u"May I borrow this book?" 60 | sp_sentence = u"¿Puedo tomar prestado este libro?" 61 | print(preprocess_sentence(en_sentence)) 62 | print(preprocess_sentence(sp_sentence)) 63 | 64 | # 1. 去除重音符号 65 | # 2. 清理句子 66 | # 3. 返回这样格式的单词对:[ENGLISH, SPANISH] 67 | def create_dataset(path, num_examples): 68 | lines = io.open(path, encoding='UTF-8').read().strip().split('\n') 69 | 70 | word_pairs = [[preprocess_sentence(w) for w in l.split('\t')] for l in lines[:num_examples]] 71 | 72 | return zip(*word_pairs) 73 | 74 | en, sp = create_dataset(path_to_file, None) 75 | print(en[-1]) 76 | print(sp[-1]) 77 | 78 | def max_length(tensor): 79 | return max(len(t) for t in tensor) 80 | 81 | #张量化 82 | def tokenize(lang): 83 | lang_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='') 84 | #单词表 85 | lang_tokenizer.fit_on_texts(lang) 86 | #句子张量 87 | tensor = lang_tokenizer.texts_to_sequences(lang) 88 | 89 | tensor = tf.keras.preprocessing.sequence.pad_sequences(tensor,padding='post') 90 | return tensor, lang_tokenizer 91 | 92 | def load_dataset(path, num_examples=None): 93 | # 创建清理过的输入输出对 94 | targ_lang, inp_lang = create_dataset(path, num_examples) 95 | 96 | input_tensor, inp_lang_tokenizer = tokenize(inp_lang) 97 | target_tensor, targ_lang_tokenizer = tokenize(targ_lang) 98 | 99 | return input_tensor, target_tensor, inp_lang_tokenizer, targ_lang_tokenizer 100 | 101 | num_examples = 50000 102 | #最多10w 103 | input_tensor, target_tensor, inp_lang, targ_lang = load_dataset(path_to_file, num_examples) 104 | 105 | # 计算目标张量的最大长度 (max_length) 106 | max_length_targ, max_length_inp = max_length(target_tensor), max_length(input_tensor) 107 | print(max_length_targ, max_length_inp) 108 | 109 | # 采用 80 - 20 的比例切分训练集和验证集 110 | input_tensor_train, input_tensor_val, target_tensor_train, target_tensor_val = train_test_split(input_tensor, target_tensor, test_size=0.2) 111 | 112 | # 显示长度 113 | print(len(input_tensor_train), len(target_tensor_train), len(input_tensor_val), len(target_tensor_val)) 114 | 115 | def convert(lang, tensor): 116 | for t in tensor: 117 | if t!=0: 118 | print ("%d ----> %s" % (t, lang.index_word[t])) 119 | 120 | print ("Input Language; index to word mapping") 121 | convert(inp_lang, input_tensor_train[0]) 122 | print () 123 | print ("Target Language; index to word mapping") 124 | convert(targ_lang, target_tensor_train[0]) 125 | 126 | #模型参数设置 127 | BUFFER_SIZE = len(input_tensor_train) 128 | BATCH_SIZE = 64 129 | steps_per_epoch = len(input_tensor_train)//BATCH_SIZE 130 | embedding_dim = 256 131 | units = 1024 132 | #+1? 133 | vocab_inp_size = len(inp_lang.word_index)+1 134 | vocab_tar_size = len(targ_lang.word_index)+1 135 | 136 | dataset = tf.data.Dataset.from_tensor_slices((input_tensor_train, target_tensor_train)).shuffle(BUFFER_SIZE) 137 | dataset = dataset.batch(BATCH_SIZE, drop_remainder=True) 138 | 139 | example_input_batch, example_target_batch = next(iter(dataset)) 140 | print(example_input_batch[0]) 141 | print(example_target_batch[0]) 142 | 143 | #编码器 144 | class Encoder(tf.keras.Model): 145 | def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz): 146 | super(Encoder, self).__init__() 147 | self.batch_sz = batch_sz 148 | self.enc_units = enc_units 149 | self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim) 150 | self.gru = tf.keras.layers.GRU(self.enc_units, 151 | return_sequences=True, 152 | return_state=True, 153 | recurrent_initializer='glorot_uniform') 154 | 155 | def call(self, x, hidden): 156 | x = self.embedding(x) 157 | output, state = self.gru(x, initial_state = hidden) 158 | return output, state 159 | 160 | def initialize_hidden_state(self): 161 | return tf.zeros((self.batch_sz, self.enc_units)) 162 | 163 | encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE) 164 | 165 | # 样本输入 166 | sample_hidden = encoder.initialize_hidden_state() 167 | sample_output, sample_hidden = encoder(example_input_batch, sample_hidden) 168 | print ('Encoder output shape: (batch size, sequence length, units) {}'.format(sample_output.shape)) 169 | print ('Encoder Hidden state shape: (batch size, units) {}'.format(sample_hidden.shape)) 170 | 171 | class BahdanauAttention(tf.keras.layers.Layer): 172 | def __init__(self, units): 173 | super(BahdanauAttention, self).__init__() 174 | self.W1 = tf.keras.layers.Dense(units) 175 | self.W2 = tf.keras.layers.Dense(units) 176 | self.V = tf.keras.layers.Dense(1) 177 | 178 | def call(self, query, values): 179 | # 隐藏层的形状 == (批大小,隐藏层大小) 180 | # hidden_with_time_axis 的形状 == (批大小,1,隐藏层大小) 181 | # 这样做是为了执行加法以计算分数 182 | hidden_with_time_axis = tf.expand_dims(query, 1) 183 | 184 | # 分数的形状 == (批大小,最大长度,1) 185 | # 我们在最后一个轴上得到 1, 因为我们把分数应用于 self.V 186 | # 在应用 self.V 之前,张量的形状是(批大小,最大长度,单位) 187 | score = self.V(tf.nn.tanh( 188 | self.W1(values) + self.W2(hidden_with_time_axis))) 189 | 190 | # 注意力权重 (attention_weights) 的形状 == (批大小,最大长度,1) 191 | attention_weights = tf.nn.softmax(score, axis=1) 192 | 193 | # 上下文向量 (context_vector) 求和之后的形状 == (批大小,隐藏层大小) 194 | context_vector = attention_weights * values 195 | context_vector = tf.reduce_sum(context_vector, axis=1) 196 | 197 | return context_vector, attention_weights 198 | 199 | attention_layer = BahdanauAttention(10) 200 | attention_result, attention_weights = attention_layer(sample_hidden, sample_output) 201 | 202 | print("Attention result shape: (batch size, units) {}".format(attention_result.shape)) 203 | print("Attention weights shape: (batch_size, sequence_length, 1) {}".format(attention_weights.shape)) 204 | 205 | class Decoder(tf.keras.Model): 206 | def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz): 207 | super(Decoder, self).__init__() 208 | self.batch_sz = batch_sz 209 | self.dec_units = dec_units 210 | self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim) 211 | self.gru = tf.keras.layers.GRU(self.dec_units, 212 | return_sequences=True, 213 | return_state=True, 214 | recurrent_initializer='glorot_uniform') 215 | self.fc = tf.keras.layers.Dense(vocab_size) 216 | 217 | # 用于注意力 218 | self.attention = BahdanauAttention(self.dec_units) 219 | 220 | def call(self, x, hidden, enc_output): 221 | # 编码器输出 (enc_output) 的形状 == (批大小,最大长度,隐藏层大小) 222 | context_vector, attention_weights = self.attention(hidden, enc_output) 223 | 224 | # x 在通过嵌入层后的形状 == (批大小,1,嵌入维度) 225 | x = self.embedding(x) 226 | 227 | # x 在拼接 (concatenation) 后的形状 == (批大小,1,嵌入维度 + 隐藏层大小) 228 | x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1) 229 | 230 | # 将合并后的向量传送到 GRU 231 | output, state = self.gru(x) 232 | 233 | # 输出的形状 == (批大小,隐藏层大小) 234 | output = tf.reshape(output, (-1, output.shape[2])) 235 | 236 | # 输出的形状 == (批大小,vocab) 237 | x = self.fc(output) 238 | 239 | return x, state, attention_weights 240 | 241 | 242 | decoder = Decoder(vocab_tar_size, embedding_dim, units, BATCH_SIZE) 243 | 244 | sample_decoder_output, _, _ = decoder(tf.random.uniform((64, 1)), 245 | sample_hidden, sample_output) 246 | 247 | print ('Decoder output shape: (batch_size, vocab size) {}'.format(sample_decoder_output.shape)) 248 | 249 | optimizer = tf.keras.optimizers.Adam() 250 | loss_object = tf.keras.losses.SparseCategoricalCrossentropy( 251 | from_logits=True, reduction='none') 252 | 253 | def loss_function(real, pred): 254 | mask = tf.math.logical_not(tf.math.equal(real, 0)) 255 | loss_ = loss_object(real, pred) 256 | 257 | mask = tf.cast(mask, dtype=loss_.dtype) 258 | loss_ *= mask 259 | 260 | return tf.reduce_mean(loss_) 261 | 262 | checkpoint_dir = './training_checkpoints' 263 | checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt") 264 | checkpoint = tf.train.Checkpoint(optimizer=optimizer, 265 | encoder=encoder, 266 | decoder=decoder) 267 | 268 | @tf.function 269 | def train_step(inp, targ, enc_hidden): 270 | loss = 0 271 | 272 | with tf.GradientTape() as tape: 273 | enc_output, enc_hidden = encoder(inp, enc_hidden) 274 | 275 | dec_hidden = enc_hidden 276 | 277 | dec_input = tf.expand_dims([targ_lang.word_index['']] * BATCH_SIZE, 1) 278 | 279 | # 将目标词作为下一个输入 280 | for t in range(1, targ.shape[1]): 281 | # 将编码器输出 (enc_output) 传送至解码器 282 | predictions, dec_hidden, _ = decoder(dec_input, dec_hidden, enc_output) 283 | 284 | loss += loss_function(targ[:, t], predictions) 285 | 286 | dec_input = tf.expand_dims(targ[:, t], 1) 287 | 288 | batch_loss = (loss / int(targ.shape[1])) 289 | 290 | variables = encoder.trainable_variables + decoder.trainable_variables 291 | 292 | gradients = tape.gradient(loss, variables) 293 | 294 | optimizer.apply_gradients(zip(gradients, variables)) 295 | 296 | return batch_loss 297 | 298 | EPOCHS = 10 299 | 300 | for epoch in range(EPOCHS): 301 | start = time.time() 302 | 303 | enc_hidden = encoder.initialize_hidden_state() 304 | total_loss = 0 305 | 306 | for (batch, (inp, targ)) in enumerate(dataset.take(steps_per_epoch)): 307 | batch_loss = train_step(inp, targ, enc_hidden) 308 | total_loss += batch_loss 309 | 310 | if batch % 100 == 0: 311 | print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1, 312 | batch, 313 | batch_loss.numpy())) 314 | # 每 2 个周期(epoch),保存(检查点)一次模型 315 | if (epoch + 1) % 2 == 0: 316 | checkpoint.save(file_prefix = checkpoint_prefix) 317 | 318 | print('Epoch {} Loss {:.4f}'.format(epoch + 1, 319 | total_loss / steps_per_epoch)) 320 | print('Time taken for 1 epoch {} sec\n'.format(time.time() - start)) 321 | 322 | def evaluate(sentence): 323 | attention_plot = np.zeros((max_length_targ, max_length_inp)) 324 | 325 | sentence = preprocess_sentence(sentence) 326 | 327 | inputs = [inp_lang.word_index[i] for i in sentence.split(' ')] 328 | inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs], 329 | maxlen=max_length_inp, 330 | padding='post') 331 | inputs = tf.convert_to_tensor(inputs) 332 | 333 | result = '' 334 | 335 | hidden = [tf.zeros((1, units))] 336 | enc_out, enc_hidden = encoder(inputs, hidden) 337 | 338 | dec_hidden = enc_hidden 339 | dec_input = tf.expand_dims([targ_lang.word_index['']], 0) 340 | 341 | for t in range(max_length_targ): 342 | predictions, dec_hidden, attention_weights = decoder(dec_input, 343 | 344 | # 存储注意力权重以便后面制图 345 | attention_weights = tf.reshape(attention_weights, (-1, )) 346 | attention_plot[t] = attention_weights.numpy() 347 | 348 | predicted_id = tf.argmax(predictions[0]).numpy() 349 | 350 | result += targ_lang.index_word[predicted_id] + ' ' 351 | 352 | if targ_lang.index_word[predicted_id] == '': 353 | return result, sentence, attention_plot 354 | 355 | # 预测的 ID 被输送回模型 356 | dec_input = tf.expand_dims([predicted_id], 0) 357 | 358 | return result, sentence, attention_plot 359 | 360 | # 注意力权重制图函数 361 | def plot_attention(attention, sentence, predicted_sentence): 362 | fig = plt.figure(figsize=(10,10)) 363 | ax = fig.add_subplot(1, 1, 1) 364 | ax.matshow(attention, cmap='viridis') 365 | 366 | fontdict = {'fontsize': 14} 367 | 368 | ax.set_xticklabels([''] + sentence, fontdict=fontdict, rotation=90) 369 | ax.set_yticklabels([''] + predicted_sentence, fontdict=fontdict) 370 | 371 | ax.xaxis.set_major_locator(ticker.MultipleLocator(1)) 372 | ax.yaxis.set_major_locator(ticker.MultipleLocator(1)) 373 | 374 | plt.show() 375 | 376 | def translate(sentence): 377 | result, sentence, attention_plot = evaluate(sentence) 378 | 379 | print('Input: %s' % (sentence)) 380 | print('Predicted translation: {}'.format(result)) 381 | 382 | attention_plot = attention_plot[:len(result.split(' ')), :len(sentence.split(' '))] 383 | plot_attention(attention_plot, sentence.split(' '), result.split(' ')) 384 | 385 | translate(u'esta es mi vida.') 386 | 387 | -------------------------------------------------------------------------------- /强化学习: -------------------------------------------------------------------------------- 1 | 强化学习 2 | MDP--马尔可夫决策过程 3 | R,S,A,P,D 4 | Q learning和p learning 5 | 6 | gym 7 | Gym是OpenAI推出的开源的强化学习的环境生成工具 8 | 9 | #Q learning 10 | import tensorflow as tf 11 | import gym 12 | #环境 13 | env = gym.make('') 14 | #初始化 15 | tf.reset_default_graph() 16 | #输入 17 | inputs = tf.placeholder(shape=[1,ns],dtype=tf.float32) 18 | #模型 19 | MDP 20 | #输出 21 | Qout = MDP(inputs) #长度为na 22 | predict = tf.argmax(Qout,1) 23 | #损失 24 | nextQ = tf.placeholder(shape=[1,na],dtype=tf.float32) 25 | loss = tf.reduce_sum(tf.square(nextQ - Qout)) 26 | #优化器 27 | trainer = tf.train.GradientDescentOptimizer() 28 | op = trainer.minimize(loss) 29 | 30 | rList = [] 31 | with tf.Session() as sess: 32 | sess.run(init) 33 | for i in range(100): 34 | #重置环境 35 | s = env.reset() 36 | rAll = 0 37 | for j in range(100):#s==target 38 | #训练第一个动作 39 | a,allQ = sess.run([predict,Qout],feed_dict={inputs:np.identity(ns)[s:s+1]}) 40 | #获得下一状态和奖励 41 | s1,r= env.step(a) 42 | #获得新的Q的值 43 | Q1 = sess.run(Qout,feed_dict={inputs:np.identity(ns)[s1:s1+1]}) 44 | #得到目标Q 45 | maxQ1 = np.max(Q1) 46 | targetQ = r + y*maxQ1 47 | #训练 48 | _ = sess.run(op,feed_dict={inputs1:np.identity(ns)[s:s+1],nextQ:targetQ}) 49 | rAll += r 50 | s = s1 51 | rList.append(rAll) 52 | 53 | #使用Policy-Gradients 54 | class PolicyGradient: 55 | # 初始化 56 | def __init__(self, n_actions, n_features, learning_rate=0.01, reward_decay=0.95, output_graph=False): 57 | 58 | # 建立 policy gradient 神经网络 59 | def model(self): 60 | #参数 61 | self.ob = tf.placeholder(tf.float32, [None, self.n_features], name="observations") # 接收 observation 62 | self.act = tf.placeholder(tf.int32, [None, ], name="actions_num") # 接收我们在这个回合中选过的 actions 63 | self.vt = tf.placeholder(tf.float32, [None, ], name="actions_value") # 接收每个 state-action 所对应的 value (通过 reward 计算) 64 | #网络 65 | all_act=MDP(self.ob) 66 | self.all_act_prob = tf.nn.softmax(all_act, name='act_prob') 67 | 68 | # 最大化总体 reward (logp * R) 69 | neg_log_prob = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=all_act, labels=self.act) # 所选 action 的概率 -log 值 70 | loss = tf.reduce_mean(neg_log_prob * self.vt) # (vt = 本轮reward + 衰减的未来reward) 引导参数的梯度下降 71 | #优化器 72 | self.train_op = tf.train.AdamOptimizer(self.lr).minimize(loss) 73 | # 选行为 74 | def choose_action(self, observation): 75 | prob_weights = self.sess.run(self.all_act_prob, 76 | feed_dict={self.ob: observation[np.newaxis, :]}) # 所有 action 的概率 77 | action = np.random.choice(range(prob_weights.shape[1]), p=prob_weights.ravel()) # 根据概率来选 action 78 | return action 79 | # transition 80 | def store_transition(self, s, a, r): 81 | self.observations.append(s) 82 | self.actions.append(a) 83 | self.rewards.append(r) 84 | # 衰减回合的 reward 85 | def _discount_and_norm_rewards(self): 86 | discounted_ep_rewards = np.zeros_like(self.rewards) 87 | running_add = 0 88 | for t in reversed(range(0, len(self.rewards))): 89 | #下一轮=以前总reward*gamma+本轮reward 90 | running_add = running_add * self.gamma + self.reward[t] 91 | discounted_ep_rewards[t] = running_add 92 | 93 | # 归一化 94 | discounted_ep_rewards -= np.mean(discounted_ep_rewards) 95 | discounted_ep_rewards /= np.std(discounted_ep_rewards) 96 | return discounted_ep_rewards 97 | # 学习更新参数 98 | def learn(self, s, a, r, s_): 99 | # 衰减, 并标准化这回合的 reward 100 | discounted_ep_rs_norm = self._discount_and_norm_rewards() 101 | 102 | # train on episode 103 | self.sess.run(self.train_op, feed_dict={ 104 | self.ob: np.vstack(self.observations), # shape=[None, n_obs] 105 | self.act: np.array(self.actions), # shape=[None, ] 106 | self.vt: discounted_ep_rs_norm, # shape=[None, ] 107 | }) 108 | 109 | self.observations, self.actions, self.ep_rewards = [], [], [] # 清空回合 data 110 | return discounted_ep_rs_norm # 返回这一回合的 state-action value 111 | 112 | -------------------------------------------------------------------------------- /快速执行: -------------------------------------------------------------------------------- 1 | tensorflow快速执行 2 | 3 | 计算图的缺点: 4 | 1.debug困难 5 | 2.与python控制不同,数据结构不同 6 | 3.placeholder,session太麻烦 7 | 8 | x=10 9 | x=tf.Variable(10) 10 | 11 | #循环操作 12 | import tensorflow as tf# version >= 1.50 13 | import tensorflow.contrib.eager as tfe 14 | tfe.enable_eager_execution() 15 | 16 | i = 0 #i=tf.constant(0) 17 | while i < 10: #with tf.Session() as sess: 18 | i = tf.add(i, 1) #for i in range(10): 19 | print(i) #print(sess.run(tf.add(i, 1))) 20 | print('%d'%i) 21 | 22 | #去除placeholder 23 | x = [[3.]] #x = tf.placeholder(tf.float32, shape=[1, 1]) 24 | m = tf.matmul(x, x) #m = tf.matmul(x, x) 25 | #with tf.Session() as sess: 26 | #m_out = sess.run(m, feed_dict={x: [[2.]]}) 27 | print(m) #print(m_out) 28 | 29 | #tensor 像数组一样操作 30 | import numpy as np 31 | x = tf.constant([1, 2, 3]) 32 | 33 | assert type(x.numpy()) == np.ndarray 34 | squared = np.square(x) 35 | 36 | for i in x: 37 | print(i) 38 | for i in squared: 39 | print(i) 40 | 41 | #梯度 42 | x = tfe.Variable(2.0) 43 | def loss(y): 44 | return (y - x) ** 2 45 | #偏导数 46 | grad = tfe.implicit_gradients(loss) 47 | #全导数 48 | grad1 = tfe.gradients_function(loss) 49 | 50 | print(loss(3.)) 51 | print(grad(3.)) 52 | print(grad1(3.)) 53 | 54 | 快速执行本质上就是不用计算图graph 55 | 何时用快速执行? 56 | 1.新创一个自己的模型 57 | 2.python老手,tensorlfow新手,需要快速上手 58 | 59 | 在tensorflow2.0中,默认为快速执行 60 | -------------------------------------------------------------------------------- /池化层和全连接层: -------------------------------------------------------------------------------- 1 | 池化层 2 | 3 | max pooling和average pooling 4 | 5 | 池化区域一般是2*2 6 | 7 | 全连接层 8 | 9 | 一般放在最后 10 | 11 | #如何将卷积层和池化层结合在一起? 12 | stride 13 | 14 | tensorflow中池化层,全连接层函数 15 | 16 | 1.keras系列 17 | tf.keras.layers.average_pooling2d( 18 | inputs, 19 | pool_size, 20 | strides, 21 | padding='valid', 22 | data_format='channels_last', 23 | name=None 24 | ) 25 | #没有变量,没有参数 26 | 27 | tf.keras.layers.max_pooling2d( 28 | inputs, 29 | pool_size, 30 | strides, 31 | padding='valid', 32 | data_format='channels_last', 33 | name=None 34 | ) 35 | 36 | tf.keras.layers.dense( 37 | inputs, 38 | units,#输出的维度 39 | activation=None, 40 | use_bias=True, 41 | kernel_initializer=None, 42 | bias_initializer=tf.zeros_initializer(), 43 | kernel_regularizer=None, 44 | bias_regularizer=None, 45 | activity_regularizer=None, 46 | kernel_constraint=None, 47 | bias_constraint=None, 48 | trainable=True, 49 | name=None, 50 | reuse=None 51 | ) 52 | 53 | 2.tf.nn 54 | 需要自定义权重 55 | tf.nn.avg_pool( 56 | value, 57 | ksize, 58 | strides, 59 | padding, 60 | data_format='NHWC', 61 | name=None 62 | ) 63 | 64 | tf.nn.max_pool( 65 | value, 66 | ksize, 67 | strides, 68 | padding, 69 | data_format='NHWC', 70 | name=None 71 | ) 72 | 73 | #在tf.nn中你能找到全连接层函数吗? 74 | #https://www.tensorflow.org/api_docs/python/tf 75 | 76 | 3.contrib系列 77 | tf.contrib.layers.avg_pool2d( 78 | inputs, 79 | kernel_size, 80 | stride=2, 81 | padding='VALID',#默认与卷积层相反 82 | data_format=DATA_FORMAT_NHWC, 83 | outputs_collections=None, 84 | scope=None 85 | ) 86 | 87 | tf.contrib.layers.max_pool2d( 88 | inputs, 89 | kernel_size, 90 | stride=2, 91 | padding='VALID', 92 | data_format=DATA_FORMAT_NHWC, 93 | outputs_collections=None, 94 | scope=None 95 | ) 96 | 97 | tf.contrib.layers.fully_connected( 98 | inputs, 99 | num_outputs, 100 | activation_fn=tf.nn.relu,# 101 | normalizer_fn=None, 102 | normalizer_params=None, 103 | weights_initializer=initializers.xavier_initializer(), 104 | weights_regularizer=None, 105 | biases_initializer=tf.zeros_initializer(), 106 | biases_regularizer=None, 107 | reuse=None, 108 | variables_collections=None,# 109 | outputs_collections=None,# 110 | trainable=True, 111 | scope=None 112 | ) 113 | 114 | #请使用三种不同接口构建如下神经网络 115 | minst数据集 116 | 1.卷积层,5*5,输出通道:64 117 | 2.最大池化层,2*2 118 | 3.卷积层,3*3,输出通道:128 119 | 4.平均池化层,2*2 120 | 5.全连接层输出通道512 121 | 6.全连接层输出通道10 122 | 7.softmax层 123 | 124 | -------------------------------------------------------------------------------- /线性模型: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | #原始数据 5 | x=range(10) 6 | y=range(10) 7 | #需预测数据 8 | x_predict=[128,256,0.5,0.125] 9 | 10 | data={'x':x,'y':y} 11 | #重置向量图 12 | tf.reset_default_graph() 13 | #为需要输入的数据添加占位符 14 | X = tf.placeholder(tf.float32, name='X') 15 | Y = tf.placeholder(tf.float32, name='Y') 16 | 17 | #为需要训练的参数添加变量 18 | w = tf.get_variable('w', initializer=tf.constant(0.0)) 19 | b = tf.get_variable('b', initializer=tf.constant(0.0)) 20 | 21 | #目标函数 22 | Y_predicted = w * X + b 23 | 24 | #损失函数 25 | loss = tf.square(Y - Y_predicted, name='loss') 26 | 27 | #优化器 28 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss) 29 | 30 | #在session中计算 31 | with tf.Session() as sess: 32 | #初始化变量 33 | sess.run(tf.global_variables_initializer()) 34 | 35 | feed_dict={X: data['x'], Y:data['y']} 36 | #训练 37 | for i in range(10000): 38 | sess.run(optimizer, feed_dict=feed_dict) 39 | #每步中输出损失 40 | #loss_out=sess.run(loss,feed_dict=feed_dict) 41 | 42 | w_out, b_out = sess.run([w, b]) 43 | #预测 44 | feed_dict={X: x_predict} 45 | y_predict=sess.run(Y_predicted,feed_dict=feed_dict) 46 | print(y_predict) 47 | -------------------------------------------------------------------------------- /自定义模型: -------------------------------------------------------------------------------- 1 | from tensorflow.keras import layers 2 | 3 | #自定义线性层 4 | class Linear(layers.Layer): 5 | def __init__(self, units=32, input_dim=28): 6 | super(Linear, self).__init__() 7 | w_init = tf.random_normal_initializer() 8 | self.w = tf.Variable(initial_value=w_init(shape=(input_dim, units),dtype='float32'),trainable=False) 9 | #self.w = self.add_weight(shape=(input_dim, units),initializer='random_normal',trainable=True) 10 | b_init = tf.zeros_initializer() 11 | self.b = tf.Variable(initial_value=b_init(shape=(units,),dtype='float32'),trainable=False) 12 | 13 | def call(self, inputs): 14 | return tf.matmul(inputs, self.w) + self.b 15 | 16 | inputs = tf.ones((2, 2)) 17 | linear_layer = Linear(4, 2) 18 | print(linear_layer.w) 19 | outputs = linear_layer(inputs) 20 | print(linear_layer.w) 21 | 22 | #input_dim未知 23 | class Linear(layers.Layer): 24 | 25 | def __init__(self, units=32): 26 | super(Linear, self).__init__() 27 | self.units = units 28 | 29 | def build(self, input_shape): 30 | self.w = self.add_weight(shape=(input_shape[-1], self.units), 31 | initializer='random_normal', 32 | trainable=True) 33 | self.b = self.add_weight(shape=(self.units,), 34 | initializer='random_normal', 35 | trainable=True) 36 | 37 | def call(self, inputs): 38 | return tf.matmul(inputs, self.w) + self.b 39 | 40 | inputs = tf.ones((2, 2)) 41 | linear_layer = Linear(4) 42 | outputs = linear_layer(inputs) 43 | print(linear_layer.w) 44 | 45 | #模型 46 | class MLPBlock(layers.Layer): 47 | 48 | def __init__(self): 49 | super(MLPBlock, self).__init__() 50 | self.linear_1 = Linear(32) 51 | self.linear_2 = Linear(32) 52 | self.linear_3 = Linear(1) 53 | 54 | def call(self, inputs): 55 | x = self.linear_1(inputs) 56 | x = tf.nn.relu(x) 57 | x = self.linear_2(x) 58 | x = tf.nn.relu(x) 59 | return self.linear_3(x) 60 | 61 | #训练 62 | optimizer = tf.keras.optimizers.SGD(learning_rate=1e-3) 63 | loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True) 64 | 65 | for x_batch_train, y_batch_train in train_dataset: 66 | #取一个batch 67 | with tf.GradientTape() as tape: 68 | logits = layer(x_batch_train) 69 | loss_value = loss_fn(y_batch_train, logits) 70 | #?为什么在with外面 71 | grads = tape.gradient(loss_value, model.trainable_weights) 72 | optimizer.apply_gradients(zip(grads, model.trainable_weights)) 73 | -------------------------------------------------------------------------------- /词向量: -------------------------------------------------------------------------------- 1 | #词向量 2 | 1.one hot 3 | the 100 4 | cat 010 5 | sat 001 6 | 2. unique number 7 | the 1 8 | cat 2 9 | sat 3 10 | 3. Word embeddings 11 | the 0.8 1.0 2.2 12 | cat 1.2 -0.1 4.3 13 | sat 0.4 2.5 -0.9 14 | 15 | from tensorflow import keras 16 | from tensorflow.keras import layers 17 | 18 | import tensorflow_datasets as tfds 19 | tfds.disable_progress_bar() 20 | 21 | embedding_layer = layers.Embedding(1000, 5) 22 | #和全链接层一样吗 23 | 24 | result = embedding_layer(tf.constant([1,2,3])) 25 | result.numpy() 26 | 27 | #result = embedding_layer(tf.constant([1230])) 28 | 29 | result = embedding_layer(tf.constant([[0,1,2],[3,4,5]])) 30 | result.shape 31 | 32 | #https://www.tensorflow.org/datasets/catalog/imdb_reviews 33 | train_data, test_data), info = tfds.load( 34 | 'imdb_reviews/subwords8k', 35 | split = (tfds.Split.TRAIN, tfds.Split.TEST), 36 | with_info=True, as_supervised=True) 37 | 38 | encoder = info.features['text'].encoder 39 | print(len(encoder.subwords)) 40 | 41 | encoder.subwordsa[0:20] 42 | 43 | padded_shapes = ([None],()) 44 | train_batches = train_data.shuffle(1000).padded_batch(10, padded_shapes = padded_shapes) 45 | test_batches = test_data.shuffle(1000).padded_batch(10, padded_shapes = padded_shapes) 46 | 47 | train_batch, train_labels = next(iter(train_batches)) 48 | print(train_batch.shape) 49 | train_batch.numpy() 50 | 51 | train_batch, train_labels = next(iter(train_batches)) 52 | print(train_batch.shape) 53 | train_batch.numpy() 54 | 55 | train_labels 56 | 57 | #建模分析情感 58 | embedding_dim=16 59 | 60 | model = keras.Sequential([ 61 | layers.Embedding(encoder.vocab_size, embedding_dim), 62 | layers.GlobalAveragePooling1D(), 63 | layers.Dense(16, activation='relu'), 64 | layers.Dense(1, activation='sigmoid') 65 | ]) 66 | 67 | model.summary() 68 | 69 | model.compile(optimizer='adam', 70 | loss='binary_crossentropy', 71 | metrics=['accuracy']) 72 | 73 | history = model.fit( 74 | train_batches, 75 | epochs=10, 76 | validation_data=test_batches, validation_steps=20) 77 | 78 | import matplotlib.pyplot as plt 79 | 80 | history_dict = history.history 81 | 82 | acc = history_dict['accuracy'] 83 | val_acc = history_dict['val_accuracy'] 84 | loss = history_dict['loss'] 85 | val_loss = history_dict['val_loss'] 86 | 87 | epochs = range(1, len(acc) + 1) 88 | 89 | plt.figure(figsize=(5,5)) 90 | plt.plot(epochs, loss, 'b', label='Training loss') 91 | plt.plot(epochs, val_loss, 'r', label='Validation loss') 92 | plt.title('Training and validation loss') 93 | plt.xlabel('Epochs') 94 | plt.ylabel('Loss') 95 | plt.legend() 96 | plt.show() 97 | 98 | plt.figure(figsize=(5,5)) 99 | plt.plot(epochs, acc, 'b', label='Training acc') 100 | plt.plot(epochs, val_acc, 'r', label='Validation acc') 101 | plt.title('Training and validation accuracy') 102 | plt.xlabel('Epochs') 103 | plt.ylabel('Accuracy') 104 | plt.legend(loc='lower right') 105 | plt.ylim((0.5,1)) 106 | plt.show() 107 | 108 | e = model.layers[0] 109 | weights = e.get_weights()[0] 110 | print(weights.shape) 111 | 112 | import io 113 | 114 | encoder = info.features['text'].encoder 115 | 116 | out_v = io.open('vecs.tsv', 'w', encoding='utf-8') 117 | out_m = io.open('meta.tsv', 'w', encoding='utf-8') 118 | 119 | for num, word in enumerate(encoder.subwords): 120 | vec = weights[num+1] # skip 0, it's padding. 121 | out_m.write(word + "\n") 122 | out_v.write('\t'.join([str(x) for x in vec]) + "\n") 123 | out_v.close() 124 | out_m.close() 125 | 126 | try: 127 | from google.colab import files 128 | except ImportError: 129 | pass 130 | else: 131 | files.download('vecs.tsv') 132 | files.download('meta.tsv') 133 | -------------------------------------------------------------------------------- /迁移学习: -------------------------------------------------------------------------------- 1 | 迁移学习 2 | 3 | 使用别人已经训练好的模型来训练自己的任务 4 | 5 | 速度快,效果好 6 | 7 | 两种方法 8 | 9 | 1.加层 10 | 11 | 2.fine tune 12 | 13 | from tensorflow.keras.models import Sequential 14 | from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D 15 | from tensorflow.keras.preprocessing.image import ImageDataGenerator 16 | 17 | import os 18 | import numpy as np 19 | import matplotlib.pyplot as plt 20 | 21 | _URL = 'https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip' 22 | 23 | path_to_zip = tf.keras.utils.get_file('cats_and_dogs.zip', origin=_URL, extract=True) 24 | 25 | PATH = os.path.join(os.path.dirname(path_to_zip), 'cats_and_dogs_filtered') 26 | 27 | train_dir = os.path.join(PATH, 'train') 28 | validation_dir = os.path.join(PATH, 'validation') 29 | 30 | #可选模型:densenet、inception、mobilenet、resnet、VGG 31 | base_model = tf.keras.applications.ResNet50(weights='imagenet') 32 | 33 | base_model.summary() 34 | 35 | base_model.trainable = False 36 | 37 | batch_size = 64 38 | epochs = 10 39 | IMG_HEIGHT = 224 40 | IMG_WIDTH = 224 41 | 42 | train_image_generator = ImageDataGenerator() # Generator for our training data 43 | validation_image_generator = ImageDataGenerator() # Generator for our validation data 44 | 45 | train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size, 46 | directory=train_dir, 47 | shuffle=True, 48 | target_size=(IMG_HEIGHT, IMG_WIDTH), 49 | class_mode='binary') 50 | 51 | sample_training_images, _ = next(train_data_gen) 52 | 53 | # This function will plot images in the form of a grid with 1 row and 5 columns where images are placed in each column. 54 | def plotImages(images_arr): 55 | fig, axes = plt.subplots(1, 3, figsize=(10,10)) 56 | axes = axes.flatten() 57 | for img, ax in zip( images_arr, axes): 58 | ax.imshow(img) 59 | ax.axis('off') 60 | plt.tight_layout() 61 | plt.show() 62 | 63 | val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size, 64 | directory=validation_dir, 65 | target_size=(IMG_HEIGHT, IMG_WIDTH), 66 | class_mode='binary') 67 | 68 | sample_testing_images, _ = next(val_data_gen) 69 | plotImages(sample_testing_images[:3]) 70 | 71 | prediction_layer1 = tf.keras.layers.Dense(128,activation='relu') 72 | prediction_layer2 = tf.keras.layers.Dense(1,activation='sigmoid') 73 | 74 | model = tf.keras.Sequential([ 75 | base_model, 76 | prediction_layer1, 77 | prediction_layer2 78 | ]) 79 | 80 | model.summary() 81 | 82 | model.compile(optimizer=tf.keras.optimizers.Adam(), 83 | loss='binary_crossentropy', 84 | metrics=['accuracy']) 85 | 86 | history = model.fit_generator( 87 | train_data_gen, 88 | epochs=epochs, 89 | validation_data=val_data_gen 90 | ) 91 | 92 | acc = history.history['acc'] 93 | val_acc = history.history['val_acc'] 94 | 95 | loss = history.history['loss'] 96 | val_loss = history.history['val_loss'] 97 | 98 | plt.figure(figsize=(8, 8)) 99 | plt.subplot(2, 1, 1) 100 | plt.plot(acc, label='Training Accuracy') 101 | plt.plot(val_acc, label='Validation Accuracy') 102 | plt.legend(loc='lower right') 103 | plt.ylabel('Accuracy') 104 | plt.ylim([min(plt.ylim()),1]) 105 | plt.title('Training and Validation Accuracy') 106 | 107 | plt.subplot(2, 1, 2) 108 | plt.plot(loss, label='Training Loss') 109 | plt.plot(val_loss, label='Validation Loss') 110 | plt.legend(loc='upper right') 111 | plt.ylabel('Cross Entropy') 112 | plt.ylim([0,1.0]) 113 | plt.title('Training and Validation Loss') 114 | plt.xlabel('epoch') 115 | plt.show() 116 | 117 | # fine tune 118 | base_model.trainable = True 119 | 120 | # Let's take a look to see how many layers are in the base model 121 | print("Number of layers in the base model: ", len(base_model.layers)) 122 | 123 | # Fine tune from this layer onwards 124 | fine_tune_at = 150 125 | 126 | # Freeze all the layers before the `fine_tune_at` layer 127 | for layer in base_model.layers[:fine_tune_at]: 128 | layer.trainable = False 129 | 130 | base_model.summary() 131 | 132 | prediction_layer = tf.keras.layers.Dense(1,activation='sigmoid') 133 | model = tf.keras.Sequential([ 134 | base_model, 135 | prediction_layer 136 | ]) 137 | model.compile(optimizer=tf.keras.optimizers.Adam(), 138 | loss='binary_crossentropy', 139 | metrics=['accuracy']) 140 | history = model.fit_generator( 141 | train_data_gen, 142 | epochs=epochs*2, 143 | validation_data=val_data_gen 144 | ) 145 | 146 | acc = history.history['accuracy'] 147 | val_acc = history.history['val_accuracy'] 148 | 149 | loss = history.history['loss'] 150 | val_loss = history.history['val_loss'] 151 | 152 | plt.figure(figsize=(8, 8)) 153 | plt.subplot(2, 1, 1) 154 | plt.plot(acc, label='Training Accuracy') 155 | plt.plot(val_acc, label='Validation Accuracy') 156 | plt.legend(loc='lower right') 157 | plt.ylabel('Accuracy') 158 | plt.ylim([min(plt.ylim()),1]) 159 | plt.title('Training and Validation Accuracy') 160 | 161 | plt.subplot(2, 1, 2) 162 | plt.plot(loss, label='Training Loss') 163 | plt.plot(val_loss, label='Validation Loss') 164 | plt.legend(loc='upper right') 165 | plt.ylabel('loss') 166 | plt.ylim([0,1.0]) 167 | plt.title('Training and Validation Loss') 168 | plt.xlabel('epoch') 169 | plt.show() 170 | 171 | 172 | -------------------------------------------------------------------------------- /风格迁移: -------------------------------------------------------------------------------- 1 | #风格转移 2 | 3 | #‘换脸’ 4 | 5 | #原理: 6 | #生成新的图片 7 | #1.内容与原图接近 8 | #2.风格与要求图片接近 9 | # 10 | #对于CNN 11 | #较深层与内容相关 12 | #较浅层与风格相关 13 | 14 | from __future__ import absolute_import, division, print_function, unicode_literals 15 | #安装最新版tensorflow 16 | !pip install -q tensorflow-gpu==2.0.0-alpha0 17 | import tensorflow as tf 18 | #下载图片或加载本地图片 19 | content_path = tf.keras.utils.get_file('turtle.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Green_Sea_Turtle_grazing_seagrass.jpg') 20 | style_path = tf.keras.utils.get_file('kandinsky.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg') 21 | 22 | import IPython.display as display 23 | 24 | import matplotlib.pyplot as plt 25 | import matplotlib as mpl 26 | mpl.rcParams['figure.figsize'] = (12,12) 27 | mpl.rcParams['axes.grid'] = False 28 | 29 | import numpy as np 30 | import time 31 | import functools 32 | 33 | #加载图片,画图演示 34 | contentimg = tf.io.read_file(content_path) 35 | contentimg = tf.image.decode_image(contentimg, channels=3) 36 | contentimg = tf.image.convert_image_dtype(contentimg, tf.float32) 37 | plt.subplot(1, 2, 1) 38 | plt.imshow(contentimg) 39 | contentimg = contentimg[tf.newaxis, :] 40 | 41 | styleimg = tf.io.read_file(style_path) 42 | styleimg = tf.image.decode_image(styleimg, channels=3) 43 | styleimg = tf.image.convert_image_dtype(styleimg, tf.float32) 44 | plt.subplot(1, 2, 2) 45 | plt.imshow(styleimg) 46 | styleimg = styleimg[tf.newaxis, :] 47 | 48 | #加载vgg模型 49 | vgg = tf.keras.applications.VGG19(include_top=False, weights='imagenet') 50 | 51 | for layer in vgg.layers: 52 | print(layer.name) 53 | 54 | # 内容层 55 | content_layers = ['block5_conv2'] 56 | 57 | # 风格层 58 | style_layers = ['block1_conv1', 59 | 'block2_conv1', 60 | 'block3_conv1', 61 | 'block4_conv1', 62 | 'block5_conv1'] 63 | #尝试一下使用不同的风格层和内容层 64 | num_content_layers = len(content_layers) 65 | num_style_layers = len(style_layers) 66 | 67 | #建立模型输出中间层 68 | def vgg_layers(layer_names): 69 | vgg = tf.keras.applications.VGG19(include_top=False, weights='imagenet') 70 | vgg.trainable = False 71 | 72 | outputs = [vgg.get_layer(name).output for name in layer_names] 73 | 74 | model = tf.keras.Model([vgg.input], outputs) 75 | return model 76 | 77 | #计算风格参数 78 | def gram_matrix(input_tensor): 79 | result = tf.linalg.einsum('bijc,bijd->bcd', input_tensor, input_tensor) 80 | input_shape = tf.shape(input_tensor) 81 | num_locations = tf.cast(input_shape[1]*input_shape[2], tf.float32) 82 | return result/(num_locations) 83 | 84 | #输出风格和内容参数 85 | class StyleContentModel(tf.keras.models.Model): 86 | def __init__(self, style_layers, content_layers): 87 | super(StyleContentModel, self).__init__() 88 | self.vgg = vgg_layers(style_layers + content_layers) 89 | self.style_layers = style_layers 90 | self.content_layers = content_layers 91 | self.num_style_layers = len(style_layers) 92 | self.vgg.trainable = False 93 | 94 | def call(self, inputs): 95 | inputs = inputs*255.0 96 | preprocessed_input = tf.keras.applications.vgg19.preprocess_input(inputs) 97 | outputs = self.vgg(preprocessed_input) 98 | style_outputs, content_outputs = (outputs[:self.num_style_layers], 99 | outputs[self.num_style_layers:]) 100 | 101 | style_outputs = [gram_matrix(style_output) 102 | for style_output in style_outputs] 103 | 104 | content_dict = {content_name:value 105 | for content_name, value 106 | in zip(self.content_layers, content_outputs)} 107 | 108 | style_dict = {style_name:value 109 | for style_name, value 110 | in zip(self.style_layers, style_outputs)} 111 | 112 | return {'content':content_dict, 'style':style_dict} 113 | 114 | #提取特征 115 | extractor = StyleContentModel(style_layers, content_layers) 116 | 117 | results = extractor(tf.constant(contentimg)) 118 | 119 | style_results = results['style'] 120 | #输出提取到的特征 121 | print('Styles:') 122 | for name, output in sorted(results['style'].items()): 123 | print(" ", name) 124 | print(" shape: ", output.numpy().shape) 125 | print(" min: ", output.numpy().min()) 126 | print(" max: ", output.numpy().max()) 127 | print(" mean: ", output.numpy().mean()) 128 | print() 129 | 130 | print("Contents:") 131 | for name, output in sorted(results['content'].items()): 132 | print(" ", name) 133 | print(" shape: ", output.numpy().shape) 134 | print(" min: ", output.numpy().min()) 135 | print(" max: ", output.numpy().max()) 136 | print(" mean: ", output.numpy().mean()) 137 | 138 | #优化 139 | style_targets = extractor(styleimg)['style'] 140 | content_targets = extractor(contentimg)['content'] 141 | 142 | image = tf.Variable(contentimg) 143 | 144 | def clip_0_1(image): 145 | return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0) 146 | #优化器 147 | opt = tf.optimizers.Adam(learning_rate=0.02, beta_1=0.99, epsilon=1e-1) 148 | #内容和风格用不同的系数 149 | style_weight=1e-2 150 | content_weight=1e4 151 | #loss 函数 152 | def style_content_loss(outputs): 153 | style_outputs = outputs['style'] 154 | content_outputs = outputs['content'] 155 | style_loss = tf.add_n([tf.reduce_mean((style_outputs[name]-style_targets[name])**2) 156 | for name in style_outputs.keys()]) 157 | style_loss *= style_weight / num_style_layers 158 | 159 | content_loss = tf.add_n([tf.reduce_mean((content_outputs[name]-content_targets[name])**2) 160 | for name in content_outputs.keys()]) 161 | content_loss *= content_weight / num_content_layers 162 | loss = style_loss + content_loss 163 | return loss 164 | 165 | #更新图片 166 | def train_step(image): 167 | with tf.GradientTape() as tape: 168 | outputs = extractor(image) 169 | loss = style_content_loss(outputs) 170 | 171 | grad = tape.gradient(loss, image) 172 | opt.apply_gradients([(grad, image)]) 173 | image.assign(clip_0_1(image)) 174 | 175 | #训练 176 | for i in range(5): 177 | train_step(image) 178 | 179 | #输出对比 180 | plt.subplot(1, 2, 1) 181 | plt.imshow(image.read_value()[0]) 182 | 183 | contentimg = tf.io.read_file(content_path) 184 | contentimg = tf.image.decode_image(contentimg, channels=3) 185 | contentimg = tf.image.convert_image_dtype(contentimg, tf.float32) 186 | plt.subplot(1, 2, 2) 187 | plt.imshow(contentimg) 188 | contentimg = contentimg[tf.newaxis, :] 189 | --------------------------------------------------------------------------------