本文整理汇总了Python中tensorflow.int8方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.int8方法的具体用法?Python tensorflow.int8怎么用?Python tensorflow.int8使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.int8方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dequantize_linear
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def test_dequantize_linear(self):
node_def = helper.make_node("DequantizeLinear",
["x", "x_scale", "x_zero_point"], ["y"])
for x, x_zero_point in [[
self._get_rnd_int(-128, 127, [2, 6], np.int8),
self._get_rnd_int(-128, 127, dtype=np.int8)
],
[
self._get_rnd_int(0, 255, [2, 6], np.uint8),
self._get_rnd_int(0, 255, dtype=np.uint8)
],
[self._get_rnd_int(-512, 512, [2, 6]),
np.int32(0)]]:
x_scale = self._get_rnd_float32(-10., 10)
y = np.subtract(np.float32(x), np.float32(x_zero_point))
y = np.multiply(y, x_scale)
output = run_node(node_def, [x, x_scale, x_zero_point])
np.testing.assert_almost_equal(output["y"], y)
示例2: test_max_pool_2d_dilations_ceil_pads_int8
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def test_max_pool_2d_dilations_ceil_pads_int8(self):
if legacy_opset_pre_ver(12):
raise unittest.SkipTest(
"ONNX version {} does not support int8 input type.".format(
defs.onnx_opset_version()))
kernel_shape = [3, 3]
strides = [2, 2]
dilations = [3, 3]
pads = [1, 1, 2, 2]
ceil_mode = 1
input_shape = [10, 3, 23, 23]
self._test_pooling(input_shape=input_shape,
kernel_shape=kernel_shape,
strides=strides,
dilations=dilations,
pads=pads,
ceil_mode=ceil_mode,
input_dtype=np.int8)
示例3: test_quantize_linear
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def test_quantize_linear(self):
node_def = helper.make_node("QuantizeLinear",
["x", "y_scale", "y_zero_point"], ["y"])
for x in [
self._get_rnd_float32(-512., 512., [2, 6]),
self._get_rnd_int(-512, 512, [2, 6])
]:
y_scale = self._get_rnd_float32(-10., 10.)
for y_zero_point in [
self._get_rnd_int(-128, 127, dtype=np.int8),
self._get_rnd_int(0, 255, dtype=np.uint8)
]:
y = np.divide(x, y_scale)
y = np.round(y)
y = np.add(y, y_zero_point)
if y_zero_point.dtype.type is np.int8:
y = np.clip(y, -128, 127).astype(np.int8)
else:
y = np.clip(y, 0, 255).astype(np.uint8)
output = run_node(node_def, [x, y_scale, y_zero_point])
np.testing.assert_almost_equal(output["y"], y)
示例4: testDtype
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def testDtype(self):
with self.test_session():
d = tf.fill([2, 3], 12., name="fill")
self.assertEqual(d.get_shape(), [2, 3])
# Test default type for both constant size and dynamic size
z = tf.zeros([2, 3])
self.assertEqual(z.dtype, tf.float32)
self.assertEqual([2, 3], z.get_shape())
self.assertAllEqual(z.eval(), np.zeros([2, 3]))
z = tf.zeros(tf.shape(d))
self.assertEqual(z.dtype, tf.float32)
self.assertEqual([2, 3], z.get_shape())
self.assertAllEqual(z.eval(), np.zeros([2, 3]))
# Test explicit type control
for dtype in [tf.float32, tf.float64, tf.int32,
tf.uint8, tf.int16, tf.int8,
tf.complex64, tf.complex128, tf.int64, tf.bool]:
z = tf.zeros([2, 3], dtype=dtype)
self.assertEqual(z.dtype, dtype)
self.assertEqual([2, 3], z.get_shape())
self.assertAllEqual(z.eval(), np.zeros([2, 3]))
z = tf.zeros(tf.shape(d), dtype=dtype)
self.assertEqual(z.dtype, dtype)
self.assertEqual([2, 3], z.get_shape())
self.assertAllEqual(z.eval(), np.zeros([2, 3]))
示例5: testOnesLike
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def testOnesLike(self):
for dtype in [tf.float32, tf.float64, tf.int32,
tf.uint8, tf.int16, tf.int8,
tf.complex64, tf.complex128, tf.int64]:
numpy_dtype = dtype.as_numpy_dtype
with self.test_session():
# Creates a tensor of non-zero values with shape 2 x 3.
d = tf.constant(np.ones((2, 3), dtype=numpy_dtype), dtype=dtype)
# Constructs a tensor of zeros of the same dimensions and type as "d".
z_var = tf.ones_like(d)
# Test that the type is correct
self.assertEqual(z_var.dtype, dtype)
z_value = z_var.eval()
# Test that the value is correct
self.assertTrue(np.array_equal(z_value, np.array([[1] * 3] * 2)))
self.assertEqual([2, 3], z_var.get_shape())
示例6: testLargeInt
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def testLargeInt(self):
# Cannot use values outside -128..127 for test, because we're also
# testing int8
s = lambda strs: [x.decode("ascii") for x in strs]
with self.test_session():
input_ = tf.placeholder(tf.int32)
int_inputs_ = [np.iinfo(np.int32).min, np.iinfo(np.int32).max]
output = tf.as_string(input_)
result = output.eval(feed_dict={input_: int_inputs_})
self.assertAllEqual(s(result), ["%d" % x for x in int_inputs_])
input_ = tf.placeholder(tf.int64)
int_inputs_ = [np.iinfo(np.int64).min, np.iinfo(np.int64).max]
output = tf.as_string(input_)
result = output.eval(feed_dict={input_: int_inputs_})
self.assertAllEqual(s(result), ["%d" % x for x in int_inputs_])
示例7: testIntTypes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def testIntTypes(self):
for dtype, nptype in [
(tf.int32, np.int32),
(tf.uint8, np.uint8),
(tf.uint16, np.uint16),
(tf.int16, np.int16),
(tf.int8, np.int8)]:
# Test with array.
t = tensor_util.make_tensor_proto([10, 20, 30], dtype=dtype)
self.assertEquals(dtype, t.dtype)
self.assertProtoEquals("dim { size: 3 }", t.tensor_shape)
a = tensor_util.MakeNdarray(t)
self.assertEquals(nptype, a.dtype)
self.assertAllClose(np.array([10, 20, 30], dtype=nptype), a)
# Test with ndarray.
t = tensor_util.make_tensor_proto(np.array([10, 20, 30], dtype=nptype))
self.assertEquals(dtype, t.dtype)
self.assertProtoEquals("dim { size: 3 }", t.tensor_shape)
a = tensor_util.MakeNdarray(t)
self.assertEquals(nptype, a.dtype)
self.assertAllClose(np.array([10, 20, 30], dtype=nptype), a)
示例8: testNumpyConversion
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def testNumpyConversion(self):
self.assertIs(tf.float32, tf.as_dtype(np.float32))
self.assertIs(tf.float64, tf.as_dtype(np.float64))
self.assertIs(tf.int32, tf.as_dtype(np.int32))
self.assertIs(tf.int64, tf.as_dtype(np.int64))
self.assertIs(tf.uint8, tf.as_dtype(np.uint8))
self.assertIs(tf.uint16, tf.as_dtype(np.uint16))
self.assertIs(tf.int16, tf.as_dtype(np.int16))
self.assertIs(tf.int8, tf.as_dtype(np.int8))
self.assertIs(tf.complex64, tf.as_dtype(np.complex64))
self.assertIs(tf.complex128, tf.as_dtype(np.complex128))
self.assertIs(tf.string, tf.as_dtype(np.object))
self.assertIs(tf.string, tf.as_dtype(np.array(["foo", "bar"]).dtype))
self.assertIs(tf.bool, tf.as_dtype(np.bool))
with self.assertRaises(TypeError):
tf.as_dtype(np.dtype([("f1", np.uint), ("f2", np.int32)]))
示例9: _input
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def _input(self, dtype='float32', shape=None, name=None):
"""Define an input for the recommender.
Parameters
----------
dtype: str
Data type: "float16", "float32", "float64", "int8", "int16", "int32", "int64", "bool", or "string".
shape: list or tuple
Input shape.
name: str
Name of the input.
Returns
-------
Tensorflow placeholder
Defined tensorflow placeholder.
"""
if dtype not in self._str_to_dtype:
raise ValueError
else:
return tf.placeholder(self._str_to_dtype[dtype], shape=shape, name=name)
示例10: test__dtype_to_bytes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def test__dtype_to_bytes():
np_tf_dt = [
(np.uint8, tf.uint8, b"uint8"),
(np.uint16, tf.uint16, b"uint16"),
(np.uint32, tf.uint32, b"uint32"),
(np.uint64, tf.uint64, b"uint64"),
(np.int8, tf.int8, b"int8"),
(np.int16, tf.int16, b"int16"),
(np.int32, tf.int32, b"int32"),
(np.int64, tf.int64, b"int64"),
(np.float16, tf.float16, b"float16"),
(np.float32, tf.float32, b"float32"),
(np.float64, tf.float64, b"float64"),
]
for npd, tfd, dt in np_tf_dt:
npd = np.dtype(npd)
assert tfrecord._dtype_to_bytes(npd) == dt
assert tfrecord._dtype_to_bytes(tfd) == dt
assert tfrecord._dtype_to_bytes("float32") == b"float32"
assert tfrecord._dtype_to_bytes("foobar") == b"foobar"
示例11: _convert_string_dtype
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def _convert_string_dtype(dtype):
if dtype == 'float16':
return tf.float16
if dtype == 'float32':
return tf.float32
elif dtype == 'float64':
return tf.float64
elif dtype == 'int16':
return tf.int16
elif dtype == 'int32':
return tf.int32
elif dtype == 'int64':
return tf.int64
elif dtype == 'uint8':
return tf.int8
elif dtype == 'uint16':
return tf.uint16
else:
raise ValueError('Unsupported dtype:', dtype)
示例12: reduce_mean_support_empty
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def reduce_mean_support_empty(input, keepdims=False):
return tf.cond(tf.size(input) > 0, lambda: tf.reduce_mean(input, keepdims=keepdims), lambda: tf.zeros_like(input))
# def bit_tensor_list(input):
# assert input.dtype in [tf.uint8, tf.uint16, tf.uint32, tf.uint64], 'unsupported data type, must be uint*'
# num_bits = 0
# if input.dtype == tf.int8:
# num_bits = 8
# elif input.dtype == tf.int16:
# num_bits = 16
# elif input.dtype == tf.uint32:
# num_bits = 32
# elif input.dtype == tf.uint64:
# num_bits = 64
# bit_tensors = []
# for i in range(num_bits):
# current_bit = 1 << i
# current_bit_tensor = tf.bitwise.bitwise_and(input, current_bit) == 1
# bit_tensors.append(current_bit_tensor)
# print(bit_tensors)
# return bit_tensors
示例13: parse_a_line_b
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def parse_a_line_b(value, base_num, signal_num):
vec = tf.decode_raw(value, tf.int8)
bases = tf.cast(tf.reshape(tf.strided_slice(vec, [0], [base_num]), [base_num]), dtype=tf.int32)
means = tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num], [base_num + base_num * 4]), [base_num, 4]),
type=tf.float32)
stds = tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num * 5], [base_num * 5 + base_num * 4]), [base_num, 4]),
type=tf.float32)
sanum = tf.cast(tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num * 9], [base_num * 9 + base_num * 2]), [base_num, 2]),
type=tf.int16), dtype=tf.int32)
signals = tf.bitcast(
tf.reshape(tf.strided_slice(vec, [base_num * 11], [base_num * 11 + 4 * signal_num]),
[signal_num, 4]), type=tf.float32)
labels = tf.cast(
tf.reshape(tf.strided_slice(vec, [base_num * 11 + signal_num * 4], [base_num * 11 + signal_num * 4 + 1]),
[1]),
dtype=tf.int32)
return bases, means, stds, sanum, signals, labels
示例14: test_tensor_array_write_read
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def test_tensor_array_write_read():
def run(dtype_str, infer_shape, element_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
np_data = np.array([[1.0, 2.0], [3.0, 4.0]]).astype(dtype_str)
in_data = [np_data, np_data]
t1 = tf.constant(np_data, dtype=dtype)
t2 = tf.constant(np_data, dtype=dtype)
ta1 = tf.TensorArray(dtype=dtype, size=2, infer_shape=infer_shape,
element_shape=element_shape)
ta2 = ta1.write(0, t1)
ta3 = ta2.write(1, t2)
out = ta3.read(0)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], 'TensorArrayReadV3:0', mode='vm')
for dtype in ["float32", "int8"]:
run(dtype, False, None)
run(dtype, False, tf.TensorShape([None, 2]))
run(dtype, True, None)
示例15: test_tensor_array_scatter
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import int8 [as 别名]
def test_tensor_array_scatter():
def run(dtype_str, infer_shape):
with tf.Graph().as_default():
dtype = tf_dtypes[dtype_str]
if infer_shape:
element_shape = tf.TensorShape([tf.Dimension(None)])
else:
element_shape = None
t = tf.constant(np.array([[1.0], [2.0], [3.0]]).astype(dtype_str), dtype=dtype)
indices = tf.constant([2, 1, 0])
ta1 = tf.TensorArray(dtype=dtype, size=3,
infer_shape=infer_shape,
element_shape=element_shape)
ta2 = ta1.scatter(indices, t)
out0 = ta2.read(0)
out1 = ta2.read(1)
out2 = ta2.read(2)
g = tf.get_default_graph()
compare_tf_with_tvm([], [], ['TensorArrayReadV3:0'], mode='vm')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_1:0'], mode='vm')
compare_tf_with_tvm([], [], ['TensorArrayReadV3_2:0'], mode='vm')
for dtype in ["float32", "int8"]:
run(dtype, False)
run(dtype, True)