iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 18
0
Google Developers Machine Learning

想使自身成長就先從連續30天的機器學習開始吧!系列 第 18

Day18 Intro to TensorFlow 2

在探索TensorFlow的設計模式跟構想後,會撰寫簡單的TensorFlow程式予以實做,內容有tensor的相加、繪圖、基礎操作指令......


Adding two tensors

a = np.array([5, 3, 8])
b = np.array([3, -1, 2])
c = np.add(a, b)
print(c)

Build the graph

a = tf.constant([5, 3, 8])
b = tf.constant([3, -1, 2])
c = tf.add(a, b)
print(c)

Run the graph

with tf.Session() as sess:
  result = sess.run(c)
  print(result)

Using a feed_dict

a = tf.placeholder(dtype=tf.int32, shape=(None,))  # batchsize x scalar
b = tf.placeholder(dtype=tf.int32, shape=(None,))
c = tf.add(a, b)
with tf.Session() as sess:
  result = sess.run(c, feed_dict={
      a: [3, 4, 5],
      b: [-1, 2, 3]
    })
  print(result)

應用範例:Heron's Formula in TensorFlow

def compute_area(sides):
  # slice the input to get the sides
  a = sides[:,0]  # 5.0, 2.3
  b = sides[:,1]  # 3.0, 4.1
  c = sides[:,2]  # 7.1, 4.8
  
  # Heron's formula
  s = (a + b + c) * 0.5   # (a + b) is a short-cut to tf.add(a, b)
  areasq = s * (s - a) * (s - b) * (s - c) # (a * b) is a short-cut to tf.multiply(a, b), not tf.matmul(a, b)
  return tf.sqrt(areasq)

with tf.Session() as sess:
  # pass in two triangles
  area = compute_area(tf.constant([
      [5.0, 3.0, 7.1],
      [2.3, 4.1, 4.8]
    ]))
  result = sess.run(area)
  print(result)

上一篇
Day17 Intro to TensorFlow
下一篇
Day19 Debugging TensorFlow
系列文
想使自身成長就先從連續30天的機器學習開始吧!31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言