你如何使用转换Tensorflow图float32来float16?目前,存在用于量化和转换为八位整数的图优化.
尝试将float32权重加载到float16图表中会失败:
DataLossError (see above for traceback): Invalid size in bundle entry: key model/conv5_1/biases; stored size 1536; expected size 768
[[Node: save/RestoreV2_16 = RestoreV2[dtypes=[DT_HALF], _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/RestoreV2_16/tensor_names, save/RestoreV2_16/shape_and_slices)]]
[[Node: save/RestoreV2_3/_39 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_107_save/RestoreV2_3", tensor_type=DT_HALF, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Run Code Online (Sandbox Code Playgroud)
我认为我的解决方案绝对不是最好的,而不是最直接的解决方案,但没有其他人发布任何内容:
我所做的是以完全精确的方式训练网络,并将它们保存在检查点中.然后我构建了一个网络副本,将所有需要的变量设置为tf.float16的dtype并删除所有训练节点.最后,我按以下方式加载和转换变量:
previous_variables = [
var_name for var_name, _
in tf.contrib.framework.list_variables('path-to-checkpoint-file')]
#print(previous_variables)
sess.run(tf.global_variables_initializer())
restore_map = {}
for variable in tf.global_variables():
if variable.op.name in previous_variables:
var = tf.contrib.framework.load_variable(
'path-to-checkpoint-file', variable.op.name)
if(var.dtype == np.float32):
tf.add_to_collection('assignOps', variable.assign(
tf.cast(var, tf.float16)))
else:
tf.add_to_collection('assignOps', variable.assign(var))
sess.run(tf.get_collection('assignOps'))
Run Code Online (Sandbox Code Playgroud)
如果你有不想转换的float32的张量,这显然有问题,我很幸运没有,因为我想将我的所有节点转换为float16精度.如果你有那些你可以进一步过滤其他if语句.我希望这回答了你的问题.
| 归档时间: |
|
| 查看次数: |
3712 次 |
| 最近记录: |