numpy - Keras LSTM 训练数据格式

标签 numpy neural-network keras lstm

我正在尝试使用 LSTM 神经网络(使用 Keras)来预测石头剪刀布游戏中对手的下一步行动。

我将输入编码为石头:[1 0 0],纸:[0 1 0],剪刀:[0 0 1]。现在我想训练神经网络,但我对训练数据的数据结构有点困惑。

我已将对手的游戏历史记录存储在具有以下结构的 .csv 文件中:

1,0,0
0,1,0
0,1,0
0,0,1
1,0,0
0,1,0
0,1,0
0,0,1
1,0,0
0,0,1

我尝试使用每 5 个数据作为训练标签,前 4 个数据作为训练输入。换句话说,在每个时间步,一个维度为 3 的向量被发送到网络,我们有 4 个时间步。

例如下面是输入数据

1,0,0
0,1,0
0,1,0
0,0,1

第五个是训练标签

1,0,0

我的问题是 Keras 的 LSTM 网络接受什么类型的数据格式?为此目的重新排列数据的最佳方法是什么?如果有帮助的话,我的不完整代码如下:

#usr/bin/python
from __future__ import print_function

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.optimizers import Adam

output_dim = 3
input_dim = 3
input_length = 4
batch_size = 20   #use all the data to train in one iteration


#each input has such strcture
#Rock: [1 0 0], Paper: [0 1 0], Scissor: [0 0 1]
#4 inputs (vectors) are sent to the LSTM net and output 1 vector as the prediction

#incomplete function
def read_data():
    raw_training = np.genfromtxt('training_data.csv',delimiter=',')




    print(raw_training)

def createNet(summary=False):
    print("Start Initialzing Neural Network!")
    model = Sequential()
    model.add(LSTM(4,input_dim=input_dim,input_length=input_length,
            return_sequences=True,activation='softmax'))
    model.add(Dropout(0.1))
    model.add(LSTM(4,
            return_sequences=True,activation='softmax'))
    model.add(Dropout(0.1))
    model.add(Dense(3,activation='softmax'))
    model.add(Dropout(0.1))
    model.add(Dense(3,activation='softmax'))
    model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
    if summary:
        print(model.summary())
    return model

if __name__=='__main__':
    createNet(True)

最佳答案

LSTM 的输入格式应具有形状 (sequence_length, input_dim)。 所以在你的情况下,形状 (4,3) 的 numpy 数组应该可以做到。

您将输入模型的内容将是一个形状的 numpy 数组(number_of_train_examples、sequence_length、input_dim)。 换句话说,您将提供形状为 (4,3) 的 number_of_train_examples 个表。 建立一个列表:

1,0,0
0,1,0
0,1,0
0,0,1

然后执行 np.array(list_of_train_example)。

但是,我不明白为什么要返回第二个 LSTM 的整个序列?它会输出形状为 (4,4) 的东西,密集层可能会失败。返回序列意味着您将返回整个序列,即 LSTM 每一步的每个隐藏输出。我会将第二个 LSTM 设置为 False,以便仅获得 Dense 层可以读取的形状 (4,) 的“摘要”向量。 无论如何,即使对于第一个 LSTM,这也意味着使用形状 (4,3) 的输入,您输出的东西具有形状 (4,4),因此您将拥有比该层的输入数据更多的参数......可以'真的不好。

关于激活,我也会使用softmax,但仅在最后一层,softmax用于获取概率作为该层的输出。在 LSTM 和 Dense 之前使用 softmax 并没有什么意义。寻求其他一些非线性,例如“sigmoid”或“tanh”。

这就是我在模型方面要做的事情

def createNet(summary=False):
    print("Start Initialzing Neural Network!")
    model = Sequential()
    model.add(LSTM(4,input_dim=input_dim,input_length=input_length,
            return_sequences=True,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (4,4)
    model.add(LSTM(4,
            return_sequences=False,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (4,)
    model.add(Dense(3,activation='tanh'))
    model.add(Dropout(0.1))
    # output shape : (3,)
    model.add(Dense(3,activation='softmax'))
    # output shape : (3,)
    model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
    if summary:
        print(model.summary())
    return model

关于numpy - Keras LSTM 训练数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140922/

相关文章:

python - Keras NASNet 训练

python - numpy 数组的最大值和最小值

python - CNN 训练精度在训练期间变得更好,但测试精度保持在 40% 左右

python - clojure 使用 scipy 和 numpy

javascript - 如何使用神经网络和突触库制作项目定价估算器

python - 错误 : Could not find a version that satisfies the requirement tensorflow (from versions: none) ERROR: No matching distribution found for tensorflow)

python - pyplot.savefig 与空导出

numpy - InfogainLoss 层

machine-learning - Keras LSTM 输入特征和错误的维度数据输入

python - 我想用keras为深度学习添加一对一层