簡體   English   中英

Tensorflow:如何轉換Tensorflow LSTM的輸入數據?

[英]Tensorflow : How to transform the input data for tensorflow LSTM?

所以,我正在嘗試使用tensorflow進行簡單分類,我的疑問是

如果我使用LSTM進行文本分類(例如:情感分類),那么我們進行數據填充,然后將其填充到LSTM tensorflow中,我們使用word_embedding,因此在word_embedding查找2維數據變為3維或2級矩陣變為3級后:

就像我有兩個文字一樣:

import tensorflow as tf

text_seq=[[11,21,43,22,11,4,1,3,5,2,8],[4,2,11,4,11,0,0,0,0,0,0]]  #2x11 

#text_seq are index of words from word_to_index dict

a=tf.get_variable('word_embedding',shape=[50,50],dtype=tf.float32,initializer=tf.random_uniform_initializer(-0.01,0.01))

lookup=tf.nn.embedding_lookup(a,text_seq)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(lookup).shape)

我會得到 :

(2, 11, 50)

我可以輕松地將其輸入LSTM,因為LSTM接受等級3

但是我的問題是如果我有數值浮點數據而不是文本數據,並且我想使用RNN進行分類,

因此,假設我的數據是:

import numpy as np

float_data=[[11.1,21.5,43.6,22.1,11.44],[33.5,12.7,7.4,73.1,89.1],[33.5,12.7,7.4,73.1,89.1],[33.5,12.7,7.4,73.1,89.1],[33.5,12.7,7.4,73.1,89.1],[33.5,12.7,7.4,73.1,89.1]]


labels=[1,2,3,4,5,6]
#2x5

batch_size=2

input_data_batch=[[11.1,21.5,43.6,22.1,11.44],[33.5,12.7,7.4,73.1,89.1]]


 #now should I reshape my data to make it rank 3 like this 


reshape_one=np.reshape(input_data_batch,[-1,batch_size,5])
print(reshape_one)


# or like this ?



reshape_two=np.reshape(input_data_batch,[batch_size,-1,5])

print(reshape_two)

輸出:

first one

[[[11.1  21.5  43.6  22.1  11.44]
  [33.5  12.7   7.4  73.1  89.1 ]]]

second one


[[[11.1  21.5  43.6  22.1  11.44]]

 [[33.5  12.7   7.4  73.1  89.1 ]]]

LSTM和其他序列模型可以接受時間較長的輸入(即維是時間,批次,通道)或批次較大的輸入(維是批次,時間,通道)。 我不知道您向tf的哪個實現傳遞了哪些標志,因此我無法從您提供的代碼中確定您是需要批處理主要輸入還是時間要求主要輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM