基于tensorflow的LSTM实现PTB预测

#%%
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================import time
import numpy as np
import tensorflow as tf
import reader#flags = tf.flags
#logging = tf.logging#flags.DEFINE_string("save_path", None,
#                    "Model output directory.")
#flags.DEFINE_bool("use_fp16", False,
#                  "Train using 16-bit floats instead of 32bit floats")#FLAGS = flags.FLAGS#def data_type():
#  return tf.float16 if FLAGS.use_fp16 else tf.float32class PTBInput(object):"""The input data."""def __init__(self, config, data, name=None):self.batch_size = batch_size = config.batch_sizeself.num_steps = num_steps = config.num_stepsself.epoch_size = ((len(data) // batch_size) - 1) // num_stepsself.input_data, self.targets = reader.ptb_producer(data, batch_size, num_steps, name=name)class PTBModel(object):"""The PTB model."""def __init__(self, is_training, config, input_):self._input = input_batch_size = input_.batch_sizenum_steps = input_.num_stepssize = config.hidden_sizevocab_size = config.vocab_size# Slightly better results can be obtained with forget gate biases# initialized to 1 but the hyperparameters of the model would need to be# different than reported in the paper.def lstm_cell():return tf.contrib.rnn.BasicLSTMCell(size, forget_bias=0.0, state_is_tuple=True)attn_cell = lstm_cellif is_training and config.keep_prob < 1:def attn_cell():return tf.contrib.rnn.DropoutWrapper(lstm_cell(), output_keep_prob=config.keep_prob)cell = tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(config.num_layers)], state_is_tuple=True)self._initial_state = cell.zero_state(batch_size, tf.float32)with tf.device("/cpu:0"):embedding = tf.get_variable("embedding", [vocab_size, size], dtype=tf.float32)inputs = tf.nn.embedding_lookup(embedding, input_.input_data)if is_training and config.keep_prob < 1:inputs = tf.nn.dropout(inputs, config.keep_prob)# Simplified version of models/tutorials/rnn/rnn.py's rnn().# This builds an unrolled LSTM for tutorial purposes only.# In general, use the rnn() or state_saving_rnn() from rnn.py.## The alternative version of the code below is:## inputs = tf.unstack(inputs, num=num_steps, axis=1)# outputs, state = tf.nn.rnn(cell, inputs,#                            initial_state=self._initial_state)outputs = []state = self._initial_statewith tf.variable_scope("RNN"):for time_step in range(num_steps):if time_step > 0: tf.get_variable_scope().reuse_variables()(cell_output, state) = cell(inputs[:, time_step, :], state)outputs.append(cell_output)output = tf.reshape(tf.concat(outputs, 1), [-1, size])self.output=outputsoftmax_w = tf.get_variable("softmax_w", [size, vocab_size], dtype=tf.float32)softmax_b = tf.get_variable("softmax_b", [vocab_size], dtype=tf.float32)logits = tf.matmul(output, softmax_w) + softmax_bloss = tf.contrib.legacy_seq2seq.sequence_loss_by_example([logits],[tf.reshape(input_.targets, [-1])],[tf.ones([batch_size * num_steps], dtype=tf.float32)])self._cost = cost = tf.reduce_sum(loss) / batch_sizeself._final_state = stateif not is_training:returnself._lr = tf.Variable(0.0, trainable=False)tvars = tf.trainable_variables()grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars),config.max_grad_norm)optimizer = tf.train.GradientDescentOptimizer(self._lr)self._train_op = optimizer.apply_gradients(zip(grads, tvars),global_step=tf.contrib.framework.get_or_create_global_step())self._new_lr = tf.placeholder(tf.float32, shape=[], name="new_learning_rate")self._lr_update = tf.assign(self._lr, self._new_lr)def assign_lr(self, session, lr_value):session.run(self._lr_update, feed_dict={self._new_lr: lr_value})@propertydef input(self):return self._input@propertydef initial_state(self):return self._initial_state@propertydef cost(self):return self._cost@propertydef final_state(self):return self._final_state@propertydef lr(self):return self._lr@propertydef train_op(self):return self._train_opclass SmallConfig(object):"""Small config."""init_scale = 0.1learning_rate = 1.0max_grad_norm = 5num_layers = 2num_steps = 20hidden_size = 200max_epoch = 4max_max_epoch = 13keep_prob = 1.0lr_decay = 0.5batch_size = 20vocab_size = 10000class MediumConfig(object):"""Medium config."""init_scale = 0.05learning_rate = 1.0max_grad_norm = 5num_layers = 2num_steps = 35hidden_size = 650max_epoch = 6max_max_epoch = 39keep_prob = 0.5lr_decay = 0.8batch_size = 20vocab_size = 10000class LargeConfig(object):"""Large config."""init_scale = 0.04learning_rate = 1.0max_grad_norm = 10num_layers = 2num_steps = 35hidden_size = 1500max_epoch = 14max_max_epoch = 55keep_prob = 0.35lr_decay = 1 / 1.15batch_size = 20vocab_size = 10000class TestConfig(object):"""Tiny config, for testing."""init_scale = 0.1learning_rate = 1.0max_grad_norm = 1num_layers = 1num_steps = 2hidden_size = 2max_epoch = 1max_max_epoch = 1keep_prob = 1.0lr_decay = 0.5batch_size = 20vocab_size = 10000def run_epoch(session, model, eval_op=None, verbose=False):"""Runs the model on the given data."""start_time = time.time()costs = 0.0iters = 0state = session.run(model.initial_state)fetches = {"cost": model.cost,"final_state": model.final_state,}if eval_op is not None:fetches["eval_op"] = eval_opfor step in range(model.input.epoch_size):feed_dict = {}for i, (c, h) in enumerate(model.initial_state):feed_dict[c] = state[i].cfeed_dict[h] = state[i].hvals = session.run(fetches, feed_dict)cost = vals["cost"]state = vals["final_state"]costs += costiters += model.input.num_stepsif verbose and step % (model.input.epoch_size // 10) == 10:print("%.3f perplexity: %.3f speed: %.0f wps" %(step * 1.0 / model.input.epoch_size, np.exp(costs / iters),iters * model.input.batch_size / (time.time() - start_time)))return np.exp(costs / iters)raw_data = reader.ptb_raw_data('simple-examples/data/')
train_data, valid_data, test_data, _ = raw_dataconfig = SmallConfig()
eval_config = SmallConfig()
eval_config.batch_size = 1
eval_config.num_steps = 1with tf.Graph().as_default():initializer = tf.random_uniform_initializer(-config.init_scale,config.init_scale)with tf.name_scope("Train"):train_input = PTBInput(config=config, data=train_data, name="TrainInput")with tf.variable_scope("Model", reuse=None, initializer=initializer):m = PTBModel(is_training=True, config=config, input_=train_input)#tf.scalar_summary("Training Loss", m.cost)#tf.scalar_summary("Learning Rate", m.lr)with tf.name_scope("Valid"):valid_input = PTBInput(config=config, data=valid_data, name="ValidInput")with tf.variable_scope("Model", reuse=True, initializer=initializer):mvalid = PTBModel(is_training=False, config=config, input_=valid_input)#tf.scalar_summary("Validation Loss", mvalid.cost)with tf.name_scope("Test"):test_input = PTBInput(config=eval_config, data=test_data, name="TestInput")with tf.variable_scope("Model", reuse=True, initializer=initializer):mtest = PTBModel(is_training=False, config=eval_config,input_=test_input)sv = tf.train.Supervisor()with sv.managed_session() as session:for i in range(config.max_max_epoch):lr_decay = config.lr_decay ** max(i + 1 - config.max_epoch, 0.0)m.assign_lr(session, config.learning_rate * lr_decay)print("Epoch: %d Learning rate: %.3f" % (i + 1, session.run(m.lr)))train_perplexity = run_epoch(session, m, eval_op=m.train_op,verbose=True)print("Epoch: %d Train Perplexity: %.3f" % (i + 1, train_perplexity))valid_perplexity = run_epoch(session, mvalid)print("Epoch: %d Valid Perplexity: %.3f" % (i + 1, valid_perplexity))test_perplexity = run_epoch(session, mtest)print("Test Perplexity: %.3f" % test_perplexity)# if FLAGS.save_path:#   print("Saving model to %s." % FLAGS.save_path)#   sv.saver.save(session, FLAGS.save_path, global_step=sv.global_step)#if __name__ == "__main__":
#  tf.app.run()

将reader.py放到代码相同目录下

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================="""Utilities for parsing PTB text files."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport collections
import os
import sysimport tensorflow as tfPy3 = sys.version_info[0] == 3def _read_words(filename):with tf.gfile.GFile(filename, "r") as f:if Py3:return f.read().replace("\n", "<eos>").split()else:return f.read().decode("utf-8").replace("\n", "<eos>").split()def _build_vocab(filename):data = _read_words(filename)counter = collections.Counter(data)count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))words, _ = list(zip(*count_pairs))word_to_id = dict(zip(words, range(len(words))))return word_to_iddef _file_to_word_ids(filename, word_to_id):data = _read_words(filename)return [word_to_id[word] for word in data if word in word_to_id]def ptb_raw_data(data_path=None):"""Load PTB raw data from data directory "data_path".Reads PTB text files, converts strings to integer ids,and performs mini-batching of the inputs.The PTB dataset comes from Tomas Mikolov's webpage:http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgzArgs:data_path: string path to the directory where simple-examples.tgz hasbeen extracted.Returns:tuple (train_data, valid_data, test_data, vocabulary)where each of the data objects can be passed to PTBIterator."""train_path = os.path.join(data_path, "ptb.train.txt")valid_path = os.path.join(data_path, "ptb.valid.txt")test_path = os.path.join(data_path, "ptb.test.txt")word_to_id = _build_vocab(train_path)train_data = _file_to_word_ids(train_path, word_to_id)valid_data = _file_to_word_ids(valid_path, word_to_id)test_data = _file_to_word_ids(test_path, word_to_id)vocabulary = len(word_to_id)return train_data, valid_data, test_data, vocabularydef ptb_producer(raw_data, batch_size, num_steps, name=None):"""Iterate on the raw PTB data.This chunks up raw_data into batches of examples and returns Tensors thatare drawn from these batches.Args:raw_data: one of the raw data outputs from ptb_raw_data.batch_size: int, the batch size.num_steps: int, the number of unrolls.name: the name of this operation (optional).Returns:A pair of Tensors, each shaped [batch_size, num_steps]. The second elementof the tuple is the same data time-shifted to the right by one.Raises:tf.errors.InvalidArgumentError: if batch_size or num_steps are too high."""with tf.name_scope(name, "PTBProducer", [raw_data, batch_size, num_steps]):raw_data = tf.convert_to_tensor(raw_data, name="raw_data", dtype=tf.int32)data_len = tf.size(raw_data)batch_len = data_len // batch_sizedata = tf.reshape(raw_data[0 : batch_size * batch_len],[batch_size, batch_len])epoch_size = (batch_len - 1) // num_stepsassertion = tf.assert_positive(epoch_size,message="epoch_size == 0, decrease batch_size or num_steps")with tf.control_dependencies([assertion]):epoch_size = tf.identity(epoch_size, name="epoch_size")i = tf.train.range_input_producer(epoch_size, shuffle=False).dequeue()x = tf.strided_slice(data, [0, i * num_steps],[batch_size, (i + 1) * num_steps])x.set_shape([batch_size, num_steps])y = tf.strided_slice(data, [0, i * num_steps + 1],[batch_size, (i + 1) * num_steps + 1])y.set_shape([batch_size, num_steps])return x, y

将simple-examples.tgz下载解压,将其中的simple-examples文件夹放到代码相同目录,然后运行,

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/Deeplearning/projects/tensorflow_projects/7_2_LSTM.py
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:469: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:470: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:471: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:472: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:473: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:476: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.np_resource = np.dtype([("resource", np.ubyte, 1)])
WARNING:tensorflow:From D:/Deeplearning/projects/tensorflow_projects/7_2_LSTM.py:126: get_or_create_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Please switch to tf.train.get_or_create_global_step
2020-05-12 12:06:21.995000: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-05-12 12:06:22.100000: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1060 6GB major: 6 minor: 1 memoryClockRate(GHz): 1.7085
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 5.79GiB
2020-05-12 12:06:22.100000: I C:\tf_jenkins\home\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1)
WARNING:tensorflow:Standard services need a 'logdir' passed to the SessionManager
Epoch: 1 Learning rate: 1.000
0.004 perplexity: 5851.399 speed: 6103 wps
0.104 perplexity: 838.206 speed: 10772 wps
0.204 perplexity: 625.268 speed: 10906 wps
0.304 perplexity: 505.829 speed: 10992 wps
0.404 perplexity: 436.651 speed: 11032 wps
0.504 perplexity: 391.628 speed: 11034 wps
0.604 perplexity: 352.968 speed: 11055 wps
0.703 perplexity: 326.436 speed: 11076 wps
0.803 perplexity: 305.440 speed: 11094 wps
0.903 perplexity: 286.137 speed: 11089 wps
Epoch: 1 Train Perplexity: 271.721
Epoch: 1 Valid Perplexity: 180.890
Epoch: 2 Learning rate: 1.000
0.004 perplexity: 214.130 speed: 10784 wps
0.104 perplexity: 152.788 speed: 11148 wps
0.204 perplexity: 160.312 speed: 11108 wps
0.304 perplexity: 155.377 speed: 11073 wps
0.404 perplexity: 152.412 speed: 11081 wps
0.504 perplexity: 150.029 speed: 11058 wps
0.604 perplexity: 145.367 speed: 11058 wps
0.703 perplexity: 143.269 speed: 11072 wps
0.803 perplexity: 141.295 speed: 11093 wps
0.903 perplexity: 137.624 speed: 11104 wps
Epoch: 2 Train Perplexity: 135.525
Epoch: 2 Valid Perplexity: 145.000
Epoch: 3 Learning rate: 1.000
0.004 perplexity: 147.102 speed: 10758 wps
0.104 perplexity: 106.609 speed: 11249 wps
0.204 perplexity: 115.864 speed: 11178 wps
0.304 perplexity: 113.114 speed: 11193 wps
0.404 perplexity: 111.991 speed: 11209 wps
0.504 perplexity: 111.132 speed: 11193 wps
0.604 perplexity: 108.429 speed: 11190 wps
0.703 perplexity: 107.791 speed: 11179 wps
0.803 perplexity: 107.106 speed: 11178 wps
0.903 perplexity: 104.823 speed: 11165 wps
Epoch: 3 Train Perplexity: 103.823
Epoch: 3 Valid Perplexity: 133.848
Epoch: 4 Learning rate: 1.000
0.004 perplexity: 117.925 speed: 10918 wps
0.104 perplexity: 86.010 speed: 11162 wps
0.204 perplexity: 94.614 speed: 11159 wps
0.304 perplexity: 92.540 speed: 11139 wps
0.404 perplexity: 92.043 speed: 11142 wps
0.504 perplexity: 91.642 speed: 11143 wps
0.604 perplexity: 89.705 speed: 11141 wps
0.703 perplexity: 89.507 speed: 11131 wps
0.803 perplexity: 89.214 speed: 11133 wps
0.903 perplexity: 87.536 speed: 11134 wps
Epoch: 4 Train Perplexity: 86.976
Epoch: 4 Valid Perplexity: 128.205
Epoch: 5 Learning rate: 0.500
0.004 perplexity: 99.482 speed: 11518 wps
0.104 perplexity: 71.572 speed: 11135 wps
0.204 perplexity: 77.783 speed: 11117 wps
0.304 perplexity: 75.010 speed: 11095 wps
0.404 perplexity: 73.936 speed: 11080 wps
0.504 perplexity: 72.969 speed: 11070 wps
0.604 perplexity: 70.799 speed: 11084 wps
0.703 perplexity: 70.063 speed: 11085 wps
0.803 perplexity: 69.277 speed: 11094 wps
0.903 perplexity: 67.383 speed: 11091 wps
Epoch: 5 Train Perplexity: 66.387
Epoch: 5 Valid Perplexity: 119.345
Epoch: 6 Learning rate: 0.250
0.004 perplexity: 80.949 speed: 11253 wps
0.104 perplexity: 59.010 speed: 11037 wps
0.204 perplexity: 64.474 speed: 11000 wps
0.304 perplexity: 62.112 speed: 10997 wps
0.404 perplexity: 61.141 speed: 11029 wps
0.504 perplexity: 60.229 speed: 11038 wps
0.604 perplexity: 58.343 speed: 11016 wps
0.703 perplexity: 57.627 speed: 11033 wps
0.803 perplexity: 56.841 speed: 11044 wps
0.903 perplexity: 55.117 speed: 11046 wps
Epoch: 6 Train Perplexity: 54.139
Epoch: 6 Valid Perplexity: 118.480
Epoch: 7 Learning rate: 0.125
0.004 perplexity: 71.864 speed: 11168 wps
0.104 perplexity: 52.413 speed: 11129 wps
0.204 perplexity: 57.320 speed: 11180 wps
0.304 perplexity: 55.204 speed: 11151 wps
0.404 perplexity: 54.303 speed: 11095 wps
0.504 perplexity: 53.469 speed: 11104 wps
0.604 perplexity: 51.751 speed: 11066 wps
0.703 perplexity: 51.083 speed: 11001 wps
0.803 perplexity: 50.330 speed: 10912 wps
0.903 perplexity: 48.729 speed: 10782 wps
Epoch: 7 Train Perplexity: 47.803
Epoch: 7 Valid Perplexity: 119.730
Epoch: 8 Learning rate: 0.062
0.004 perplexity: 67.433 speed: 11311 wps
0.104 perplexity: 49.194 speed: 10756 wps
0.204 perplexity: 53.766 speed: 10837 wps
0.304 perplexity: 51.729 speed: 10921 wps
0.404 perplexity: 50.863 speed: 10930 wps
0.504 perplexity: 50.067 speed: 10937 wps
0.604 perplexity: 48.428 speed: 10921 wps
0.703 perplexity: 47.775 speed: 10935 wps
0.803 perplexity: 47.038 speed: 10924 wps
0.903 perplexity: 45.496 speed: 10902 wps
Epoch: 8 Train Perplexity: 44.603
Epoch: 8 Valid Perplexity: 120.403
Epoch: 9 Learning rate: 0.031
0.004 perplexity: 64.861 speed: 11055 wps
0.104 perplexity: 47.546 speed: 11043 wps
0.204 perplexity: 51.967 speed: 10913 wps
0.304 perplexity: 49.953 speed: 10559 wps
0.404 perplexity: 49.107 speed: 10547 wps
0.504 perplexity: 48.320 speed: 10557 wps
0.604 perplexity: 46.724 speed: 10610 wps
0.703 perplexity: 46.066 speed: 10670 wps
0.803 perplexity: 45.328 speed: 10714 wps
0.903 perplexity: 43.817 speed: 10738 wps
Epoch: 9 Train Perplexity: 42.936
Epoch: 9 Valid Perplexity: 120.310
Epoch: 10 Learning rate: 0.016
0.004 perplexity: 63.397 speed: 10577 wps
0.104 perplexity: 46.576 speed: 10926 wps
0.204 perplexity: 50.966 speed: 10927 wps
0.304 perplexity: 48.964 speed: 10886 wps
0.404 perplexity: 48.134 speed: 10857 wps
0.504 perplexity: 47.355 speed: 10895 wps
0.604 perplexity: 45.795 speed: 10895 wps
0.703 perplexity: 45.138 speed: 10849 wps
0.803 perplexity: 44.399 speed: 10865 wps
0.903 perplexity: 42.907 speed: 10879 wps
Epoch: 10 Train Perplexity: 42.034
Epoch: 10 Valid Perplexity: 119.880
Epoch: 11 Learning rate: 0.008
0.004 perplexity: 62.544 speed: 11253 wps
0.104 perplexity: 45.981 speed: 10952 wps
0.204 perplexity: 50.366 speed: 10979 wps
0.304 perplexity: 48.381 speed: 11025 wps
0.404 perplexity: 47.563 speed: 11005 wps
0.504 perplexity: 46.790 speed: 11007 wps
0.604 perplexity: 45.252 speed: 11011 wps
0.703 perplexity: 44.602 speed: 11022 wps
0.803 perplexity: 43.871 speed: 11013 wps
0.903 perplexity: 42.392 speed: 11001 wps
Epoch: 11 Train Perplexity: 41.527
Epoch: 11 Valid Perplexity: 119.427
Epoch: 12 Learning rate: 0.004
0.004 perplexity: 62.054 speed: 10973 wps
0.104 perplexity: 45.632 speed: 10424 wps
0.204 perplexity: 50.013 speed: 10452 wps
0.304 perplexity: 48.048 speed: 10299 wps
0.404 perplexity: 47.240 speed: 10300 wps
0.504 perplexity: 46.472 speed: 10436 wps
0.604 perplexity: 44.947 speed: 10523 wps
0.703 perplexity: 44.301 speed: 10582 wps
0.803 perplexity: 43.579 speed: 10621 wps
0.903 perplexity: 42.108 speed: 10657 wps
Epoch: 12 Train Perplexity: 41.247
Epoch: 12 Valid Perplexity: 119.145
Epoch: 13 Learning rate: 0.002
0.004 perplexity: 61.794 speed: 10945 wps
0.104 perplexity: 45.438 speed: 10817 wps
0.204 perplexity: 49.814 speed: 10845 wps
0.304 perplexity: 47.862 speed: 10831 wps
0.404 perplexity: 47.062 speed: 10767 wps
0.504 perplexity: 46.300 speed: 10803 wps
0.604 perplexity: 44.782 speed: 10825 wps
0.703 perplexity: 44.139 speed: 10831 wps
0.803 perplexity: 43.422 speed: 10864 wps
0.903 perplexity: 41.956 speed: 10874 wps
Epoch: 13 Train Perplexity: 41.097
Epoch: 13 Valid Perplexity: 119.008
Test Perplexity: 114.313Process finished with exit code 0

基于tensorflow的LSTM实现PTB预测相关推荐

  1. TensorFlow搭建LSTM实现时间序列预测(负荷预测)

    目录 I. 前言 II. 数据处理 III. 模型 IV. 训练/测试 V. 源码及数据 I. 前言 前面已经写过不少时间序列预测的文章: 深入理解PyTorch中LSTM的输入和输出(从input输 ...

  2. 基于Keras的LSTM多变量时间序列预测(北京PM2.5数据集pollution.csv)

                                 基于Keras的LSTM多变量时间序列预测 传统的线性模型难以解决多变量或多输入问题,而神经网络如LSTM则擅长于处理多个变量的问题,该特性使 ...

  3. 【基于MATLAB实现LSTM光伏输出功率预测】

    基于MATLAB实现LSTM(长短记忆网络)光伏输出功率预测 背景 近年来,光伏市场在全世界的规模迅速增长.2020 年中国宣布采取新政策来提高国家的贡献力,目标是在2030 年前实现碳达峰,在206 ...

  4. 情感分析之电影评论分析-基于Tensorflow的LSTM

    1. 深度学习在自然语言处理中的应用 自然语言处理是教会机器如何去处理或者读懂人类语言的系统,目前比较热门的方向,包括如下几类: 对话系统 - 比较著名的案例有:Siri,Alexa 和 Cortan ...

  5. 基于Keras的LSTM多变量时间序列预测

    LSTM是一种时间递归神经网络,它出现的原因是为了解决RNN的一个致命的缺陷.原生的RNN会遇到一个很大的问题,叫做The vanishing gradient problem for RNNs,也就 ...

  6. 如何基于TensorFlow使用LSTM和CNN实现时序分类任务

    https://www.jiqizhixin.com/articles/2017-09-12-5 By 蒋思源2017年9月12日 09:54 时序数据经常出现在很多领域中,如金融.信号处理.语音识别 ...

  7. 【人工智能笔记】第五节:基于TensorFlow 2.0进行股票预测(JIT与Eager双模式实现)

    该模型是典型的数据预测模型.实现多参数输入含时序,预测多个结果数据. 输入维度(batch_size,历史数据长度,输入参数数量),这里取15个维度,包含:年.月.日.上证指数.深证指数与目标股票(开 ...

  8. 基于Keras的LSTM多变量时间序列预测,包括单步和多步(北京PM2.5数据集pollution.csv)

    直接简单点转自其他博客 大神 Jason Brownlee的杰作:https://machinelearningmastery.com/multivariate-time-series-forecas ...

  9. 基于keras 搭建LSTM GRU模型预测 共享单车使用情况 完整代码+数据 数据分析 计算机毕设

    项目运行教程:https://www.bilibili.com/video/BV1nT411k7dT/?spm_id_from=333.999.0.0 附完整代码数据:

  10. 手把手教你:基于LSTM的股票预测系统

    系列文章 第七章.手把手教你:基于深度残差网络(ResNet)的水果分类识别系统 第六章.手把手教你:人脸识别的视频打码 第五章.手把手教你:基于深度学习的滚动轴承故障诊断 目录 系列文章 一.项目简 ...

最新文章

  1. [转]JDBC中日期时间的处理技巧
  2. 安装R语言开发环境RStudio服务器版
  3. Prometheus + Granafa 构建高大上的MySQL监控平台
  4. Vue项目中使用Echarts(一)
  5. UC浏览器如何调节手机屏幕亮度
  6. openoffice使用总结001---版本匹配问题unknown document format for file: E:\apache-tomcat-8.5.23\webapps\ZcnsDms\
  7. C#中upd分包与发送,已经实现全部代码
  8. 【渝粤题库】陕西师范大学200261 复变函数 作业(专升本、高起本)
  9. 关于lisp的一些资源
  10. Word VBA:批量转PDF且保留书签
  11. 亲密爱人:《亲密关系》读书笔记
  12. 数据安全之数据分类分级系统建设
  13. 详细解释JavaScript中三元表达式
  14. 扰动观察法怎么写matlab,扰动观察法
  15. 深搜回溯与不回溯的区别
  16. 喷淋系统在安装算量软件中如何计算工程量?
  17. windows系统vmware重装步骤
  18. 精益创业实战 - 第14章 评估产品和市场的匹配程度
  19. 二代测序的原理和简介
  20. 工作多年,对程序员“未来”的一些看法

热门文章

  1. Excel设置图片固定在某个单元格内部
  2. OpenCV物体颜色检测(Python)
  3. 图森面试官| 图森未来首席科学家王乃岩:播下去的种子,早晚会开花
  4. 语音合成(TTS)论文优选:Forward Attention in Sequence- To-Sequence Acoustic Modeling for Speech Synthesis
  5. 微信小程序-仿微信朋友圈
  6. 【高项】沟通管理(ITTO)
  7. 7.5 SNN《脉冲神经网络研究进展综述》笔记
  8. Learning-Based Approximation of Interconnect Delay and Slew in Signoff Timing Tools
  9. 从play_mp3例程出发理解ESP32-ADF的使用方法
  10. 实验吧——安全杂项之“A记录”详解