1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import tensorflow as tf
a = tf.constant([1, 2, 3]) b = tf.constant([[1, 2], [3, 4]]) c = tf.zeros((3, 4)) d = tf.ones((2, 3)) e = tf.random.normal((3, 3), mean=0, stddev=1) f = tf.range(0, 10, delta=2)
print(f"张量形状: {b.shape}") print(f"数据类型: {b.dtype}") print(f"设备位置: {b.device}")
x = tf.constant([1.0, 2.0, 3.0]) y = tf.constant([4.0, 5.0, 6.0])
print(tf.add(x, y)) print(tf.multiply(x, y)) print(tf.matmul(x[:2, tf.newaxis], y[tf.newaxis, :2])) print(tf.reduce_mean(x)) print(tf.reduce_sum(x))
|