Keras使用使用动态LSTM/RNN

padding:

def generate(mtp = 100,batch = 50):#最长时间步,词向量长度为200,batch_size = 50
	origin_input = np.random.random_sample([batch,np.random.randint(mtp/2,mtp),200])#时间长随机从mtp/2-mtp选择
    return pad_sequences(origin_input , mtp ,dtype="float32")#不足mtp的 填充到 mtp 长度,默认值为0.0,注意dtype,默认是int32

padding后的向量进行输入:

mtp = 100
vector_size = 200
ipt = Input(shape = (mtp,vector_size),name = "input")
mask = Masking(mask_value=0.0,name = "mask_padding")(ipt)#将值为0的序列时间过滤掉
bilstm = Bidirectional(LSTM(100),name = "bilstm")(mask)
lstm = LSTM(100)(mask)

你可能感兴趣的:(TensorFlow)