TensorFlow变量常用操作

1、矩阵之间相乘:
import tensorflow as tf
a = 3
# Create a variable.
w = tf.Variable([[0.5,1.0]])
x = tf.Variable([[2.0],[1.0]])

y = tf.matmul(w, x)

#variables have to be explicitly initialized before you can run Ops
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print (y.eval())

打印:

[[ 2.]]

2、TensorFlow很多操作跟numpy类似
#tensorflow很多操作跟numpy类似
# float32
a = tf.zeros([3, 4], tf.int32) #==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

tensor = [[1, 2, 3], [4, 5, 6]]
b = tf.zeros_like(tensor) #==> [[0, 0, 0], [0, 0, 0]]

c = tf.ones([2, 3], tf.int32) #==> [[1, 1, 1], [1, 1, 1]]

d = tf.ones_like(tensor) #==> [[1, 1, 1], [1, 1, 1]]

# Constant 1-D Tensor populated with value list.
e = tensor = tf.constant([1, 2, 3, 4, 5, 6, 7])#=> [1 2 3 4 5 6 7]

# Constant 2-D tensor populated with scalar value -1.
f = tensor = tf.constant(-1.0, shape=[2, 3]) #=> [[-1. -1. -1.]
                                          #   [-1. -1. -1.]]

g = tf.linspace(10.0, 12.0, 3, name="linspace") #=> [ 10.0  11.0  12.0]

start = 3
limit = 18
delta = 3
h = tf.range(start, limit, delta) #==> [3, 6, 9, 12, 15]

init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print("a = ", sess.run(a))
    print("b = ", sess.run(b))
    print("c = ", sess.run(c))
    print("d = ", sess.run(d))
    print("e = ", sess.run(e))
    print("f = ", sess.run(f))
    print("g = ", sess.run(g))
    print("h = ", sess.run(h))

打印:

a =  [[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

b =  [[0 0 0]
 [0 0 0]]

c =  [[1 1 1]
 [1 1 1]]

d =  [[1 1 1]
 [1 1 1]]

e =  [1 2 3 4 5 6 7] 

f =  [[-1. -1. -1.]
 [-1. -1. -1.]] 

g =  [ 10.  11.  12.] 

h =  [ 3  6  9 12 15]

3、生成的值服从具有指定平均值和标准差的正态分布
#生成的值服从具有指定平均值和标准差的正态分布
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# 洗牌 Shuffle the first dimension of a tensor
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# Each time we run these ops, different results are generated
sess = tf.Session()
print (sess.run(norm))
print (sess.run(shuff))
sess.close()

打印:

[[ 2.7366724   4.19591808 -0.39844567]
 [ 0.6297431  -0.77744102  5.4052186 ]] 

[[3 4]
 [5 6]
 [1 2]]

4、常量的加减乘除
a = tf.constant(5.0)
b = tf.constant(10.0)

add = tf.add(a,b,name="add")
subtract = tf.subtract(a,b,name="subtract")
multiply = tf.multiply(a,b,name="multiply")
divide = tf.div(a,b,name="divide")

with tf.Session() as sess:
    print("a = " , sess.run(a))
    print("b = " , sess.run(b))
    print("a + b = " , sess.run(add))
    print("a - b = " , sess.run(subtract))
    print("a x b = " , sess.run(multiply))
    print("a / b = " , sess.run(divide))

打印:

a =  5.0
b =  10.0
a + b =  15.0
a - b =  -5.0
a x b =  50.0
a / b =  0.5

5、assign方法:要修改Variable对象的值,可使用assign方法。该方法的作用是为Variable对象赋予新值。请注意,assign是一个Op,要使其生效必须在一个Session对象中运行。
state = tf.Variable(0)
new_value = tf.add(state, tf.constant(1))
update = tf.assign(state, new_value)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(state))
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

打印:

0
1
2
3

6、train.Saver
w = tf.Variable([[0.5,1.0]])
x = tf.Variable([[2.0],[1.0]])
y = tf.matmul(w, x)
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(init_op)
    # Do some work with the model.
    # Save the variables to disk.
    save_path = saver.save(sess, "F:/AI/data/mydata")
    print ("Model saved in file: ", save_path)

这里写图片描述


7、numpy 转换成tensor
import numpy as np
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
     print(sess.run(ta))

打印:

[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
8、使用placeholder(占位符)
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
    print(sess.run(output, feed_dict={input1:[7.], input2:[2.]}))

打印:

[ 14.]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值