Using LSTM with a feature vector ?
Hello,
I'm trying to implement an LSTM application but I have some troubles. I adapted the code of the warp-ctc example.
I have a (100, 16, 25088) feature vector reshaped into (100, 16*250880) where 16 is the length of the sequence and a (100,) label vector with 4 categories.
Here are my iterator and the LSTM_unroll
class SimpleBatch(object):
def __init__(self, data_names, data, label_names, label):
self.data = data
self.label = label
self.data_names = data_names
self.label_names = label_names
self.pad = 0
self.index = None # TODO: what is index?
@property
def provide_data(self):
return [(n, x.shape) for n, x in zip(self.data_names, self.data)]
@property
def provide_label(self):
return [(n, x.shape) for n, x in zip(self.label_names, self.label)]
class CNNIter(mx.io.DataIter):
def __init__(self,count, data, label, batch_size, num_label, init_states):
super(CNNIter, self).__init__()
self.data = data
self.label = label
self.batch_size = batch_size
self.count = count
self.num_label = num_label
self.init_states = init_states
self.init_state_arrays = [mx.nd.zeros(x[1]) for x in init_states]
self.provide_data = [('data', (self.batch_size, data.shape[1]))] + init_states
self.provide_label = [('softmax_label', (self.batch_size,))]
def __iter__(self):
print 'iter'
init_state_names = [x[0] for x in self.init_states]
for k in range(self.count):
data = []
label = []
seq = random.sample(range(self.data.shape[0]), self.batch_size)
for i in range(self.batch_size):
data.append(self.data[seq[i],:])
label.append(self.label[seq[i]])
data_all = [mx.nd.array(data)] + self.init_state_arrays
label_all = [mx.nd.array(label)]
data_names = ['data'] + init_state_names
label_names = ['softmax_label']
data_batch = SimpleBatch(data_names, data_all, label_names, label_all)
yield data_batch
def reset(self):
pass
def lstm_unroll(num_lstm_layer, seq_len,
num_hidden, num_label):
param_cells = []
last_states = []
for i in range(num_lstm_layer):
param_cells.append(LSTMParam(i2h_weight=mx.sym.Variable("l%d_i2h_weight" % i),
i2h_bias=mx.sym.Variable("l%d_i2h_bias" % i),
h2h_weight=mx.sym.Variable("l%d_h2h_weight" % i),
h2h_bias=mx.sym.Variable("l%d_h2h_bias" % i)))
state = LSTMState(c=mx.sym.Variable("l%d_init_c" % i),
h=mx.sym.Variable("l%d_init_h" % i))
last_states.append(state)
assert(len(last_states) == num_lstm_layer)
# embeding layer
data = mx.sym.Variable('data')
label = mx.sym.Variable('softmax_label')
wordvec = mx.sym.SliceChannel(data=data, num_outputs=seq_len, squeeze_axis=1)
hidden_all = []
for seqidx in range(seq_len):
hidden = wordvec[seqidx]
for i in range(num_lstm_layer):
next_state = lstm(num_hidden, indata=hidden,
prev_state=last_states[i],
param=param_cells[i],
seqidx=seqidx, layeridx=i)
hidden = next_state.h
last_states[i] = next_state
hidden_all.append(hidden)
hidden_concat = mx.sym.Concat(*hidden_all, dim=0)
pred = mx.sym.FullyConnected(data=hidden_concat, num_hidden=num_label)
label = mx.sym.Reshape(data=label, shape=(-1,))
label = mx.sym.Cast(data = label, dtype = 'int32')
sm = mx.sym.SoftmaxOutput(data=pred, label = label, name='softmax')
return sm
lstm = lstm_unroll(num_lstm_layer = num_lstm_layer, seq_len = seq_len, num_hidden = num_hidden, num_label = num_label)
train = CNNIter(10,X_train,labels_train,batch_size=batch_size, num_label = num_label, init_states = init_states)
model = mx.model.FeedForward(ctx = mx.cpu(), symbol = lstm, num_epoch=num_epoch, learning_rate=learning_rate, momentum=momentum, wd=0.00001)
model.fit(X = train)
When I run the code, I have an "InferShape Error in softmax's label argument". I tried to use a (100,16) label vector but it doesn't work.
Do you have any idea of what could be the problem ?
Thank you very much
You can try " label = mx.sym.Variable('softmax_label', shape=(100, 16)) ". And I think before you reshape the label you should transpose it.
I have the same question. Do you have a solution? How to train my own data in the lstm?
I haven't been able to figure this out either. I start off with a dataframe where one column is the label or actual outcome, and 360 other columns where each one is a feature. This is probably a dumb question at this point but can someone help illuminate?
This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!