placeholder

import tensorflow as tf 
import numpy as np 

# 去掉警告信息
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# placeholder 输入为 一个数字
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: 7, input2: 2}))    # 14.0
    print(sess.run(output, feed_dict = {input1: [7], input2: [2]}))# [14.0]


# placeholder 输入为 矩阵
input1 = tf.placeholder(tf.float32, [2, 2])
input2 = tf.placeholder(tf.float32, [2, 2])

output = tf.multiply(input1, input2) # 点点相乘

with tf.Session() as sess:
    print(sess.run(output, feed_dict = {input1: [[1, 2], [3, 4]], input2: [[1, 2], [3, 4]]}))
# [[  1.   4.]
#  [  9.  16.]]

# matmul 与 multiply
input1 = tf.placeholder(tf.float32, [2, 2])
input2 = tf.placeholder(tf.float32, [2, 2])

output = tf.matmul(input1, input2) # 点点相乘

with tf.Session() as sess:
    print(sess.run(output, feed_dict = {input1: [[1, 2], [3, 4]], input2: [[1, 2], [3, 4]]}))
# [[  7.  10.]
#  [ 15.  22.]]

你可能感兴趣的:(placeholder)