【发布时间】:2019-05-09 16:18:11
【问题描述】:
在调试 tf 感到沮丧后,我最近才切换到 PyTorch,并了解它几乎完全等同于在 numpy 中编码。我的问题是我们可以在 PyTorch 模型中使用哪些允许的 python 方面(完全放在 GPU 上),例如。 if-else 必须在tensorflow中实现如下
a = tf.Variable([1,2,3,4,5], dtype=tf.float32)
b = tf.Variable([6,7,8,9,10], dtype=tf.float32)
p = tf.placeholder(dtype=tf.float32)
ps = tf.placeholder(dtype=tf.bool)
li = [None]*5
li_switch = [True, False, False, True, True]
for i in range(5):
li[i] = tf.Variable(tf.random.normal([5]))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
def func_0():
return tf.add(a, p)
def func_1():
return tf.subtract(b, p)
with tf.device('GPU:0'):
my_op = tf.cond(ps, func_1, func_0)
for i in range(5):
print(sess.run(my_op, feed_dict={p:li[i], ps:li_switch[i]}))
上述代码在 pytorch 中的结构会发生怎样的变化?如何将上面的变量和操作放在 GPU 上,并在 pytorch 中将列表输入并行化到我们的图形中?
【问题讨论】:
-
我不认为每个问题都会像堆栈要求我们做的那样遵循模板。问题很简单,在要放在 GPU 上的 pytorch“模型”中,python 的代码(如果有的话)是什么,torch 的代码是什么?上面的代码几乎是我想要的。上面有一些解释的余地,任何内容都会帮助我多说几句。
-
你是对的。但是,我们向您保证准确的重构结果。我们需要看一个输入和输出的例子,我认为这并不多。
-
好的,我一会儿再编辑。
-
@AndrewNaguib 告诉我
标签: python tensorflow pytorch