簡體   English   中英

如何在tensorflow RNN中使用numpy數組輸入

[英]How to use numpy array inputs in tensorflow RNN

我只是很好奇如何生成序列,批處理或紀元以將其輸入到張量流模型中,即從numpy數組生成的多層RNN圖。 最初的numpy數組是從熊貓數據集和下面的Sklearn拆分生成的。

從脾氣暴躁的熊貓

#define features and labels using X, Y from a numpy array
 X = Input_Output_Matrix.iloc[:, 0:3].values
 y = np.around(Input_Output_Matrix.iloc[:, 3], decimals=1).values

# Splitting the dataset into the Training set and Test set
  from sklearn.cross_validation import train_test_split
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, 
        random_state = 0)   

注意:非常重要

y_train.shape

出[37] :( 6721,100)

X_train.shape

出[38] :( 6721,3)

現在的形狀

縮放功能以加速模型

 from sklearn.preprocessing import StandardScaler
 sc = StandardScaler()
 X = sc.fit_transform(X)


res = tf.one_hot(indices=y, depth=100)
with tf.Session() as sess:
     y = sess.run(res)

為了生成配置參數。

# Configuration is wrapped in one object for easy tracking and passing.
  class RNNConfig():
       input_size = X_train.shape[1]
       output_size = y_train.shape[1]
       num_steps = 100
       lstm_size = y_train.shape[0]//100
       num_layers = 4
       keep_prob = 0.8
       batch_size = 100
       init_learning_rate = 0.001
       learning_rate_decay = 0.99
       init_epoch = 5
       max_epoch = 5000

DEFAULT_CONFIG = RNNConfig()

用於配置的輸入參數實際上基於numpy數組的形狀,假設3個輸入的input_size = 3,而從一個熱編碼的輸出推導出的output_size = 100,即,深度等於100。

 #one hot encoding to generate 10 columns for the labels

 res = tf.one_hot(indices=y, depth=100)

with tf.Session() as sess:
y = sess.run(res)

with multi_lstm_graph.as_default():

    x_data = tf.placeholder(tf.float32, [None, DEFAULT_CONFIG.num_steps, 
                          DEFAULT_CONFIG.input_size])

    y_label = tf.placeholder(tf.float32, [None, DEFAULT_CONFIG.num_steps, 
                          DEFAULT_CONFIG.output_size])

    learning_rate = tf.placeholder(tf.float32, None)

    def _create_one_cell():
        lstm_cell = tf.contrib.rnn.LSTMCell(config.lstm_size, 
                                           state_is_tuple=True)

        if config.keep_prob < 1.0:
            lstm_cell = tf.contrib.rnn.DropoutWrapper(lstm_cell, 
                              output_keep_prob=config.keep_prob)
        return lstm_cell               
    cell = tf.contrib.rnn.MultiRNNCell([_create_one_cell() for _ in 
               range(config.num_layers)], state_is_tuple=True) if 
                      config.num_layers > 1 else _create_one_cell()

    val, _ = tf.nn.dynamic_rnn(cell, x_data, dtype=tf.float32)

    val = tf.transpose(val, [1, 0, 2])

        last = tf.gather(val, int(val.get_shape()[0]) - 1)

        weight = tf.Variable(tf.truncated_normal([config.lstm_size, 
                  config.input_size]))

        bias = tf.Variable(tf.constant(0.01, shape=[config.input_size]))

        y_pred = tf.matmul(last, weight) + bias

對於圖形特征

張量流功能如下所列,#現在開始訓練

        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits
                 (logits=y_pred, labels=y_label), name="graph_loss")
        optimizer = tf.train.AdamOptimizer(learning_rate)
        minimize = optimizer.minimize(loss )
        tf.summary.scalar("loss_mse", loss)

最后參加培訓

with tf.Session(graph=Multilayer_RNN_Graph_Cell) as sess:    

    tf.global_variables_initializer().run()

配置參數

    learning_rates_to_use = [config.init_learning_rate*
                           (config.learning_rate_decay ** max(
                    float(i + 1 -config.init_epoch), 0.0)) for i in 
                                         range(config.max_epoch)]

    test_data_feed = {inputs: X_test, targets: X_test, learning_rate: 0.0}

這是我嘗試迭代時期的方法。

        for epoch_step in range(DEFAULT_CONFIG.max_epoch):
        current_lr = learning_rates_to_use[epoch_step]

這又是根據輸入數組的形狀(特別是要素數量)進行的批處理。

        for _ in range(int(X_train.shape[0]/config.batch_size)):
            rand_index = np.random.choice(len(X_train), 
                                 size=config.batch_size)
            batch_X = X_train[rand_index].reshape((1, config.num_steps, 
                                 config.input_size))
            #indexing of 1_D np.array
            batch_y = y_train[rand_index].reshape((1, config.num_steps, 
                             config.output_size)) 

            '''Each loop below completes one epoch training.'''
            train_data_feed = {inputs: batch_X,
                                targets: batch_y,
                                learning_rate: 0}    

            '''Each loop below completes one epoch training.'''              
            train_loss, _ = sess.run([loss, minimize], train_data_feed)
            cost_history = np.append(cost_history, train_loss)                

            '''results of the Session'''

            print('Epoch', epoch, 'completed out of', hm_epochs,'loss:', 
                        cost_history)  

            '''In order to test for Model Accuracy '''

        if epoch_step%10 == 0:                
            test_loss, _pred, _summary = sess.run([loss, prediction, 
                             merged_summary], test_data_feed)
            assert len(_pred) == len(y_test)
            print ("Epoch %d [%f]:" % (epoch_step, current_lr), test_loss) 

現在我的輸出了。 我收到以下錯誤。 我的logits_size = [1,3]有一個特別的問題,我不知道它是如何生成的。 它與兩個矩陣都不相關(輸入矩陣X_train或輸出矩陣y_train。)。 我的問題是這如何使logits_size與labels_size = [100,100]匹配。

提前致謝

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
C:\Users\MAULIDI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1326     try:
-> 1327       return fn(*args)
   1328     except errors.OpError as e:

C:\Users\MAULIDI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1305                                    feed_dict, fetch_list, target_list,
-> 1306                                    status, run_metadata)
   1307 

C:\Users\MAULIDI\Anaconda3\lib\contextlib.py in __exit__(self, type, value, traceback)
     87             try:
---> 88                 next(self.gen)
     89             except StopIteration:

C:\Users\MAULIDI\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in raise_exception_on_not_ok_status()
    465           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466           pywrap_tensorflow.TF_GetCode(status))
    467   finally:

InvalidArgumentError: logits and labels must be same size: logits_size=[1,3] labels_size=[100,100]
     [[Node: train/SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](train/Reshape, train/Reshape_1)]]

During handling of the above exception, another exception occurred: 

我認為問題出在代碼的這一部分。

 val = tf.transpose(val, [1, 0, 2])

 last = tf.gather(val, int(val.get_shape()[0]) - 1)

RNN的輸出為(時間步長,batch_index,數據),並且您將轉置為(batch_index,時間步長,數據)。 然后,在軸0上使用index = shape [0]-1進​​行收集(這是默認設置)。 因此,您正在使用批處理的最后一個元素。 您可能要指定到軸1。

做到這一點的另一種方法是使代碼更整潔:

last = val[:, -1, :]

我猜您只是在測試中做了一個步驟,所以應該解釋一下1。我現在看不到任何其他錯誤,所以我猜您的input_size為3,當您進行矩陣乘法時,您會得到[ 1、3]。

檢查重物的形狀是否為(x,100)。 如果您的批次大小為100,則固定這兩個應給出形狀正確的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM