LSTM与keras [英] LSTM with keras

查看:173
本文介绍了LSTM与keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些训练数据x_train和与此x_train相关的标签,称为y_train.这是x_trainy_train的构造方式:

I have some training data x_train and some corresponding labels for this x_train called y_train. Here is how x_train and y_train are constructed:

train_x = np.array([np.random.rand(1, 1000)[0] for i in range(10000)])
train_y = (np.random.randint(1,150,10000))

train_x具有10000行,每行1000列. train_y对于train_x中的每个样本在1到150之间有一个标签,并表示每个train_x样本的代码.

train_x has 10000 rows and 1000 columns for each row. train_y has a label between 1 and 150 for each sample in train_x and represents a code for each train_x sample.

我还有一个名为sample的样本,它是1行1000列,我想将其用于此LSTM模型的预测.此变量定义为

I also have a sample called sample, which is 1 row with 1000 columns, which I want to use for prediction on this LSTM model. This variable is defined as

sample = np.random.rand(1,1000)[0]

我正在尝试使用Keras在此数据上训练和预测LSTM.我想使用此特征向量,并使用此LSTM来预测1到150范围内的代码之一.我知道这些是随机数组,但是我无法发布所拥有的数据.我尝试了以下方法,我认为该方法行得通,但遇到了一些问题

I am trying to train and predict an LSTM on this data using Keras. I want to take in this feature vector and use this LSTM to predict one of the codes in range 1 to 150. I know these are random arrays, but I cannot post the data I have. I have tried the following approach which I believe should work, but am facing some issues

    model = Sequential()
    model.add(LSTM(output_dim = 32, input_length = 10000, input_dim = 1000,return_sequences=True))
    model.add(Dense(150, activation='relu'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
    history = model.fit(train_x, train_y,
              batch_size=128, nb_epoch=1,
              verbose = 1)
    model.predict(sample)

对该管道的任何帮助或调整都将非常有用.我不确定output_dim是否正确.我想对1000维数据的每个样本传递LSTM训练,然后重现1到150范围内的特定代码.谢谢.

Any help or adjustments to this pipeline would be great. I am not sure if the output_dim is correct. I want to pass train the LSTM on each sample of the 1000 dimension data and then reproduce a specific code that is in range 1 to 150. Thank you.

推荐答案

我看到至少需要更改三件事:

I see at least three things you need to change:

  1. 更改此行:

  1. Change this line:

model.add(Dense(150, activation='relu'))

收件人:

model.add(Dense(150, activation='softmax'))

'relu'保留为激活状态会使输出不受限制,而它需要具有概率解释(当您使用categorical_crossentropy时).

as leaving 'relu' as activation makes your output unbounded whereas it needs to have a probabilistic interpretation (as you use categorical_crossentropy).

更改损失或目标:

在使用categorical_crossentropy时,您需要将目标更改为长度为150的单热编码矢量.另一种方法是保留目标,但将损耗更改为sparse_categorical_crossentropy .

As you are using categorical_crossentropy you need to change your target to be a one-hot encoded vector of length 150. Another way is to leave your target but to change loss to sparse_categorical_crossentropy.

更改目标范围:

Keras具有基于0的数组索引(如PythonCC++中那样,因此您的值应在[0, 150)范围内,而不是[1, 150].

Keras has a 0-based array indexing (as in Python, C and C++ so your values should be in range [0, 150) instead [1, 150].

这篇关于LSTM与keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆