私はテンソルフローのウェーブセットの独自の実装に取り組んでいます。私は生成時にオーディオがゼロになるこの問題を抱えています。私のクラスが正しく重み付けされていると助けになると思います。これを行うために、私はコスト関数をその価値が出現する頻度で除算することに決めました。今私は、私が訓練するときに、周波数の連続的な合計を維持しています。それらを計算するために、私は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)
一つのアイデア: 'for'ループの代わりに' tf.unsorted_segment_sum(tf.ones([...])、Y_) ':あなたはこのような何かを書くことができます。 –