Understanding LSTM Tensorflow Waveform

I have an X dataset that consists of N = 4000 samples , each sample consists of d = 2 attributes (continuous values) spanning back t = 10 times the steps . I also have corresponding β€œlabels” for each sample, which are also continuous values, in step 11.

At the moment, my dataset has the form X: [4000.20], Y: [4000].

I want to train LSTM with TensorFlow to predict the value of Y (regression), given the 10 previous d-function inputs, but I find it difficult to do this in TensorFlow.

The main problem I'm currently facing is understanding how TensorFlow expects input to be formatted. I have seen various examples such as this , but these examples relate to one large line of continuous time series. My data are different samples, each of which is an independent time series.

+16
source share
2 answers

The documentation tf.nn.dynamic_rnnreads:

inputs: RNN inputs. If time_major == False(by default), it must be a form tensor: [batch_size, max_time, ...]or a nested tuple of such elements.

, [batch_size, 10, 2]. , 4000 , batch_size . - ( ):

batch_size = 32
# batch_size sequences of length 10 with 2 values for each timestep
input = get_batch(X, batch_size).reshape([batch_size, 10, 2])
# Create LSTM cell with state size 256. Could also use GRUCell, ...
# Note: state_is_tuple=False is deprecated;
# the option might be completely removed in the future
cell = tf.nn.rnn_cell.LSTMCell(256, state_is_tuple=True)
outputs, state = tf.nn.dynamic_rnn(cell,
                                   input,
                                   sequence_length=[10]*batch_size,
                                   dtype=tf.float32)

, outputs [batch_size, 10, 256], 256- . state [batch_size, 256]. , , :

predictions = tf.contrib.layers.fully_connected(state.h,
                                                num_outputs=1,
                                                activation_fn=None)
loss = get_loss(get_batch(Y).reshape([batch_size, 1]), predictions)

256 outputs state cell.output_size . cell.state_size. LSTMCell, , . . LSTMCell.

+8

( "" , np.reshape() , . 3D, np.reshape , ).

RNN , "" .

( , ), " ". , 2D- 3D- RNN.

, : 5 (.. - , , ), 2 ( 2 ), :

(df, )

In [1]: import numpy as np                                                           

In [2]: arr = np.random.randint(0,10,20).reshape((5,4))                              

In [3]: arr                                                                          
Out[3]: 
array([[3, 7, 4, 4],
       [7, 0, 6, 0],
       [2, 0, 2, 4],
       [3, 9, 3, 4],
       [1, 2, 3, 0]])

In [4]: import pandas as pd                                                          

In [5]: df = pd.DataFrame(arr, columns=['f1_t1', 'f2_t1', 'f1_t2', 'f2_t2'])         

In [6]: df                                                                           
Out[6]: 
   f1_t1  f2_t1  f1_t2  f2_t2
0      3      7      4      4
1      7      0      6      0
2      2      0      2      4
3      3      9      3      4
4      1      2      3      0

. , RNNs " " - . , . ; : 1, - 2.

, 3D-, , 5 . , -: RNN ( - ) (.. timestep1) (.. timestep2). ... ( ). , , , , . .

. , - df , , , (.. 1 2 1) , 3- 4-, , , , .

In [7]: arrStack1 = arr[:,0:2]                                                       

In [8]: arrStack1                                                                    
Out[8]: 
array([[3, 7],
       [7, 0],
       [2, 0],
       [3, 9],
       [1, 2]])

In [9]: arrStack2 = arr[:,2:4]                                                       

In [10]: arrStack2                                                                   
Out[10]: 
array([[4, 4],
       [6, 0],
       [2, 4],
       [3, 4],
       [3, 0]])

, , , (" "), :

In [11]: arrfinal3D = np.stack([arrStack1, arrStack2])                               

In [12]: arrfinal3D                                                                  
Out[12]: 
array([[[3, 7],
        [7, 0],
        [2, 0],
        [3, 9],
        [1, 2]],

       [[4, 4],
        [6, 0],
        [2, 4],
        [3, 4],
        [3, 0]]])

In [13]: arrfinal3D.shape                                                            
Out[13]: (2, 5, 2)

: , RNN, 2D-.

( :

In [14]: arrfinal3D_1 = np.stack([arr[:,0:2], arr[:,2:4]])                           

In [15]: arrfinal3D_1                                                                
Out[15]: 
array([[[3, 7],
        [7, 0],
        [2, 0],
        [3, 9],
        [1, 2]],

       [[4, 4],
        [6, 0],
        [2, 4],
        [3, 4],
        [3, 0]]])

!

0

Source: https://habr.com/ru/post/1016464/


All Articles