Tensorflow 笔记

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()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容