tensorflow_gpu相关接口及多gpu训练及相关注意

多gpu训练使用keras

multi_gpu_model(model, gpus, cpu_merge=True, cpu_relocation=False)

这个函数有个极其重要的参数,cpu_merge和cpu_relocation,这个参数会影响到你训练成功或失败,因为比如你用了否定的,然后你训练却用了cpu存模型,或者相反,则会导致你更新参数失败。

正确的案例应该模仿官方,我的方式是

with tf.device('/cpu:0'):
    predict_model = keras.models.Model(inputs=input_images, outputs=cls_pred)
muti_gpu_model = keras.utils.multi_gpu_model(model=predict_model, gpus=2)

freeze测试:

现在有个问题就是我要冻结参数时,我是冻结多gpu模型还是冻结原来的模型还是都冻结?

 

 

查看设备名字:
 

tf.test.gpu_device_name()

Returns the name of a GPU device if available or the empty string.

 

tf.contrib.eager.list_devices()

Names of the available devices, as a list.

 

使用具体设备的函数

# Place the operations on device "GPU:0" in the "ps" job.
device_spec = DeviceSpec(job="ps", device_type="GPU", device_index=0)
with tf.device(device_spec):
  # Both my_var and squared_var will be placed on /job:ps/device:GPU:0.
  my_var = tf.Variable(..., name="my_variable")
  squared_var = tf.square(my_var)

If a DeviceSpec is partially specified, it will be merged with other DeviceSpecs according to the scope in which it is defined. DeviceSpec components defined in inner scopes take precedence over those defined in outer scopes.

with tf.device(DeviceSpec(job="train", )):
  with tf.device(DeviceSpec(job="ps", device_type="GPU", device_index=0):
    # Nodes created here will be assigned to /job:ps/device:GPU:0.
  with tf.device(DeviceSpec(device_type="GPU", device_index=1):
    # Nodes created here will be assigned to /job:train/device:GPU:1.

DeviceSpec consists of 5 components -- each of which is optionally specified:

  • Job: The job name.
  • Replica: The replica index.
  • Task: The task index.
  • Device type: The device type string (e.g. "CPU" or "GPU").
  • Device index: The device index.

__init__

__init__(
    job=None,
    replica=None,
    task=None,
    device_type=None,
    device_index=None
)

Create a new DeviceSpec object.

Args:

  • job: string. Optional job name.
  • replica: int. Optional replica index.
  • task: int. Optional task index.
  • device_type: Optional device type string (e.g. "CPU" or "GPU")
  • device_index: int. Optional device index. If left unspecified, device represents 'any' device_index.

 

封装好的一个分支

tf.distribute

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值