tensorflow与pytorch张量互转
Posted AI 菌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow与pytorch张量互转相关的知识,希望对你有一定的参考价值。
1、tensorflow张量转pytorch张量
- tensorflow(Tensor)–>numpy.ndarray–>pytorch(Tensor)
import torch
import tensorflow as tf
tf_tensor = tf.constant([1,2,3])
with tf.compat.v1.Session().as_default():
np_array = tf_tensor.eval()
torch_tensor = torch.from_numpy(np_array )
2、pytorch张量转tensorflow张量
- pytorch(Tensor)–>numpy.ndarray–>tensorflow(Tensor)
import torch
import tensorflow as tf
torch_tensor = torch.ones(100)
np_tensor = torch_tensor.numpy()
tf_tensor = tf.convert_to_tensor(np_tensor)
以上是关于tensorflow与pytorch张量互转的主要内容,如果未能解决你的问题,请参考以下文章