人工智能实践:Tensorflow笔记 Class 6:循环神经网络

目录

6.1 循环核

1.循环核

2.按时间步展开

6.2 循环计算层 

1.层数

2.Tensorflow描述循环计算层

6.3 循环计算过程(Ⅰ) 

步骤

​代码

6.4 循环计算过程(Ⅱ)

步骤

代码

6.5 Embeddng编码

​输入一个字母

6.6 RNN实现股票预测

6.7 LSTM实现股票预测

6.8 GRU实现股票预测


6.1 循环核

复习:卷积就是特征提取器(CBAPD),借助卷积核提取空间特征后,送入全连接网络

1.循环核

参数时间共享,循环层提取时间信息

2.按时间步展开

6.2 循环计算层 

1.层数

向输出方向增长

2.Tensorflow描述循环计算层

 数据送入RNN时,x_train维度=[送入样本数、循环核时间展开步数、每个时间步输入特征个数]

6.3 循环计算过程(Ⅰ) 

步骤

字母预测任务:

词向量空间:

 随机生成why,wxh,whh三个参数矩阵,记忆体个数为3。

过程:

代码

#导入库
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense,SimpleRNN
import matplotlib.pyplot as plt
import os

#输入
input_word='abcde'
#为使字母输入神经网络将其编为数字
w_to_id={'a':0,'b':1,'c':2,'d':3,'e':4}
#将数字编为独热码
id_to_onehot={0:[1.,0.,0.,0.,0.],1:[0.,1.,0.,0.,0.],2:[0.,0.,1.,0.,0.],3:[0.,0.,0.,1.,0.],
              4:[0.,0.,0.,0.,1.]}

#训练用的特征及标签
x_train=[id_to_onehot[w_to_id['a']],id_to_onehot[w_to_id['b']],id_to_onehot[w_to_id['c']],
            id_to_onehot[w_to_id['d']],id_to_onehot[w_to_id['e']]]
y_train=[w_to_id['b'],w_to_id['c'],w_to_id['d'],w_to_id['e'],w_to_id['a']]

#打乱训练集顺序
np.random.seed(7)
np.random.shuffle(x_train)
np.random.seed(7)
np.random.shuffle(y_train)
tf.random.set_seed(7)

#将输入特征变为RNN层期待的形状
#第一个维度:样本数
#第二个维度:循环核时间展开步数
#第三个维度:每个时间步输入特征的个数
x_train=np.reshape(x_train,(len(x_train),1,5)) #5即字母对应的独热码,有五个数
y_train=np.array(y_train)

#搭建循环层
model=tf.keras.Sequential([
    SimpleRNN(3),                      #三个记忆体
    Dense(5,activation='softmax')      #全连接层
])

#设置优化器、准确度函数、损失函数
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

checkpoint_save_path='./checkpoint/rnn_onehot_lprel.ckpt'

#保存模型及其参数
if os.path.exists(checkpoint_save_path+'.index'):
    print('load the model')
    model.load_weights(checkpoint_save_path)

cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                save_weights_only=True,
                                                save_best_only=True,
                                                monitor='loss')
history=model.fit(x_train,y_train,batch_size=32,epochs=100,callbacks=[cp_callback])
model.summary()

file=open('./weights.txt','w')
for v in model.trainable_variables:
    file.write(str(v.name)+'\n')
    file.write(str(v.shape)+'\n')
    file.write(str(v.numpy())+'\n')
file.close()

acc=history.history['sparse_categorical_accuracy']
loss=history.history['loss']
#绘制准确率、损失函数曲线
plt.subplot(1,2,1)
plt.plot(acc,label='training accuracy')
plt.title('training accuracy')
plt.legend()

plt.subplot(1,2,2)
plt.plot(loss,label='training loss')
plt.title('training loss')
plt.legend()
plt.show()

#预测
preNum=int(input('input your number:'))         #输入需要预测的任务量
for i in range(preNum):
    alphabet1=input('input test alphabet:')     #输入一个字母
    alphabet=[id_to_onehot[w_to_id[alphabet1]]] #转换为独热码

    alphabet=np.reshape(alphabet,(1,1,5))       
    result=model.predict([alphabet])        #预测
    pred=tf.argmax(result,axis=1)           #预测结果最大值
    pred=int(pred)
    tf.print(alphabet1+'->'+input_word[pred])

6.4 循环计算过程(Ⅱ)

连续输入四个字母,预测下一个字母

步骤

代码

#导入库
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense,SimpleRNN
import matplotlib.pyplot as plt
import os

#输入
input_word='abcde'
#为使字母输入神经网络将其编为数字
w_to_id={'a':0,'b':1,'c':2,'d':3,'e':4}
#将数字编为独热码
id_to_onehot={0:[1.,0.,0.,0.,0.],1:[0.,1.,0.,0.,0.],2:[0.,0.,1.,0.,0.],3:[0.,0.,0.,1.,0.],
              4:[0.,0.,0.,0.,1.]}

#训练用的特征及标签
x_train=[
        [id_to_onehot[w_to_id['a']],id_to_onehot[w_to_id['b']],id_to_onehot[w_to_id['c']],id_to_onehot[w_to_id['d']]],
        [id_to_onehot[w_to_id['b']],id_to_onehot[w_to_id['c']],id_to_onehot[w_to_id['d']],id_to_onehot[w_to_id['e']]],
        [id_to_onehot[w_to_id['c']],id_to_onehot[w_to_id['d']],id_to_onehot[w_to_id['e']],id_to_onehot[w_to_id['a']]],
        [id_to_onehot[w_to_id['d']],id_to_onehot[w_to_id['e']],id_to_onehot[w_to_id['a']],id_to_onehot[w_to_id['b']]],
        [id_to_onehot[w_to_id['e']],id_to_onehot[w_to_id['a']],id_to_onehot[w_to_id['b']],id_to_onehot[w_to_id['c']]],
        ]

y_train=[w_to_id['e'],w_to_id['a'],w_to_id['b'],w_to_id['c'],w_to_id['d']]

#打乱训练集顺序
np.random.seed(7)
np.random.shuffle(x_train)
np.random.seed(7)
np.random.shuffle(y_train)
tf.random.set_seed(7)

#将输入特征变为RNN层期待的形状
#第一个维度:样本数
#第二个维度:循环核时间展开步数
#第三个维度:每个时间步输入特征的个数
x_train=np.reshape(x_train,(len(x_train),4,5)) #5即字母对应的独热码,有五个数
y_train=np.array(y_train)

#搭建循环层
model=tf.keras.Sequential([
    SimpleRNN(3),                      #三个记忆体
    Dense(5,activation='softmax')      #全连接层
])

#设置优化器、准确度函数、损失函数
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

checkpoint_save_path='./checkpoint/rnn_onehot_lprel.ckpt'

#保存模型及其参数
if os.path.exists(checkpoint_save_path+'.index'):
    print('load the model')
    model.load_weights(checkpoint_save_path)

cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                save_weights_only=True,
                                                save_best_only=True,
                                                monitor='loss')
history=model.fit(x_train,y_train,batch_size=32,epochs=100,callbacks=[cp_callback])
model.summary()

file=open('./weights.txt','w')
for v in model.trainable_variables:
    file.write(str(v.name)+'\n')
    file.write(str(v.shape)+'\n')
    file.write(str(v.numpy())+'\n')
file.close()

acc=history.history['sparse_categorical_accuracy']
loss=history.history['loss']
#绘制准确率、损失函数曲线
plt.subplot(1,2,1)
plt.plot(acc,label='training accuracy')
plt.title('training accuracy')
plt.legend()

plt.subplot(1,2,2)
plt.plot(loss,label='training loss')
plt.title('training loss')
plt.legend()
plt.show()

#预测
preNum=int(input('input your number:'))         #输入需要预测的任务量
for i in range(preNum):
    alphabet1=input('input test alphabet:')     #输入一个字母
    alphabet=[id_to_onehot[w_to_id[a]] for a in alphabet1] #转换为独热码

    alphabet=np.reshape(alphabet,(1,4,5))       
    result=model.predict([alphabet])        #预测
    pred=tf.argmax(result,axis=1)           #预测结果最大值
    pred=int(pred)
    tf.print(alphabet1+'->'+input_word[pred])

6.5 Embeddng编码

独热码具有一定的局限性

使用Embedding编码

输入一个字母

#导入库
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense,SimpleRNN,Embedding5
import matplotlib.pyplot as plt
import os

#输入
input_word='abcde'
#为使字母输入神经网络将其编为数字
w_to_id={'a':0,'b':1,'c':2,'d':3,'e':4}
#将数字编为独热码
id_to_onehot={0:[1.,0.,0.,0.,0.],1:[0.,1.,0.,0.,0.],2:[0.,0.,1.,0.,0.],3:[0.,0.,0.,1.,0.],
              4:[0.,0.,0.,0.,1.]}

#训练用的特征及标签
x_train=[w_to_id['a'],w_to_id['b'],w_to_id['c'],w_to_id['d'],w_to_id['e']]

y_train=[w_to_id['b'],w_to_id['c'],w_to_id['d'],w_to_id['e'],w_to_id['a']]

#打乱训练集顺序
np.random.seed(7)
np.random.shuffle(x_train)
np.random.seed(7)
np.random.shuffle(y_train)
tf.random.set_seed(7)

#将输入特征变为RNN层期待的形状
#第一个维度:样本数
#第二个维度:循环核时间展开步数
#第三个维度:每个时间步输入特征的个数
x_train=np.reshape(x_train,(len(x_train),1)) #5即字母对应的独热码,有五个数
y_train=np.array(y_train)

#搭建循环层
model=tf.keras.Sequential([
    Embedding(5,2),
    SimpleRNN(3),                      #三个记忆体
    Dense(5,activation='softmax')      #全连接层
])

#设置优化器、准确度函数、损失函数
model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

checkpoint_save_path='./checkpoint/rnn_onehot_lprel.ckpt'

#保存模型及其参数
if os.path.exists(checkpoint_save_path+'.index'):
    print('load the model')
    model.load_weights(checkpoint_save_path)

cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                save_weights_only=True,
                                                save_best_only=True,
                                                monitor='loss')
history=model.fit(x_train,y_train,batch_size=32,epochs=100,callbacks=[cp_callback])
model.summary()

file=open('./weights.txt','w')
for v in model.trainable_variables:
    file.write(str(v.name)+'\n')
    file.write(str(v.shape)+'\n')
    file.write(str(v.numpy())+'\n')
file.close()

acc=history.history['sparse_categorical_accuracy']
loss=history.history['loss']
#绘制准确率、损失函数曲线
plt.subplot(1,2,1)
plt.plot(acc,label='training accuracy')
plt.title('training accuracy')
plt.legend()

plt.subplot(1,2,2)
plt.plot(loss,label='training loss')
plt.title('training loss')
plt.legend()
plt.show()

#预测
preNum=int(input('input your number:'))         #输入需要预测的任务量
for i in range(preNum):
    alphabet1=input('input test alphabet:')     #输入一个字母
    alphabet=[w_to_id[alphabet1]] #转换为独热码

    alphabet=np.reshape(alphabet,(1,1))       
    result=model.predict([alphabet])        #预测
    pred=tf.argmax(result,axis=1)           #预测结果最大值
    pred=int(pred)
    tf.print(alphabet1+'->'+input_word[pred])

6.6 RNN实现股票预测

6.7 LSTM实现股票预测

6.8 GRU实现股票预测

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MOOC(大规模开放式在线课程)是一种通过网络平台开设的在线教育课程,可以为广大学习者提供方便灵活的学习机会。人工智能实践TensorFlow笔记,是由北京大学推出的一门针对人工智能领域的实践课程,旨在帮助学习者掌握使用TensorFlow框架进行深度学习的基本方法和技巧。 该课程的代码提供了一系列丰富的示例和实践项目,通过这些代码我们可以了解和掌握TensorFlow的使用方法。其中包括数据处理、模型构建、模型训练与评估等关键步骤。通过学习和实践,我们可以学会如何搭建神经网络模型,进行图像分类、文本生成等任务。 在这门课程中,北京大学的代码示例主要围绕深度学习的常用库TensorFlow展开,通过给出具体的代码实现,解释了每部分的原理和操作方法,帮助学习者理解基本概念和技术,熟悉TensorFlow框架和编程语言的使用。 此外,这门课程还涵盖了一些实践项目,例如基于TensorFlow的手写数字识别、图像分类与预测、文本生成等。通过完成这些实践项目,我们可以加深对TensorFlow的理解并提高实践能力。 总之,人工智能实践TensorFlow笔记 - 北京大学代码是一门结合了理论与实践的在线课程,通过教授深度学习的基本概念和TensorFlow的应用方法,帮助学习者掌握人工智能领域的基本技能。通过这门课程,我们可以学习到TensorFlow的使用方法,掌握一定的实践能力,并将这些知识应用于实际项目当中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值