【发布时间】:2020-08-10 20:02:33
【问题描述】:
我正在尝试将代码从 tf 转换为 pytorch。 我被卡住的代码部分是这个 sess.run。据我所知,pytorch 不需要它,但我找不到复制它的方法。我附上代码。
TF:
ebnos_db = np.linspace(1,6, 6)
bers_no_training = np.zeros(shape=[ebnos_db.shape[0]])
for j in range(epochs):
for i in range(ebnos_db.shape[0]):
ebno_db = ebnos_db[i]
bers_no_training[i] += sess.run(ber, feed_dict={
batch_size: samples,
noise_var: ebnodb2noisevar(ebno_db, coderate)
})
bers_no_training /= epochs
samples 是一个 int32,而 ebnodb2noisevar() 返回一个 float32。
TF 中的 BER 计算如下:
ber = tf.reduce_mean(tf.cast(tf.not_equal(x, x_hat), dtype=tf.float32))
在 PT 中:
wrong_bits = ( torch.eq(x, x_hat).type(torch.float32) * -1 ) + 1
ber = torch.mean(wrong_bits)
我认为BER计算得很好,但主要问题是我不知道如何将sess.run转换为PyTorch,也不完全了解它的功能。
谁能帮帮我?
谢谢
【问题讨论】:
标签: python numpy tensorflow pytorch