应用TensorFlow构建lstm

应用TensorFlow构建lstm

import tensorflow as tf
from tensorflow.contrib import rnn

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

learning_rate = 0.001
training_steps = 10000
batch_size = 128
display_step = 200

num_inputs = 28 # 每个时间步的输入数据特征数
timesteps = 28  # 时间步数目
num_hiddens = 64 # LSTM cell中的units数量
num_classes = 10 # 类别数目
num_layers = 2   # 循环神经网络层数

X = tf.placeholder("float", [None, timesteps, num_inputs])
Y = tf.placeholder("float",[None, num_classes])
keep_prob = tf.placeholder(tf.float32, [])

weights = {
    'out': tf.Variable(tf.random_normal([num_hiddens, num_classes]))
}
biases = {
    'out': tf.Variable(tf.random_normal([num_classes]))
}

def lstm(lstm_x, weights, biases, keep_prob):
    # 多层lstm
    multi_cell = []
    for i in range(num_layers):
        # num_units是LSTM输出结果的维度
        lstm_cell = rnn.BasicLSTMCell(num_units=num_hiddens, forget_bias=1.0, state_is_tuple=True)
        # dropout 一般只设置 output_keep_prob
        # 丢弃机制只应该在训练期间使用
        lstm_cell = rnn.DropoutWrapper(cell=lstm_cell,input_keep_prob=1.0,output_keep_prob=keep_prob)
        multi_cell.append(lstm_cell)
    
    #  MultiRNNCell 来实现多层 LSTM    
    mlstm_cell = rnn.MultiRNNCell(multi_cell, state_is_tuple=True)
    # 用全零来初始化state
    init_state = mlstm_cell.zero_state(batch_size, dtype=tf.float32)
    # time_major==False 时, outputs.shape = [batch_size, time_step_size, hidden_size]
    outputs, state = tf.nn.dynamic_rnn(mlstm_cell, inputs=lstm_x, initial_state=init_state, time_major=False) 
    
    # 注意tf.nn.static_rnn中inputs为按时间步展开的[batch_size,input_size],x=tf.unstack(x,axis=1) 
    
    # 取最后一个时间步的输出(batch_size, num_hiddens)
    h_state = outputs[:, -1, :]
    # 或者 h_state = state[-1][1]
    # or 调整维度为 (steps, batch, hidden_size)
    # outputs = tf.transpose(outputs, [1,0,2])
    # h_state = outputs[-1]
    
    # 全连接层,输出(batch_size, num_classes)
    return tf.matmul(h_state, weights['out']) + biases['out']

logits = lstm(X, weights, biases, keep_prob)

# 定义损失及优化器
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
# 计算准确度
correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    
    for step in range(1, training_steps+1):
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # 注意x数据维度
        batch_x = batch_x.reshape((batch_size, timesteps, num_inputs))
        sess.run(train_op, feed_dict={X:batch_x, Y:batch_y, keep_prob:0.9})
        if step % display_step == 0 or step == 1:

            loss, acc = sess.run([loss_op, accuracy], feed_dict={X:batch_x, Y:batch_y, keep_prob:1.})
            print("Step " + str(step) + ", Minibatch Loss= " + \
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))

    print("Optimization Finished!")

    # Calculate accuracy for 128 mnist test images
    test_len = 128
    test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_inputs))
    test_label = mnist.test.labels[:test_len]
    print("Testing Accuracy:", sess.run(accuracy, feed_dict={X: test_data, Y: test_label, keep_prob:1.}))
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要基于 TensorFlow 实现 LSTM,需要使用 TensorFlow 的 RNN 模块。以下是一个简单的 Java 代码片段,演示如何使用 TensorFlowLSTMCell 类构建 LSTM 模型: ``` import org.tensorflow.*; import org.tensorflow.data.*; import org.tensorflow.framework.optimizers.*; import org.tensorflow.op.*; import org.tensorflow.types.*; int inputSize = 10; int numUnits = 32; int batchSize = 16; int sequenceLength = 20; // 创建 LSTM 单元 LSTMCell lstmCell = LSTMCell.create(numUnits); // 创建输入占位符 Placeholder<Float> inputs = Placeholder.create(Shape.of(batchSize, sequenceLength, inputSize), DataType.FLOAT); // 初始化 LSTM 状态 Tensor<Float> zeroState = lstmCell.zeroState(batchSize, DataType.FLOAT); StateTuple<Float, Float> state = StateTuple.create(zeroState, zeroState); // 遍历序列并运行 LSTM for (int i = 0; i < sequenceLength; i++) { // 提取当前时间步的输入 Operand<Float> inputStep = tf.slice(inputs, tf.constant(new int[]{0, i, 0}), tf.constant(new int[]{batchSize, 1, inputSize})); // 运行 LSTM 单元 LSTMCell.LSTMCellOutput output = lstmCell.apply(inputStep, state); // 更新状态 state = output.state(); } // 提取最终的 LSTM 状态 Operand<Float> finalState = state.c(); // 创建会话并运行图 try (Session session = new Session()) { // ... } ``` 这只是 LSTM 的一个简单实现,实际应用中需要根据具体需求进行修改和优化。同时,需要注意 TensorFlow 的版本和依赖库的兼容性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值