2

我試圖在tensorflow中設計一個簡單的lstm。我想數據的順序從1到10在張量流中使用LSTM RNN進行分類,ValueError:Shape(1,10,5)必須具有rank 2

我有10時間戳和數據X.我只考慮一個序列現在分爲類,所以我的批量大小= 1 在每一個時代,生成一個新的序列。例如X是像這 -

X [[ 2.52413028 2.49449348 2.46520466 2.43625973 2.40765466 2.37938545 
    2.35144815 2.32383888 2.29655379 2.26958905]] 

一個numpy的陣列要使其適合於LSTM輸入,我首先被轉換到一張量,然後再成形它(的batch_size,sequence_lenght,輸入尺寸) -

X= np.array([amplitude * np.exp(-t/tau)]) 
print 'X', X 

#Sorting out the input 
train_input = X 
train_input = tf.convert_to_tensor(train_input) 
train_input = tf.reshape(train_input,[1,10,1]) 
print 'ti', train_input 

對於輸出我生成的1類範圍內的一個熱的編碼標籤至10

#------------sorting out the output 
train_output= [int(math.ceil(tau/resolution))] 
train_output= one_hot(train_output, num_labels=10) 
print 'label', train_output 

train_output = tf.convert_to_tensor(train_output) 

>>label [[ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]] 

然後創建用於tensorflow圖表佔位符,取得LSTM細胞並給權重和b ias-

data = tf.placeholder(tf.float32, shape= [batch_size,len(t),1]) 
target = tf.placeholder(tf.float32, shape = [batch_size, num_classes]) 

cell = tf.nn.rnn_cell.LSTMCell(num_hidden) 
output, state = rnn.dynamic_rnn(cell, data, dtype=tf.float32) 

weight = tf.Variable(tf.random_normal([batch_size, num_classes, 1])), 
bias = tf.Variable(tf.random_normal([num_classes])) 

#training 
prediction = tf.nn.softmax(tf.matmul(output,weight) + bias) 
cross_entropy = -tf.reduce_sum(target * tf.log(prediction)) 
optimizer = tf.train.AdamOptimizer() 
minimize = optimizer.minimize(cross_entropy) 

我已經寫了代碼到目前爲止,並在訓練步驟出現錯誤。它與輸入形狀有關嗎?這裏是回溯---

回溯(最近通話最後一個):

File "/home/raisa/PycharmProjects/RNN_test1/test3.py", line 66, in <module> 
prediction = tf.nn.softmax(tf.matmul(output,weight) + bias) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1036, in matmul 
name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 911, in _mat_mul 
transpose_b=transpose_b, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op 
op_def=op_def) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2156, in create_op 
set_shapes_for_outputs(ret) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs 
shapes = shape_func(op) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/common_shapes.py", line 81, in matmul_shape 
a_shape = op.inputs[0].get_shape().with_rank(2) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 625, in with_rank 
raise ValueError("Shape %s must have rank %d" % (self, rank)) 
ValueError: Shape (1, 10, 5) must have rank 2 

回答

2

看你的代碼,你RNN輸出應該有batch_size x 1 x num_hidden一個維度,而你的W具有尺寸batch_size x num_classes x 1不過你想要的乘法這兩個是batcH_size x num_classes

你可以試試output = tf.reshape(output, [batch_size, num_hidden])weight = tf.Variable(tf.random_normal([num_hidden, num_classes])),讓我知道那是怎麼回事?

+0

感謝您的回覆。我仍然不確定RNN輸出的形狀應該是什麼。我嘗試將其重新設置爲[batch_size,num_hidden],並將權重設置爲[num_hidden,num_classes,正如您所建議的,但我收到錯誤 - – zerogravty

+0

ValueError:尺寸10和5不兼容 – zerogravty

+0

現在,RNN輸出具有形狀的 '張量( 「Reshape_1:0」,形狀=(5,10),D型細胞= FLOAT32)' 但是權重矩陣具有 '張量( 「形狀:0」 的形狀,形狀=(2,) ,dtype = int32)' – zerogravty

1

如果您使用TF> = 1.0,則可以利用tf.contrib.rnn庫和OutputProjectionWrapper將完全連接的層添加到RNN的輸出。是這樣的:我使用softmax_cross_entropy_with_logits而不是用prediction OP和手動計算交叉熵的

# Network definition. 
cell = tf.contrib.rnn.LSTMCell(num_hidden) 
cell = tf.contrib.rnn.OutputProjectionWrapper(cell, num_classes) # adds an output FC layer for you 
output, state = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32) 

# Training. 
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=targets) 
cross_entropy = tf.reduce_sum(cross_entropy) 
optimizer = tf.train.AdamOptimizer() 
minimize = optimizer.minimize(cross_entropy) 

注意。它應該是更有效和強大的。

OutputProjectionWrapper基本上做同樣的事情,但它可能有助於緩解一些頭痛。