TensorFlow-1

先定义变量再定义操作,再在一个session中进行初始化,进行计算。

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

#tf需要先把数据转化为tf,tensor,再进行flow操作,也就是先定义变量再定义操作,再进行下面的初始化,后对初始化,创造了一个范围session,在范围中初始化,然后在范围的初始化后进行计算。

在TensorFlow中,数据类型用float32,因为在cpu还是在gpu上都是支持的

# float32
tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

# 'tensor' is [[1, 2, 3], [4, 5, 6]]
tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]
tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]

# 'tensor' is [[1, 2, 3], [4, 5, 6]]
tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]

# Constant 1-D Tensor populated with value list.
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.
tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                              [-1. -1. -1.]]


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


# 'start' is 3
# 'limit' is 18
# 'delta' is 3

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

建立一个高斯分布,指定均值mean,方差stddev,步骤还是先进行定义变量和计算方式在进行初始化,在设定范围进行初始化和计算。

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


小例子,0初始,循环加1,打印出来

state = tf.Variable(0)
new_value = tf.add(state, tf.constant(1))#相加
update = tf.assign(state, new_value)#更新,把new的值赋给前面的state的值,进行更新
#上面是骨架写好了没法计算,需要建立session区域,然后对全局变量初始化tf.global_variables_initializer(),然后循环跑骨架内容。仔细观察跑的时候的变量的形式。
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))


saver类,先存着计算步骤

#tf.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, "C://tensorflow//model//test")

    print ("Model saved in file: ", save_path)


下面是np转化为tf格式的方法,建议还是直接用tf.Variable([[0.5,1.0]]),这样的,直接支持的,比较好。

import numpy as np
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:

     print(sess.run(ta))




下面是另一种逻辑编程,先建立两个区域预留给数据用,后在生成数据feed_dict={input1:[7.], input2:[2.]}

input1 = tf.placeholder(tf.float32)#需要指定格式tf.float32
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
    print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))

猜你喜欢

转载自blog.csdn.net/n_sapientia/article/details/81054685