如何在 tensorflow RNN 中使用 numpy 数组输入

How to use numpy array inputs in tensorflow RNN

我只是好奇如何生成序列、批次和/或时期以输入张量流模型,multi_layer 来自 numpy 数组的 RNN 图。最初 numpy 数组是从 pandas 数据集和下面的 Sklearn 拆分生成的。

从 Numpy 到 Pandas

#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 数组的形状,假设 input_size = 3 用于 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 的输出是 (timestep, batch_index, data) 而你要转置为 (batch_index, timestep, data)。然后你在轴 0 上使用 indices = shape[0] - 1 进行收集(这是默认设置)。所以你正在获取批处理的最后一个元素。您可能想指定轴 1。

另一种使代码更简洁的方法是:

last = val[:, -1, :]

我猜你在测试中只做了一个时间步,所以这应该可以解释 1。 我现在没有看到任何其他错误,所以我猜你的 input_size 是 3 并且当你进行矩阵乘法时你会得到 [1, 3].

检查权重的形状是否类似于 (x, 100)。如果您的批量大小为 100,则修复这两个应该会给出具有正确形状的结果。