2017-02-07 9 views
1

Eigentlich kann ich diese Frage nicht beschreiben. Es ist so komisch.tf.reshape funktioniert nicht wie erwartet

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}) 

Das sind meine ganzen Code, ich kann kein Problem darin finden.

Aber eine ValueError: Cannot feed value of shape (1077, 27, 50, 50) for Tensor 'source_flat_reshape:0', which has shape '(?, ?, 800)' wurde ausgelöst.

Die Fehlermeldung ist seltsam, weil es bei source = tf.reshape(source_flat, [-1, source_max_step, 5*5*32], "source_flat_reshape") geschehen scheint, aber wie könnte source_flat eine Form von (1077, 27, 50, 50) haben? Es sollte (1077*77, 800)

sein Und manchmal wurde eine andere ValueError: Cannot feed value of shape (1077, 27, 50, 50) for Tensor 'Reshape:0', which has shape '(?, 50, 50)' ausgelöst.

Es ist auch schwer zu verstehen, warum es passiert ist?

Hoffe, dass jemand mir eine Hand geben könnte.

Antwort

1

Sehen Sie, was passiert, wenn Sie feed_dict verwenden - verweisen Sie auf die Variablen source und target. Die Python-Variable bezieht sich jedoch nicht mehr auf die Platzhalter, sondern auf die Umformungsoperatoren - daher wird die Operation 'übersprungen'.

Die einfachste Lösung ist die Umbenennung der Platzhalter in etwas Einzigartiges. Weiter unten im Netzwerk ist es OK, denselben Namen wiederzuverwenden (Sie könnten einfach jeden Layer aufrufen net), es spielt keine Rolle, solange Sie nicht mehr auf sie verweisen müssen.

Probieren Sie es aus?

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

vielen Dank. Gestern habe ich endlich das Problem gefunden. Der Platz ist genau dort, wo das Problem liegt. – Sraw

Verwandte Themen