循环神经网络-字母预测3

import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense,SimpleRNN,Embedding

input_word="abcde"
w_to_id={"a":0,"b":1,"c":2,"d":3,"e":4}
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(425)
np.random.shuffle(x_train)
np.random.seed(425)
np.random.shuffle(y_train)
np.random.seed(425)

x_train=np.reshape(x_train,(len(x_train),1))
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.RNNNet2/RNNNet2.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=1000,callbacks=[cp_callback])
model.summary()
print(model.trainable_variables)
file = open('./weights.RNNNet2.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()

###############################################    show   ###############################################

# 显示训练集和验证集的acc和loss曲线
acc     =history.history['sparse_categorical_accuracy']
loss    =history.history['loss']

plt.subplot(1, 2, 1)
plt.plot(acc,    label='Training Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(loss,    label='Training Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()

preNum=int(input("Input the number of test alphabet:"))
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])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值