ubuntu18.04LTS服务器用vituralenv安装和配置pytorch和tensorflow

============tensorflow=================

$ python3 -m venv tf14
====输入例子====
# $ vim ~/.bashrc #(添加如下行,可以设置bash默认启动)
$ source ~/tf14/bin/activate
$ pip3 install tensorflow-gpu==1.14
$ pip3 install ipython
$ ipython

====输入例子====

import tensorflow as tf

with tf.device('/cpu:0'):
  a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
  b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')

with tf.device('/gpu:1'):
  c = a+b

print(c)
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))

quit()
$ deactivate

============pytorch=================

$ python3 -m venv pytorch13
$ source ~/pytorch13/bin/activate
$ pip3 install torch===1.3.0 torchvision===0.4.1 -f https://download.pytorch.org/whl/torch_stable.html
$ pip3 install ipython
$ ipython
====输入例子====
import torch as t
x = t.rand(5,3)
y = t.rand(5,3)
if t.cuda.is_available():
    x = x.cuda()
    y = y.cuda()
    print(x+y)
=====有结果代表成果====
tensor([[1.4504, 1.8244, 1.7167],
[0.6931, 1.4888, 0.5346],
[0.9387, 0.4503, 0.8335],
[1.3832, 0.9331, 1.4760],
[1.6562, 0.7950, 0.2785]], device='cuda:0')
quit()

$ deactivate

原文地址:https://www.cnblogs.com/jeshy/p/11743778.html