2016-10-22 49 views
0

我正在自己的tensorflow中實現wavenet。生成時音頻變爲零時,我一直有這個問題。我認爲如果我的課程權重正確,這可能會有所幫助。爲此,我決定將我的成本函數除以其出現的頻率。現在我正在訓練中保持頻率總數。要計算它們,我展開128個不同的不同值的列表,並計算每個值的計數。我覺得應該有辦法做到這一點與矢量操作,但我不確定如何。你們中的任何一個人都知道我能如何消除for循環?計數類頻率以平衡張量流中的類

with tf.variable_scope('training'): 
    self.global_step = tf.get_variable('global_step', [], tf.int64, initializer = tf.constant_initializer(), trainable = False) 
    class_count = tf.get_variable('class_count', (quantization_channels,), tf.int64, initializer = tf.constant_initializer(), trainable = False) 
    total_count = tf.get_variable('total_count', [], tf.int64, initializer = tf.constant_initializer(), trainable = False) 

    y_ = tf.reshape(y_, (-1,)) 
    y = tf.reshape(y, (-1, quantization_channels)) 

    counts = [0] * quantization_channels 
    for i in range(quantization_channels): 
     counts[i] = tf.reduce_sum(tf.cast(tf.equal(y_, i), tf.int64)) 

    counts = class_count + tf.pack(counts) 
    total = total_count + tf.reduce_prod(tf.shape(y_, out_type = tf.int64)) 
    with tf.control_dependencies([tf.assign(class_count, counts), tf.assign(total_count, total)]): 
     class_freq = tf.cast(counts, tf.float32)/tf.cast(total, tf.float32) 

    weights = tf.gather(class_freq, y_) 
    self.cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_)/(quantization_channels * weights + 1e-2)) 
    self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, 1), y_), tf.float32)) 

    opt = tf.train.AdamOptimizer(self.learning_rate) 
    grads = opt.compute_gradients(self.cost) 
    grads = [(tf.clip_by_value(g, -1.0, 1.0), v) for g, v in grads] 
    self.train_step = opt.apply_gradients(grads, global_step = self.global_step) 
+0

一個想法:你可以寫這樣的:'tf.unsorted_segment_sum(tf.ones([...]),Y_)'代替'for'循環。 –

回答