Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法理解LSTM输出。_Python_Keras_Lstm_Sequential - Fatal编程技术网

Python 无法理解LSTM输出。

Python 无法理解LSTM输出。,python,keras,lstm,sequential,Python,Keras,Lstm,Sequential,我有两个包含数字序列的数据集。其中一个是我的X,另一个是Y 比如说, X: 1 0 7 2 4 8 2 0 5 9 2 1 . . . Shape of X is: (10000, 4) Y: 10 24 5 15 7 6 10 4 13 22 6 2 . . . Shape of Y is: (10000, 4) X中的值在0-10范围内,而Y中的值在0-24范围内 我在Keras中使用LSTM实现来训练X和Y。由于LSTM模型要求输入是三维的,我对数据进行了预处理,并将X更改为1

我有两个包含数字序列的数据集。其中一个是我的X,另一个是Y

比如说,

X:
1 0 7 2
4 8 2 0
5 9 2 1
.
.
.

Shape of X is: (10000, 4)

Y:
10 24 5 15
7  6  10 4
13 22 6  2
.
.
.
Shape of Y is: (10000, 4)
X中的值在0-10范围内,而Y中的值在0-24范围内

我在Keras中使用LSTM实现来训练X和Y。由于LSTM模型要求输入是三维的,我对数据进行了预处理,并将X更改为10000、4、10,Y更改为10000、4、24

在预处理为一个热编码后,这些是实际数据,并不代表所使用的数据,例如:

X:
[[[1 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 1 0 ... 0 0 0]
  [1 0 0 ... 0 0 0]]

 [[0 0 1 ... 0 0 0]
  [0 0 0 ... 0 1 0]
  [1 0 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]
  [0 0 0 ... 0 0 0]]

 ...

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 1 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [1 0 0 ... 0 0 0]
  [1 0 0 ... 0 0 0]
  [0 1 0 ... 0 0 0]]]



Y:
[[[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]
  [0 0 0 ... 0 0 0]]

 ...

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 1 0 0]]

 [[0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]]
这是我的LSTM型号的代码:

    X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 1)
    model = Sequential()
    model.add(LSTM(output_dim = 24, input_shape = X_train.shape[1:], return_sequences = True, init = 'glorot_normal', inner_init = 'glorot_normal', activation = 'sigmoid'))
    model.add(LSTM(output_dim = 24, input_shape = X_train.shape[1:], return_sequences = True, init = 'glorot_normal', inner_init = 'glorot_normal', activation = 'sigmoid'))
    model.add(LSTM(output_dim = 24, input_shape = X_train.shape[1:], return_sequences = True, init = 'glorot_normal', inner_init = 'glorot_normal', activation = 'sigmoid'))
    model.add(LSTM(output_dim = 24, input_shape = X_train.shape[1:], return_sequences = True, init = 'glorot_normal', inner_init = 'glorot_normal', activation = 'sigmoid'))
    model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    model.fit(X_train, Y_train, nb_epoch = 500, validation_data = (X_test, Y_test))
    model.save('LSTM500.h5')

    predictions = model.predict(X_test)
    predictions = np.array(predictions, dtype = np.float64)
    predictions = predictions.reshape(2000, 4, 24)
输出:

[[[[0.1552688  0.15805855 0.2013046  ... 0.16005482 0.19403476
    0.        ]
   [0.0458279  0.09995601 0.06456595 ... 0.09573169 0.07952237
    0.        ]
   [0.19871283 0.19968285 0.06270849 ... 0.14653654 0.18313469
    0.        ]
   [0.08407309 0.091876   0.09707277 ... 0.12661831 0.12858406
    0.        ]]

  [[0.15482235 0.14433247 0.18260191 ... 0.15641384 0.20746264
    0.        ]
   [0.03096719 0.05375536 0.05373315 ... 0.05018555 0.07592873
    0.        ]
   [0.20420487 0.17884348 0.13145864 ... 0.17901334 0.19768076
    0.        ]
   [0.03465272 0.06732351 0.02182322 ... 0.06144218 0.07827628
    0.        ]]

  [[0.15116604 0.15068266 0.18474537 ... 0.17088319 0.15841168
    0.        ]
   [0.09633015 0.11277901 0.10069521 ... 0.09309217 0.11326427
    0.        ]
   [0.17512578 0.13187788 0.10418645 ... 0.10735759 0.10635827
    0.        ]
   [0.13673681 0.12714103 0.06212005 ... 0.03213149 0.14153068
    0.        ]]

  ...
预测形状:1,2000,4,24

我将预测数组重新调整为2000,4,24

[[[0.1552688  0.15805855 0.2013046  ... 0.16005482 0.19403476 0.        ]
  [0.0458279  0.09995601 0.06456595 ... 0.09573169 0.07952237 0.        ]
  [0.19871283 0.19968285 0.06270849 ... 0.14653654 0.18313469 0.        ]
  [0.08407309 0.091876   0.09707277 ... 0.12661831 0.12858406 0.        ]]

 [[0.15482235 0.14433247 0.18260191 ... 0.15641384 0.20746264 0.        ]
  [0.03096719 0.05375536 0.05373315 ... 0.05018555 0.07592873 0.        ]
  [0.20420487 0.17884348 0.13145864 ... 0.17901334 0.19768076 0.        ]
  [0.03465272 0.06732351 0.02182322 ... 0.06144218 0.07827628 0.        ]]

 [[0.15116604 0.15068266 0.18474537 ... 0.17088319 0.15841168 0.        ]
  [0.09633015 0.11277901 0.10069521 ... 0.09309217 0.11326427 0.        ]
  [0.17512578 0.13187788 0.10418645 ... 0.10735759 0.10635827 0.        ]
  [0.13673681 0.12714103 0.06212005 ... 0.03213149 0.14153068 0.        ]]

 ...

我似乎不理解我得到的结果。这些数字是多少?预测数组不应该只包含0和1中的值吗?这样,我可以像Y_测试一样检索实际值?谢谢。

这些数字是您从上一层得到的数字,这是一个sigmoid层,sigmoid将返回0到1之间的值,这就是我们在输出中看到的值

如何解释这些价值观

由于您正在馈送并寻找一个热输出,您可以选择最后一个轴上的最大数,并使用np.argmaxprediction,axis=-1获取该轴上的索引值。这将为您提供一个形状为2000,4的numpy数组,每个元素都是介于[0,24,与原始数据的格式相同。这些值是LSTM模型预测的最可能结果

第二大数字将是第二大可能的结果