TensorFlow入门例子(0)

来源:互联网 发布:自媒体 内容来源 知乎 编辑:程序博客网 时间:2024/06/05 21:15

Introduction

basic_operations

# Basic constant operationsfrom __future__ import print_functionimport tensorflow as tfa = tf.constant(2)b = tf.constant(3)with tf.Session() as sess:    print("a=2, b=3")   # a=2, b=3    print("Addition with constants: %i" % sess.run(a+b))  # Addition with constants: 5    print("Multiplication with constants: %i" % sess.run(a*b))  # Multiplication with constants: 6# Basic Operations with variable as graph inputa = 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}))  # Addition with variables: 5    print("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3}))  # Multiplication with variables: 6# More in details:matrix1 = tf.constant([[3., 3.]])matrix2 = tf.constant([[2.],[2.]])product = tf.matmul(matrix1, matrix2)with tf.Session() as sess:    result = sess.run(product)    print(result)  # [[ 12.]]

helloworld

import tensorflow as tfhello = tf.constant('Hello, TensorFlow!')sess = tf.Session()print sess.run(hello)  # Hello, TensorFlow!
原创粉丝点击