首页 > 其他分享 >tensorflow1.x的使用记录

tensorflow1.x的使用记录

时间:2022-11-01 18:06:40浏览次数:63  
标签:sess tensorflow1 run 记录 print Session 使用 tf tensorflow


tensorflow1.x 使用原则:

  1. 先定义图,搭建骨架
  2. 再进行图计算

基本模式

import tensorflow as tf
x = tf.constant(12,dtype= 'float32')
sess= tf.Session()
print(sess.run(x))

tensorflow1.x的使用记录_数据类型

指定数据类型dtype

import tensorflow as tf
x = tf.constant(12, dtype= 'float32')
with tf.Session() as sess:
print(sess.run(x))
'''12.0'''

全局初始化,一开始就初始化值

import tensorflow as tf
x = tf.constant(12, dtype= 'float32')
y = tf.Variable(x + 11)
# 初始化
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(y))
import tensorflow as tf
x = tf.constant([14, 23, 40, 30])
y = tf.Variable(x*2 + 100)
model = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(model)
print(sess.run(y))

placeholder占位符

  1. 用于一开始不知道具体形状的操作,填充符使用
import tensorflow as tf
x = tf.placeholder("float", None)
y = x * 10 + 500
with tf.Session() as sess:
placeX = sess.run(y, feed_dict={x: [0, 5, 15, 25]})
print(placeX)
'''
[500. 550. 650. 750.]
  1. 用于知道部分形状
import tensorflow as tf
x = tf.placeholder("float", [None, 4])
y = x * 10 + 1
with tf.Session() as session:
dataX = [[12, 2, 0, -2],
[14, 4, 1, 0],]
placeX = session.run(y, feed_dict={x: dataX})
print(placeX)
'''
[[ 121. 21. 1. -19.]
[ 141. 41. 11. 1.]]


'''
import tensorflow as tf
x = tf.placeholder("float", [2, 4])
y = x * 10 + 1
with tf.Session() as session:
dataX = [[12, 2, 0, -2],
[14, 4, 1, 0],]
placeX = session.run(y, feed_dict={x: dataX})
print(placeX)
'''
[[ 121. 21. 1. -19.]
[ 141. 41. 11. 1.]]
'''
import tensorflow as tf
x = tf.placeholder("float", [2, 4])
y = x * 10 + 1
with tf.Session() as session:
dataX = [[12, 2, 0, -2],
[14, 4, 1, 0],]
placeX = session.run(y, feed_dict={x: dataX})
print(placeX)
'''
[[ 121. 21. 1. -19.]
[ 141. 41. 11. 1.]]
'''

用于初始化权重和bias

# Weight and Bias as Variables as they are to be tuned
W = tf.Variable([2], dtype=tf.float32)
b = tf.Variable([3], dtype=tf.float32)
# Training dataset that will be fed while training
x = tf.placeholder(tf.float32)
# Linear Model
linear_model = W * x + b


标签:sess,tensorflow1,run,记录,print,Session,使用,tf,tensorflow
From: https://blog.51cto.com/u_13859040/5814613

相关文章