1 Hello Word
import tensorflow as tf
hello_op =tf.constant("hello,word!")
with tf.Session() as sess:
print(sess.run(hello_op))
2.Tensorflow 简单运算
import tensorflow as tf
a = tf.add(3,5)
sess = tf.Session()
print(sess.run(a))
# 8
sess.close();
简单运用
加载Tensorflow 简历俩个matrix 输出俩个matrix 矩阵相乘的结果 matmul 表示俩个矩阵相乘
import tensorflow as tf
matrix1 =tf.constant([3,3])
matrix2 = tf.constant([[2],[2]])
product = tf.matmul(matrix1,matrix2)
product 不是直接计算的步骤,我们会使用session来激活 product 计算结果
sess = tf.Session()
result =sess.run(product)
print(result)
Variable 变量
import tensorflow as tf
state =tf.Variable(0,name='counter')
#定义常量 one
one =tf .constant(1)
# 定义加法步骤
new_value = tf.add(state,one)
# 将state 更新成new_value
update = tf.assign(state,new_value)
#使用Session
with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state))
占位符 placeholder
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 =tf.placeholder(tf.float32)
#mul = multiply 是将input1 和input2 做乘法运算,并输出为output
output =tf.multiply(input1,input2)
with tf.Session() as sess:
print(sess.run(ouput,feed_dict={input1:[7.],input2:[2.]}))
添加层 def add_layer()
网友评论