美文网首页
开始学习RNN

开始学习RNN

作者: 刘大力_ | 来源:发表于2018-04-30 22:33 被阅读0次

    首先从阅读论文开始。

    先后阅读了如下文章

    关于《A Critical Review of Recurrent Neural Networks for Sequence Learning》的阅读理解

    《Understanding LSTM Networks》——文章对 LSTM 结构为什么这样设计,做了一步步的推理解释

    关于《Supervised Sequence Labelling with Recurrent Neural Networks》的阅读理解

    ……一些文章

    然后是一些tensorflow实现RNN或LSTM的例子。

    目前,把普通的神经网络改造成RNN的成果如下。对RNN用tensorflow实现的逻辑可以理顺,但是实现起来有错误,提示维度不匹配。正在检查原因。

    import numpyas np

    import pandasas pd

    import tensorflowas tf

    # 转为onehot编码

    def turn_onehot(df):

    for keyin df.columns:

    oneHot = pd.get_dummies(df[key])

    for oneHotKeyin oneHot.columns:# 防止重名

                oneHot = oneHot.rename(columns={oneHotKey: key +'_' +str(oneHotKey)})

    df = df.drop(key,axis=1)

    df = df.join(oneHot)

    return df

    # 获取一批次的数据

    def get_batch(x_date, y_date, batch):

    global pointer

    x_date_batch = x_date[pointer:pointer + batch]

    y_date_batch = y_date[pointer:pointer + batch]

    pointer = pointer + batch

    return x_date_batch, y_date_batch

    # 生成layer

    def add_layer(input_num, output_num, x, layer, active=None):

    # 生成权重

        with tf.name_scope('layer' + layer +'/W' + layer):

    W = tf.Variable(tf.random_normal([2*input_num, output_num],dtype=tf.float32),name='W' + layer)

    tf.summary.histogram('layer' + layer +'/W' + layer, W)

    # 加入L2正则化

            if isregularization:

    tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(lambda1)(W))

    # 生成偏移量

        with tf.name_scope('layer' + layer +'/b' + layer):

    b = tf.Variable(tf.zeros([output_num]) +0.1,dtype=tf.float32,name='b' + layer)

    tf.summary.histogram('layer' + layer +'/b' + layer, b)

    # 激活

        with tf.name_scope('layer' + layer +'/l' + layer):

    l = active(tf.matmul(x, W) + b)# 使用sigmoid激活函数,备用函数还有relu

            tf.summary.histogram('layer' + layer +'/l' + layer, l)

    return l

    hiddenDim =1000  # 隐藏层神经元数

    lambda1 =0.5  # 正则化超参数

    save_file ='./train_model.ckpt'

    pointer =0

    time_step =1

    istrain =True  # 启用训练模式

    istensorborad =False  # 启用tensorboard

    isregularization =False  # 启用正则化

    if istrain:

    samples =2000

        batch =1  # 每批次的数据输入数量

    else:

    samples =550

        batch =1  # 每批次的数据输入数量

    with tf.name_scope('inputdate-x-y'):

    # 导入

        df = pd.DataFrame(pd.read_csv('GHMX.CSV',header=0))

    # 产生 y_data 值 (n, 1)

        y_date = df['number'].values

    y_date = y_date.reshape((-1,1))

    # 产生 x_data 值 (n, 4+12+31+24)

        df = df.drop('number',axis=1)

    df = turn_onehot(df)

    x_data = df.values

    ###生成神经网络模型

    # 占位符

    with tf.name_scope('inputs'):

    x = tf.placeholder(tf.float32,shape=[None, time_step,71],name='x_input')

    y_ = tf.placeholder(tf.float32,shape=[None,1],name='y_input')

    keep_prob = tf.placeholder(tf.float32,name='keep_prob')

    # 生成神经网络

    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=71,forget_bias=1.0,state_is_tuple=True)

    lstm_cell = tf.nn.rnn_cell.DropoutWrapper(cell=lstm_cell,input_keep_prob=1.0,output_keep_prob=keep_prob)

    mlstm_cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cellfor _in range(3)])

    init_state = mlstm_cell.zero_state(batch,dtype=tf.float32)

    outputs, date = tf.nn.dynamic_rnn(mlstm_cell,inputs=x,initial_state=init_state,time_major=False)

    h_date= outputs[:, -1, :]

    y = add_layer(71,1, h_date,'1', tf.nn.relu)

    # 计算loss

    with tf.name_scope('loss'):

    # loss = tf.reduce_mean(tf.reduce_sum(tf.square(y - y_), name='square'), name='loss')  #损失函数,损失不下降,换用别的函数

    # loss = -tf.reduce_sum(y_*tf.log(y))  #损失仍然不下降

    # loss = -tf.reduce_sum(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)) , name='loss')

        loss = tf.losses.mean_squared_error(labels=y_,predictions=y)

    #tf.add_to_collection('losses', mse_loss)  # 损失集合

    #loss = tf.add_n(tf.get_collection('losses'))

        tf.summary.scalar('loss', loss)

    # 梯度下降

    with tf.name_scope('train_step'):

    train_step = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)# 有效的学习率0.000005

    # 初始化

    init = tf.global_variables_initializer()

    sess = tf.Session()

    if istensorborad:

    merged = tf.summary.merge_all()

    writer = tf.summary.FileWriter('logs/', sess.graph)

    sess.run(init)

    # 保存/读取模型

    saver = tf.train.Saver()

    if not istrain:

    saver.restore(sess, save_file)

    for iin range(samples):

    x_date_batch, y_date_batch = get_batch(x_data, y_date, batch)

    feed_dict = {x: x_date_batch, y_: y_date_batch, keep_prob:1.0}

    if istrain:

    _, loss_value, y_value, y__value = sess.run((train_step, loss, y, y_),feed_dict=feed_dict)

    print('y=', y_value,'----ture=', y__value)

    print(loss_value)

    else:

    loss_value, y_value, y__value = sess.run((loss, y, y_),feed_dict=feed_dict)

    print('y=', y_value,'----ture=', y__value)

    print(loss_value)

    if istensorborad:

    result = sess.run(merged,feed_dict=feed_dict)

    writer.add_summary(result, i)

    # 保存模型

    if istrain:

    saver.save(sess, save_file)

    相关文章

      网友评论

          本文标题:开始学习RNN

          本文链接:https://www.haomeiwen.com/subject/cezglftx.html