import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# os.environ['PYTHONWARNINGS'] = 'ignore'
import tensorflow as tf
import numpy as np
%precision 2
ipython_plain = get_ipython().display_formatter.formatters['text/plain']
ipython_plain.for_type(np.float64, ipython_plain.lookup_by_type(float));
tf.constant(2, 'float32', [3])
Metal device set to: Apple M1 Max
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([2., 2., 2.], dtype=float32)>
tf.constant(np.random.uniform(size=[2,2]).astype('float32'))
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0.06, 0.09],
[0.61, 0.82]], dtype=float32)>
t3 = tf.range(3)
t3
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([0, 1, 2], dtype=int32)>
t23 = tf.constant(np.array([[1,2,3], [4,5,6]]))
t23
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[1, 2, 3],
[4, 5, 6]])>
with tf.device('/CPU:0'): # Apple TF-GPU bug
var1 = tf.Variable(tf.constant(2, 'float32', [3]), name='var1')
var1
<tf.Variable 'var1:0' shape=(3,) dtype=float32, numpy=array([2., 2., 2.], dtype=float32)>
var1.assign(var1 + 1)
var1
<tf.Variable 'var1:0' shape=(3,) dtype=float32, numpy=array([3., 3., 3.], dtype=float32)>
var1[1]
<tf.Tensor: shape=(), dtype=float32, numpy=3.0>
var1[1].assign(9)
var1
<tf.Variable 'var1:0' shape=(3,) dtype=float32, numpy=array([3., 9., 3.], dtype=float32)>
t23
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[1, 2, 3],
[4, 5, 6]])>
t23 + 1
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=
array([[2, 3, 4],
[5, 6, 7]])>
t23 > 2
<tf.Tensor: shape=(2, 3), dtype=bool, numpy=
array([[False, False, True],
[ True, True, True]])>
tf.reduce_sum(t23)
<tf.Tensor: shape=(), dtype=int64, numpy=21>
tf.reduce_sum(t23, axis=0)
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([5, 7, 9])>
tf.reduce_sum(t23, axis=0, keepdims=True)
<tf.Tensor: shape=(1, 3), dtype=int64, numpy=array([[5, 7, 9]])>
tf.reduce_max(t23)
<tf.Tensor: shape=(), dtype=int64, numpy=6>
tf.reduce_mean(t23)
<tf.Tensor: shape=(), dtype=int64, numpy=3>
tf.argmax(t23, axis=1)
<tf.Tensor: shape=(2,), dtype=int64, numpy=array([2, 2])>
tf.cumsum(v3)
<tf.Tensor: shape=(3,), dtype=int64, numpy=array([1, 3, 6])>
t32 = tf.transpose(t23)
t32
<tf.Tensor: shape=(3, 2), dtype=int64, numpy=
array([[1, 4],
[2, 5],
[3, 6]])>
tf.matmul(t23, t32)
<tf.Tensor: shape=(2, 2), dtype=int64, numpy=
array([[14, 32],
[32, 77]])>
tf.squeeze(tf.constant(0, 'int32', [3,2,1]))
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[0, 0],
[0, 0],
[0, 0]], dtype=int32)>
tf.expand_dims(t23, -1).shape
TensorShape([2, 3, 1])
tf.expand_dims(t23, 0).shape
TensorShape([1, 2, 3])