3
我有一個預測張量(實際網絡)Tensorflow重塑張
(Pdb) pred
<tf.Tensor 'transpose_1:0' shape=(?, 200, 200) dtype=float32>
和y張
y = tf.placeholder("float", [None, n_steps, n_classes])
(Pdb) y
<tf.Tensor 'Placeholder_1:0' shape=(?, 200, 200) dtype=float32>
我想將它送入
f.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
然而,它要求尺寸爲[batch_size, num_classes]
所以我想重塑既pred
和y
,使他們看起來像這樣
<tf.Tensor 'transpose_1:0' shape=(?, 40000) dtype=float32>
但是當我運行reshape
我得到
(Pdb) tf.reshape(pred, [40000])
<tf.Tensor 'Reshape_1:0' shape=(40000,) dtype=float32>
代替(?,40000)
我怎麼能維持None
尺寸? (批量大小尺寸)
我還貼出所有相關的代碼...
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_steps, n_classes])
# Define weights
weights = {
'hidden': tf.Variable(tf.random_normal([n_hidden, n_classes]), dtype="float32"),
'out': tf.Variable(tf.random_normal([n_hidden, n_classes]), dtype="float32")
}
biases = {
'hidden': tf.Variable(tf.random_normal([n_hidden]), dtype="float32"),
'out': tf.Variable(tf.random_normal([n_classes]), dtype="float32")
}
def RNN(x, weights, biases):
# Prepare data shape to match `rnn` function requirements
# Current data input shape: (batch_size, n_steps, n_input)
# Permuting batch_size and n_steps
x = tf.transpose(x, [1, 0, 2])
# Reshaping to (n_steps*batch_size, n_input)
x = tf.reshape(x, [-1, n_input])
# Split to get a list of 'n_steps' tensors of shape (batch_size, n_hidden)
# This input shape is required by `rnn` function
x = tf.split(0, n_steps, x)
# Define a lstm cell with tensorflow
lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
output_matrix = []
for i in xrange(n_steps):
temp = tf.matmul(outputs[i], weights['out']) + biases['out']
# temp = tf.matmul(weights['hidden'], outputs[i]) + biases['hidden']
output_matrix.append(temp)
pdb.set_trace()
return output_matrix
pred = RNN(x, weights, biases)
# temp = RNN(x)
# pdb.set_trace()
# pred = tf.shape(temp)
pred = tf.pack(tf.transpose(pred, [1,0,2]))
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
http://stackoverflow.com/questions/36668542/flatten-batch-in-tensorflow –