TensorFlow TensorProto 创建方法


发布日期 : 2021-08-06 05:09:28 UTC

访问量: 10 次浏览

TensorFlow – 如何创建一个TensorProto

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

TensorProto主要用于生成numpy数组。

使用的方法

  • make\_tensor\_proto :这个函数接受需要放入TensorProto的值和其他可选参数。

示例

示例 1:

# importing the library
import tensorflow as tf

# Initializing Input
value = tf.constant([1, 15], dtype = tf.float64)

# Printing the Input
print("Value: ", value)

# Getting TensorProto
res = tf.make_tensor_proto(value)

# Printing the resulting tensor
print("Result: ", res)

输出:

Value: tf.Tensor([ 1. 15.], shape=(2, ), dtype=float64)
Result: dtype: DT_DOUBLE
tensor_shape {
dim {
size: 2
}
}
tensor_content: "\000\000\000\000\000\000\360?\000\000\000\000\000\000.@"

实例2:
本实例使用Python数组来生成TensorProto。

# importing the library
import tensorflow as tf

# Initializing Input
value = [1, 2, 3, 4]

# Printing the Input
print("Value: ", value)

# Getting TensorProto
res = tf.make_tensor_proto(value)

# Printing the resulting tensor
print("Result: ", res)

输出:

Value: [1, 2, 3, 4]
Result: dtype: DT_INT32
tensor_shape {
dim {
size: 4
}
}
tensor_content: "\001\000\000\000\002\000\000\000\003\000\000\000\004\000\000\000"