本文是初学keras这两天来,自己仿照addition_rnn.py,写的一个实例,数据处理稍微有些不同,但是准确性相比addition_rnn.py 差一点,下面直接贴代码,
解释和注释都在代码里边。
<span style="font-family: Arial, Helvetica, sans-serif;">#coding:utf-8</span>
from keras.models import Sequential
from keras.layers.recurrent import LSTM
from utils import log
from numpy import random
import numpy as np
from keras.layers.core import RepeatVector, TimeDistributedDense, Activation
'''
先用lstm实现一个计算加法的keras版本, 根据addition_rnn.py改写
size: 500
10次: test_acu = 0.3050 base_acu= 0.3600
30次: rest_acu = 0.3300 base_acu= 0.4250
size: 50000
10次: test_acu: loss: 0.4749 - acc: 0.8502 - val_loss: 0.4601 - val_acc: 0.8539
base_acu: loss: 0.3707 - acc: 0.9008 - val_loss: 0.3327 - val_acc: 0.9135
20次: test_acu: loss: 0.1536 - acc: 0.9505 - val_loss: 0.1314 - val_acc: 0.9584
base_acu: loss: 0.0538 - acc: 0.9891 - val_loss: 0.0454 - val_acc: 0.9919
30次: test_acu: loss: 0.0671 - acc: 0.9809 - val_loss: 0.0728 - val_acc: 0.9766
base_acu: loss: 0.0139 - acc: 0.9980 - val_loss: 0.0502 - val_acc: 0.9839
'''
log = log()
#defination the global variable
training_size = 50000
hidden_size = 128
batch_size = 128
layers = 1
maxlen = 7
single_digit = 3
def generate_data():
log.info("generate the questions and answers")
questions = []
expected =