tf.matmul / tf.multiply

时间:2023-03-10 04:49:28
tf.matmul  / tf.multiply

import tensorflow as tf
import numpy as np

1.tf.placeholder

placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存

等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。

2.tf.session

1.tf.multiply 点乘

input1 = tf.placeholder(tf.float32)

input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1, input2)

with tf.Session() as sess:
     print(sess.run(output, feed_dict = {input1:[3.], input2: [4.]}))  --[12.]

2.tf.matmul 矩阵相乘

x = tf.placeholder(tf.float32, shape=(3, 3))
   y = tf.matmul(x, x)

with tf.Session() as sess:
  #print(sess.run(y)) # ERROR:此处x还没有赋值
  rand_array = np.random.rand(3, 3)
  print("rand_array",rand_array)
  print(sess.run(y, feed_dict={x: rand_array}))