# Basic Operations example using TensorFlow library
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
with tf.Session() as sess:
print("a: %i" % sess.run(a), "b:%i" % sess.run(b))
print("Addition with constant: %i" % sess.run(a + b))
print("Multiplication with constants: %i" % sess.run(a * b))
print("-----------------------------------------------------")
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
mul = tf.multiply(a, b)
with tf.Session() as sess:
print("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}))
print("Multiplication with variables: % i" % sess.run(mul, feed_dict={a: 2, b: 3}))
print("--------------------------------------------------------")
matrix1 = tf.constant([[3.,3.]])
matrix2 = tf.constant([[2.], [2.]])
print("matrix1:", matrix1)
print("matrix2:", matrix2)
product = tf.matmul(matrix1, matrix2)
print("product:", product)
# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op. This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session. They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of threes ops in the
# graph: the two constants and matmul.
#
# The output of the op is returned in 'result' as a numpy `ndarray` object.
with tf.Session() as sess:
result = sess.run(product)
print("result:", result)
if __name__ == "__main__":
pass
Basic Operations
最新推荐文章于 2022-09-27 21:40:29 发布