2017-04-11 211 views
3

我對TF相當陌生,並開始使用TF教程學習它。 我剛纔簡單複製從TF網站上的旋轉模型,並嘗試運行它,但 我收到一條錯誤消息:
回溯(最近通話最後一個):ValueError:將參數'值'列爲'ConcatV2'Op長度小於最小長度2

文件「C:\用戶\ jan \ Desktop \ tensorflow prac \ swivel \ swivel.py「,第362行,在 tf.app.run()

文件」C:\ Users \ jhan \ AppData \ Local \ Programs \ Python \ Python35 \ lib (main(_sys.argv [:1] + flags_passthrough))

(第44行,在運行 _sys.exit(main(_sys.argv [:1] + flags_passthrough)文件「C:\ Users \ jhan \ Desktop \ tensorflow prac \ swivel \ swivel.py」,行289,在主 model = SwivelModel(FLAGS)

文件「C:\ Users \ jhan \ Desktop \ tensorflow prac \旋轉\ swivel.py 「線路257,在初始化 l2_loss = tf.reduce_mean(tf.concat(軸= 0,值= l2_losses),0,

文件」 C:\用戶\ jhan \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ tensorflow \ python \ ops \ array_ops.py「,行1034,concat name = name)

文件」C:\ Users \ jhan \應用程序數據\本地\程序\ Python的\ Python35 \ LIB \站點包s \ tensorflow \ python \ ops \ gen_array_ops.py「,第519行,in _concat_v2 name = name)

文件」C:\ Users \ jan \ AppData \ Local \ Programs \ Python \ Python35 \ lib \包\ tensorflow \ python的\框架\ op_def_library.py」,線路546,在apply_op (input_name,op_type_name,LEN(值),num_attr.minimum))

ValueError異常:list參數 '值' 到 'ConcatV2' 歐普長度爲0比最小長度短2 ..

我的代碼是:

class SwivelModel(object): 

def __init__(self, config): 
    """Construct graph for dmc.""" 
    self._config = config 
# Create paths to input data files 
log('Reading model from: %s', config.input_base_path) 
count_matrix_files = glob.glob(config.input_base_path + '/shard-*.pb') 
row_sums_path = config.input_base_path + '/row_sums.txt' 
col_sums_path = config.input_base_path + '/col_sums.txt' 

# Read marginals 
row_sums = read_marginals_file(row_sums_path) 
col_sums = read_marginals_file(col_sums_path) 

self.n_rows = len(row_sums) 
self.n_cols = len(col_sums) 
log('Matrix dim: (%d,%d) SubMatrix dim: (%d,%d)', 
    self.n_rows, self.n_cols, config.submatrix_rows, config.submatrix_cols) 
self.n_submatrices = (self.n_rows * self.n_cols/
         (config.submatrix_rows * config.submatrix_cols)) 
log('n_submatrices: %d', self.n_submatrices) 

with tf.device('/cpu:0'): 
    # ===== CREATE VARIABLES ====== 
    # Get input 
    global_row, global_col, count = count_matrix_input(
    count_matrix_files, config.submatrix_rows, config.submatrix_cols) 

    # Embeddings 
    self.row_embedding = embeddings_with_init(
    embedding_dim=config.embedding_size, 
    vocab_size=self.n_rows, 
    name='row_embedding') 
    self.col_embedding = embeddings_with_init(
    embedding_dim=config.embedding_size, 
    vocab_size=self.n_cols, 
    name='col_embedding') 
    tf.summary.histogram('row_emb', self.row_embedding) 
    tf.summary.histogram('col_emb', self.col_embedding) 

    matrix_log_sum = math.log(np.sum(row_sums) + 1) 
    row_bias_init = [math.log(x + 1) for x in row_sums] 
    col_bias_init = [math.log(x + 1) for x in col_sums] 
    self.row_bias = tf.Variable(
     row_bias_init, trainable=config.trainable_bias) 
    self.col_bias = tf.Variable(
     col_bias_init, trainable=config.trainable_bias) 
    tf.summary.histogram('row_bias', self.row_bias) 
    tf.summary.histogram('col_bias', self.col_bias) 

    # Add optimizer 
    l2_losses = [] 
    sigmoid_losses = [] 
    self.global_step = tf.Variable(0, name='global_step') 
    opt = tf.train.AdagradOptimizer(config.learning_rate) 

    all_grads = [] 

devices = ['/gpu:%d' % i for i in range(FLAGS.num_gpus)] \ 
    if FLAGS.num_gpus > 0 else get_available_gpus() 
self.devices_number = len(devices) 
with tf.variable_scope(tf.get_variable_scope()): 
    for dev in devices: 
    with tf.device(dev): 
     with tf.name_scope(dev[1:].replace(':', '_')): 
     # ===== CREATE GRAPH ===== 
     # Fetch embeddings. 
     selected_row_embedding = tf.nn.embedding_lookup(
      self.row_embedding, global_row) 
     selected_col_embedding = tf.nn.embedding_lookup(
      self.col_embedding, global_col) 

     # Fetch biases. 
     selected_row_bias = tf.nn.embedding_lookup(
      [self.row_bias], global_row) 
     selected_col_bias = tf.nn.embedding_lookup(
      [self.col_bias], global_col) 

     # Multiply the row and column embeddings to generate predictions. 
     predictions = tf.matmul(
      selected_row_embedding, selected_col_embedding, 
      transpose_b=True) 

     # These binary masks separate zero from non-zero values. 
     count_is_nonzero = tf.to_float(tf.cast(count, tf.bool)) 
     count_is_zero = 1 - count_is_nonzero 

     objectives = count_is_nonzero * tf.log(count + 1e-30) 
     objectives -= tf.reshape(
      selected_row_bias, [config.submatrix_rows, 1]) 
     objectives -= selected_col_bias 
     objectives += matrix_log_sum 

     err = predictions - objectives 

     # The confidence function scales the L2 loss based on the raw 
     # co-occurrence count. 
     l2_confidence = (config.confidence_base + 
         config.confidence_scale * tf.pow(
          count, config.confidence_exponent)) 

     l2_loss = config.loss_multiplier * tf.reduce_sum(
      0.5 * l2_confidence * err * err * count_is_nonzero) 
     l2_losses.append(tf.expand_dims(l2_loss, 0)) 

     sigmoid_loss = config.loss_multiplier * tf.reduce_sum(
      tf.nn.softplus(err) * count_is_zero) 
     sigmoid_losses.append(tf.expand_dims(sigmoid_loss, 0)) 

     loss = l2_loss + sigmoid_loss 
     grads = opt.compute_gradients(loss) 
     all_grads.append(grads) 

with tf.device('/cpu:0'): 
    # ===== MERGE LOSSES ===== 
    l2_loss = tf.reduce_mean(tf.concat(axis=0, values=l2_losses), 0, 
          name="l2_loss") 
    sigmoid_loss = tf.reduce_mean(tf.concat(axis=0, values=sigmoid_losses), 0, 
           name="sigmoid_loss") 
    self.loss = l2_loss + sigmoid_loss 
    average = tf.train.ExponentialMovingAverage(0.8, self.global_step) 
    loss_average_op = average.apply((self.loss,)) 
    tf.summary.scalar("l2_loss", l2_loss) 
    tf.summary.scalar("sigmoid_loss", sigmoid_loss) 
    tf.summary.scalar("loss", self.loss) 

    # Apply the gradients to adjust the shared variables. 
    apply_gradient_ops = [] 
    for grads in all_grads: 
    apply_gradient_ops.append(opt.apply_gradients(
     grads, global_step=self.global_step)) 

    self.train_op = tf.group(loss_average_op, *apply_gradient_ops) 
    self.saver = tf.train.Saver(sharded=True) 

回答

1

我不能完全知道錯誤在哪裏,但:

查看代碼錯誤是說l2_losses是空的。只是這條線之前掉落print聲明,檢查l2_losses的價值:

print(l2_losses) # new print statement 
l2_loss = tf.reduce_mean(tf.concat(axis=0, values=l2_losses), 0, 
          name="l2_loss") 

該代碼應運行開箱即用,所以你在做什麼碼?

+0

#putonspectacles 你是對的關於l2_losses是空的!我沒有改變任何東西,只是輸出的目錄。我想我必須弄清楚爲什麼這個列表是空的,嗯謝謝,但! –

+0

@JasonHan問題? – putonspectacles

+0

您是否嘗試過Swivel模型?只是想知道是否有其他人有類似的錯誤 –