2017-02-07 44 views
1

其實,我不知道如何描述這個問題。這很奇怪。tf.reshape不能正常工作

import tensorflow as tf 
import numpy as np 
import pickle 

def weight_and_bias(name ,shape): 
    weight = tf.get_variable("W" + name, shape=shape, initializer=tf.contrib.layers.xavier_initializer()) 
    bias = tf.get_variable("B" + name, shape=shape[-1], initializer=tf.contrib.layers.xavier_initializer()) 
    return weight, bias 

def conv2d_2x2(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 5, 5, 1], padding='SAME') 

def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 

sess = tf.InteractiveSession() 

source = tf.placeholder(tf.float32, [None, None, 50, 50]) 
source_len = tf.placeholder(tf.int32, [None]) 
source_max_step = tf.shape(source)[1] 

target = tf.placeholder(tf.float32, [None, None, 50, 50]) 
target_len = tf.placeholder(tf.int32, [None]) 
target_max_step = tf.shape(target)[1] 

W_conv, B_conv = weight_and_bias('conv1', [5, 5, 1, 32]) 

source = tf.reshape(source, [-1, 50, 50], "source_reshape") 
source_tmp = tf.reshape(source, [-1, 50, 50 ,1]) 
source_conv = tf.nn.relu(conv2d_2x2(source_tmp, W_conv) + B_conv) 
source_pool = max_pool_2x2(source_conv) 
source_flat = tf.reshape(source_pool, [-1, 5 * 5 * 32], "source_pool_reshape") 
source = tf.reshape(source_flat, [-1, source_max_step, 5*5*32], "source_flat_reshape") 

W_conv, B_conv = weight_and_bias('conv2', [5, 5, 1, 32]) 

target = tf.reshape(target, [-1, 50, 50], "target_reshape") 
target_tmp = tf.reshape(target, [-1, 50, 50 ,1]) 
target_conv = tf.nn.relu(conv2d_2x2(target_tmp, W_conv) + B_conv) 
target_pool = max_pool_2x2(target_conv) 
target_flat = tf.reshape(target_pool, [-1, 5 * 5 * 32], "target_pool_reshape") 
target = tf.reshape(target_flat, [-1, target_max_step, 5*5*32], "target_flat_reshape") 

source_cell = tf.nn.rnn_cell.LSTMCell(500, initializer=tf.contrib.layers.xavier_initializer()) 
target_cell = tf.nn.rnn_cell.LSTMCell(500, initializer=tf.contrib.layers.xavier_initializer()) 
source_rnn_output, _ = tf.nn.dynamic_rnn(source_cell, source, source_len, dtype=tf.float32, scope = "source") 
target_rnn_output, _ = tf.nn.dynamic_rnn(target_cell, target, target_len, dtype=tf.float32, scope = "target") 

source_output = tf.transpose(source_rnn_output, [1, 0, 2]) 
target_output = tf.transpose(target_rnn_output, [1, 0, 2]) 

source_final_output = tf.gather(source_output, -1) 
target_final_output = tf.gather(target_output, -1) 

output = tf.concat(1, [source_final_output, target_final_output]) 

W_sf, B_sf = weight_and_bias('sf', [1000, 2]) 
predict = tf.nn.softmax(tf.matmul(output, W_sf) + B_sf) 
y = tf.placeholder(tf.float32, [None, 2]) 

cross_entropy = -tf.reduce_sum(y * tf.log(predict)) 
train_step = tf.train.RMSPropOptimizer(1e-4).minimize(cross_entropy) 

correct_prediction = tf.equal(tf.arg_max(predict, 1), tf.arg_max(y, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

with open('set', 'rb') as f: 
    _set = pickle.load(f) 
    training_set = _set[0] 
    training_len = _set[1] 
    training_label = _set[2] 

sess.run(tf.global_variables_initializer()) 

for i in range(20000): 

    if i % 100 == 0: 
     train_accuacy = accuracy.eval(feed_dict = {source: training_set[0], target: training_set[1], source_len: training_len[0], target_len: training_len[1], y: training_label}) 
     print("step %d, training accuracy %g"%(i, train_accuacy)) 
    train_step.run(feed_dict = {source: training_set[0], target: training_set[1], source_len: training_len[0], target_len: training_len[1], y: training_label}) 

這些是我的整個代碼,我找不到任何問題。

但是提出了一個ValueError: Cannot feed value of shape (1077, 27, 50, 50) for Tensor 'source_flat_reshape:0', which has shape '(?, ?, 800)'

錯誤信息很奇怪,因爲它似乎發生在source = tf.reshape(source_flat, [-1, source_max_step, 5*5*32], "source_flat_reshape"),但source_flat怎麼可能形狀爲(1077, 27, 50, 50)?它應該是(1077*77, 800)

並且有時還有另一個ValueError: Cannot feed value of shape (1077, 27, 50, 50) for Tensor 'Reshape:0', which has shape '(?, 50, 50)'被提出。

這也很難理解,爲什麼發生?

希望任何人都可以幫我一把。

回答

1

看看當您使用feed_dict時會發生什麼 - 您參考變量sourcetarget。然而,python變量不再指佔位符,而是指重塑操作 - 因此操作被「跳過」。

最簡單的修復方法是將佔位符重命名爲獨特的東西。進一步在網絡中重新使用相同的名稱是可以的(您可以只調用每個圖層的net),只要您不再需要引用它們就沒有關係。

試試這個去嗎?

import tensorflow as tf 
import numpy as np 
import pickle 

def weight_and_bias(name ,shape): 
    weight = tf.get_variable("W" + name, shape=shape, initializer=tf.contrib.layers.xavier_initializer()) 
    bias = tf.get_variable("B" + name, shape=shape[-1], initializer=tf.contrib.layers.xavier_initializer()) 
    return weight, bias 

def conv2d_2x2(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 5, 5, 1], padding='SAME') 

def max_pool_2x2(x): 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 

sess = tf.InteractiveSession() 

source_placeholder = tf.placeholder(tf.float32, [None, None, 50, 50]) 
source_len = tf.placeholder(tf.int32, [None]) 
source_max_step = tf.shape(source)[1] 

target_placeholder = tf.placeholder(tf.float32, [None, None, 50, 50]) 
target_len = tf.placeholder(tf.int32, [None]) 
target_max_step = tf.shape(target)[1] 

W_conv, B_conv = weight_and_bias('conv1', [5, 5, 1, 32]) 

source = tf.reshape(source_placeholder, [-1, 50, 50], "source_reshape") 
source_tmp = tf.reshape(source, [-1, 50, 50 ,1]) 
source_conv = tf.nn.relu(conv2d_2x2(source_tmp, W_conv) + B_conv) 
source_pool = max_pool_2x2(source_conv) 
source_flat = tf.reshape(source_pool, [-1, 5 * 5 * 32], "source_pool_reshape") 
source = tf.reshape(source_flat, [-1, source_max_step, 5*5*32], "source_flat_reshape") 

W_conv, B_conv = weight_and_bias('conv2', [5, 5, 1, 32]) 

target = tf.reshape(target_placeholder, [-1, 50, 50], "target_reshape") 
target_tmp = tf.reshape(target, [-1, 50, 50 ,1]) 
target_conv = tf.nn.relu(conv2d_2x2(target_tmp, W_conv) + B_conv) 
target_pool = max_pool_2x2(target_conv) 
target_flat = tf.reshape(target_pool, [-1, 5 * 5 * 32], "target_pool_reshape") 
target = tf.reshape(target_flat, [-1, target_max_step, 5*5*32], "target_flat_reshape") 

source_cell = tf.nn.rnn_cell.LSTMCell(500, initializer=tf.contrib.layers.xavier_initializer()) 
target_cell = tf.nn.rnn_cell.LSTMCell(500, initializer=tf.contrib.layers.xavier_initializer()) 
source_rnn_output, _ = tf.nn.dynamic_rnn(source_cell, source, source_len, dtype=tf.float32, scope = "source") 
target_rnn_output, _ = tf.nn.dynamic_rnn(target_cell, target, target_len, dtype=tf.float32, scope = "target") 

source_output = tf.transpose(source_rnn_output, [1, 0, 2]) 
target_output = tf.transpose(target_rnn_output, [1, 0, 2]) 

source_final_output = tf.gather(source_output, -1) 
target_final_output = tf.gather(target_output, -1) 

output = tf.concat(1, [source_final_output, target_final_output]) 

W_sf, B_sf = weight_and_bias('sf', [1000, 2]) 
predict = tf.nn.softmax(tf.matmul(output, W_sf) + B_sf) 
y = tf.placeholder(tf.float32, [None, 2]) 

cross_entropy = -tf.reduce_sum(y * tf.log(predict)) 
train_step = tf.train.RMSPropOptimizer(1e-4).minimize(cross_entropy) 

correct_prediction = tf.equal(tf.arg_max(predict, 1), tf.arg_max(y, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

with open('set', 'rb') as f: 
    _set = pickle.load(f) 
    training_set = _set[0] 
    training_len = _set[1] 
    training_label = _set[2] 

sess.run(tf.global_variables_initializer()) 

for i in range(20000): 

    if i % 100 == 0: 
     train_accuacy = accuracy.eval(feed_dict = {source_placeholder: training_set[0], target_placeholder: training_set[1], source_len: training_len[0], target_len: training_len[1], y: training_label}) 
     print("step %d, training accuracy %g"%(i, train_accuacy)) 
    train_step.run(feed_dict = {source_placeholder: training_set[0], target_placeholder: training_set[1], source_len: training_len[0], target_len: training_len[1], y: training_label}) 
+0

非常感謝。昨天我終於找到了問題。這個地方正是問題所在。 – Sraw