# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/janewen134/tensorflow_self_improvement/blob/after_class_revision/revision1.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="l4H_FcOA4zPk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="38a16b45-896d-4b5e-9899-f5d199cf4743"
import tensorflow as tf
import numpy as np

a = tf.constant(np.arange(1,9), dtype=tf.int32)
print(a)

# + id="OKzcyl4d8SDj" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="58e5ba0c-0c41-48ae-ef3a-8bb9829781ed"
import tensorflow as tf
import numpy as np
a = np.arange(1,3, dtype=np.int32)
b = tf.convert_to_tensor(a)
print(a)

# + id="pfOwRS_j-iOJ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} outputId="9e51e73d-42a6-4b4c-8672-2c0670a7183b"
import tensorflow as tf
a = tf.random.normal([5,5,5], mean=0, stddev=2)
b = tf.random.truncated_normal([5,5,5], mean=0, stddev=2)
c = tf.random.uniform([3,3], minval=-2, maxval=2)
print(c)

# + id="EjgEc6Gf6Htw" colab_type="code" colab={}
import tensorflow as tf
tf.ones([3,3,3])
tf.zeros([6,7],dtype=tf.int16)
c=tf.fill([3,3,3,3], 0)
#print(c)

# + id="-nThi983WK8x" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="be5e0b4f-5442-410c-ac9a-631b9b6fd028"
x1 = tf.constant ([1., 2., 3.], dtype=tf.float64)
print(x1)
x2 = tf.cast (x1, tf.int32)
print(x2)
print (tf.reduce_min(x2), tf.reduce_max(x2))

# + id="c2CKCGNaWN-l" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 102} outputId="0508f943-1049-416b-c0c4-a28ddd170429"
x=tf.constant( [ [ 1, 2, 3],
[ 2,2,3]])
print(x)
print(tf.reduce_mean( x )) 
print(tf.reduce_sum( x, axis=1 ))

# + id="G62hhws2XGzr" colab_type="code" colab={}
w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1))

# + id="BckqgzqdZjo_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="20c96484-8500-4041-c4c9-aa847ae9fd4c"
a = tf.ones([1, 3])
b = tf.fill([1, 3], 3.) 
print(a)
print(b)
print(tf.add(a,b)) 
print(tf.subtract(a,b)) 
print(tf.multiply(a,b)) 
print(tf.divide(b,a))

# + id="V7K0YWJAaHEw" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 153} outputId="9a909fd8-f812-4840-f836-42dd77d228e4"
a = tf.fill([2, ], 3.) 
print(a) 
print(tf.pow(a, 3)) 
print(tf.square(a)) 
print(tf.sqrt(a))
b = tf.fill([1, 2], 3.) 
print(b) 
print(tf.square(b))
print(tf.pow(b,3))
print(tf.sqrt(b))

# + id="XNw8cYzlarX6" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} outputId="33c09304-9eb4-4bc4-a375-5ae6687b9ef9"
a = tf.ones([3, 2])
b = tf.fill([2, 3], 3.) 
print(tf.matmul(a,b))

# + id="lNSQY4ZPb602" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 139} outputId="aab68644-234b-4181-9cc1-1045a2a534d0"
import tensorflow as tf

features = tf.ones([5, 4])
labels = tf.constant([12,22,44,21])
dataset = tf.data.Dataset.from_tensor_slices((features[:,:1], features[:,1:]))
print(dataset)
for element in dataset:
  print(element)

# + id="p6ikAeG2eSf_" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="7187a972-e53e-4db7-afa8-cb69634bc3f4"
with tf.GradientTape() as tape:
  # w = tf.Variable(tf.constant(3.0))
  # loss = tf.pow(w,2) 
  w = tf.Variable(3.0)
  loss = tf.square(w) 
grad = tape.gradient(loss, w)
print(grad)

# + id="Cp1L1h7chQC1" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="330e42d3-84aa-4b6f-ca52-2ccee695a17f"
seq = ['one', 'two', 'three']
i = 9
for j, element in enumerate(seq): 
  print(i, element)
  print(j, element)

# + id="Zvzg2R3ChsxT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} outputId="c5407bd0-b967-4014-e808-c353a266895c"
classes = 4
labels = tf.constant([1,2,5]) # max value 5 is out of range
labels = tf.constant([1,2,4]) # max value is 4 at a maximum
labels = tf.constant([-1, 0, 4]) # min value -1 is out of range, too
output = tf.one_hot(labels, depth=classes)
print(output)

# + id="To2HY4wCLfVd" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="2e3e2db6-00da-470e-98be-519a702073a3"
import tensorflow as tf

t = tf.nn.softmax([2.,3.,1.])
print(t)

t_ = tf.constant([2.,3,1])
t__ = tf.nn.softmax(t_)
print(t__)
# They are definitely the same thing!!!!!

# + id="Zy704UN7ioD1" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0a7cbd40-5455-4dad-9549-7e5cbf7f4004"
y = tf.constant([1., 2, -8]) # The value should be half, bfloat16, float, double
y_pro = tf.nn.softmax(y)
print("After softmax, y_pro is: ", y_pro)

# + id="NohojfUGlIxt" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="62d40d08-75cd-4bb8-e0fe-ab24234806d2"
w = tf.Variable(3)
w.assign_sub(1)
print(w)

# + id="RweP5mLEm_cl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 136} outputId="c57c1de8-3bb2-4fbe-a52c-fae329a6294c"
import numpy as np
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print(test)
print( tf.argmax (test, axis=0)) # 返回每一列(经度)最大值的索引 
print( tf.argmax (test, axis=1)) # 返回每一行(纬度)最大值的索引
print(tf.argmax(test)) # default axis is axis 0

# + id="YxsmLsMznS3H" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="7139c305-b1ba-49e6-be92-8c37320e0cd8"
import numpy as np
np.random.seed(4)
a = np.random.randint(100, size=(2,2))
print(a)

# + id="_Aer-HJVmzyc" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered