<jupyter_start><jupyter_text>Low Level TensorFlow, Part I: Basics* https://www.tensorflow.org/guide/low_level_intro<jupyter_code># import and check version
import tensorflow as tf
# tf can be really verbose
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)
# a small sanity check, does tf seem to work ok? 
hello = tf.constant('Hello TF!')
sess = tf.Session()
print(sess.run(hello))
sess.close()<jupyter_output>b'Hello TF!'<jupyter_text>First define a computational graph composed of operations and tensors* the main object you manipulate and pass around is the tf.Tensor. * a tf.Tensor object represents a partially defined computation that will eventually produce a value* tf.Tensor is a generalization of vectors and matrices to potentially higher dimensions* TensorFlow represents tf.Tensor as n-dimensional arrays of base datatypes* TensorFlow programs work by first building a graph of tf.Tensor object* detailing how each tensor is computed based on the other available tensors by running parts of this graph to achieve the desired resultshttps://www.tensorflow.org/guide/tensors<jupyter_code>a = tf.constant(3.0, dtype=tf.float32) # special type of tensor
b = tf.constant(4.0) # also tf.float32 implicitly
total = a + b
print(a)
print(b)
print(total)
# types need to match
try:
  tf.constant(3.0, dtype=tf.float32) + tf.constant(4, dtype=tf.int32)
except TypeError as te:
  print(te)
# https://www.tensorflow.org/api_docs/python/tf/dtypes/cast
a = tf.constant(3, dtype=tf.int32)
b = tf.cast(tf.constant(4.0, dtype=tf.float32), tf.int32)
int_total = a + b
int_total<jupyter_output><empty_output><jupyter_text>Then use a session to execute the graph<jupyter_code># sessions need to be closed in order not to leak ressources, this makes sure close is called in any case
with tf.Session() as sess:
  print(sess.run(total))
#   print(sess.run(int_total))<jupyter_output>7.0<jupyter_text>Graphs can be executed on CPU, GPU, and even TPU<jupyter_code># let's see what compute devices we have available, hopefully a GPU 
# if you do not see it, switch on under Runtime->Change runtime type
with tf.Session() as sess:
  devices = sess.list_devices()
  for d in devices:
      print(d.name)
tf.test.gpu_device_name()
# GPU requires nvidia cuda
tf.test.is_built_with_cuda()
with tf.device("/device:XLA_CPU:0"):
  with tf.Session() as sess:
    print(sess.run(total))<jupyter_output>7.0<jupyter_text>Feeding data to a graph<jupyter_code>x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
with tf.Session() as sess:
  try:
    print(sess.run(z))
  except tf.errors.InvalidArgumentError as iae:
     print(iae.message)
with tf.Session() as sess:
    print(sess.run(z, feed_dict={x: 3.0, y: 4.5}))<jupyter_output>7.5
{'fraction_non_alphanumeric': 0.10564663023679417, 'fraction_numerical': 0.014207650273224045, 'mean_word_length': 5.15695067264574, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '136331', 'license': 'MIT', 'n_tokens_mistral': 945, 'n_tokens_neox': 912, 'n_words': 291, 'path': 'notebooks/tensorflow/tf_low_level_intro.ipynb', 'repo_name': 'subodhchhabra/ai', 'size': '2745'}