tensorflow中的一些操作和numpy中的很像,下面列出几个比较常见的操作
import tensorflow as tf #定义三行四列的零矩阵
tf.zeros([3,4])
#定义两行三列的全1矩阵
tf.ones([2,3])
#定义常量
tensor = tf.constant([1,2,3,4,5,6,7])
#定义两行三列全为-1的矩阵
tensor = tf.constant(-1.0.shape=[2,3])
#[10 11 12]
tf.linspace(10.0,12.0,3,name="linespace") tf.range(start,end,delta)
#构造两行三列的均值为mean,方差为stddev的符合正态分布的矩阵
norm = tf.random_normal([2,3],mean=-1,stddev=4)
#洗牌操作
c = tf.constant([[1,2],[3,4],[5,6]])
shuff = tf.random_shuffle(c)
sess = tf.Session()
print(sess.run(norm))
print(sess.run(shuff))
#赋个初始值
state = tf.Variable(0)
#初始值加1
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))
#numpy向tensorflow转换
import numpy as np
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
print(sess.run(ta))
#tensorflow中的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.]}))