我想嘗試bidirectional_rnn來預測時間序列。代碼是: #BiRNN_model.pybidirectional_rnn:輸入必須是一個序列
import tensorflow as tf
類BiRNN(對象):
"""
a bidirection RNN
"""
def __init__(self, in_size, out_size, num_steps=20, cell_size=20, batch_size=50,
num_layers=2, keep_prob=0.5, is_training=True):
"""
:param in_size: int, the dimension of input
:param out_size: int, the dimension of output
:param num_steps: int, the number of time steps
:param cell_size: int, the size of lstm cell
:param batch_size: int, the size of mini bacth
:param num_layers: int, the number of cells
:param keep_prob: float, the keep probability of dropout layer
:param is_training: bool, set True for training model, but False for test model
"""
self.in_size = in_size
self.out_size = out_size
self.num_steps = num_steps
self.cell_size = cell_size
self.batch_size = batch_size
self.num_layers = num_layers
self.keep_prob = keep_prob
self.is_training = is_training
self.__build_model__()
def __build_model__(self):
"""
The inner method to construct the BiRNN model.
"""
# Input and output placeholders
self.x = tf.placeholder(tf.float32, shape=[None, self.num_steps, self.in_size])
self.y = tf.placeholder(tf.float32, shape=[None, self.num_steps, self.out_size])
# Add the first input layer
with tf.variable_scope("input"):
# Reshape x to 2-D tensor
inputs = tf.reshape(self.x, shape=[-1, self.in_size]) # [batch_size*num_steps, in_size]
W, b = self._get_weight_bias(self.in_size, self.cell_size)
inputs = tf.nn.xw_plus_b(inputs, W, b, name="input_xW_plus_b")
# inputs = tf.matmul(inputs,W)+b
# Reshep to 3-D tensor
#inputs = tf.reshape(inputs, shape=[-1, self.num_steps, self.cell_size]) # [batch_size, num_steps, in_size]
inputs = tf.reshape(inputs, shape=[-1, self.in_size])
# Dropout the inputs
if self.is_training and self.keep_prob < 1.0:
inputs = tf.nn.dropout(inputs, keep_prob=self.keep_prob)
#Construct birnn cells
biRNN_fw_cell = tf.contrib.rnn.BasicRNNCell(num_units = self.cell_size)
biRNN_bw_cell = tf.contrib.rnn.BasicRNNCell(num_units = self.cell_size)
if self.is_training and self.keep_prob < 1.0:
fw_cell = tf.contrib.rnn.DropoutWrapper(biRNN_fw_cell, output_keep_prob=self.keep_prob)
bw_cell = tf.contrib.rnn.DropoutWrapper(biRNN_fw_cell, output_keep_prob=self.keep_prob)
cell_Fw = tf.contrib.rnn.MultiRNNCell([fw_cell] * self.num_layers)
cell_Bw = tf.contrib.rnn.MultiRNNCell([bw_cell] * self.num_layers)
#the initial state
self.init_state_fw = cell_Fw.zero_state(self.batch_size, dtype=tf.float32)
self.init_state_bw = cell_Bw.zero_state(self.batch_size, dtype=tf.float32)
#add biRNN layer
with tf.variable_scope("BRNN"):
outputs,final_state_fw,final_state_bw = tf.contrib.rnn.static_bidirectional_rnn(cell_Fw,cell_Bw,inputs,
initial_state_fw =self.init_state_fw,
initial_state_bw = self.init_state_bw
)
self.final_state_fw = final_state_fw
self.final_state_bw = final_state_bw
# Add the output layer
with tf.variable_scope("output"):
output = tf.reshape(outputs, shape=[-1, self.cell_size])
W, b = self._get_weight_bias(self.cell_size, self.out_size)
output = tf.nn.xw_plus_b(output, W, b, name="output")
self.pred = output
losses = tf.contrib.legacy_seq2seq.sequence_loss_by_example([tf.reshape(self.pred, [-1, ])],
[tf.reshape(self.y, [-1, ])],
[tf.ones([self.batch_size * self.num_steps])],
average_across_timesteps=True,
softmax_loss_function=self._ms_cost)
self.cost = tf.reduce_sum(losses)/tf.to_float(self.batch_size)
def _ms_cost(self, y_pred, y_target):
"""The quadratic cost function"""
return 0.5 * tf.square(y_pred - y_target)
def _get_weight_bias(self, in_size, out_size):
"""
Create weight and bias variables
"""
weights = tf.get_variable("weight", shape=[in_size, out_size],
initializer=tf.random_normal_initializer(mean=0.0, stddev=1.0))
biases = tf.get_variable("bias", shape=[out_size, ], initializer=tf.constant_initializer(0.1))
return weights, biases
但是,如果我運行的代碼,這是一個錯誤: 文件」 ../model/ BiRNN_model.py「,行70,build_model initial_state_bw = self.init_state_bw 文件」/home/lucas/.local/lib/python3.5/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn .py「,第328行,在static_bidirectional_rnn中 raise TypeError(」inputs必須是序列「) TypeError:輸入必須是序列
static_bidirectional_rnn中的輸入參數不是序列。我是tensorflow和深度學習的新手,我花了很多天的時間來嘗試修復這個錯誤,但是我失敗了。有人可以幫助我嗎?謝謝。