# 变量
import tensorflow as tf
biases=tf.Variable(tf.zeros([2,3]))#定义一个2x3的全0矩阵
q = K.ones(shape=(2, 2))
sess=tf.InteractiveSession()#使用InteractiveSession函数
biases.initializer.run()#使用初始化器 initializer op 的 run() 方法初始化 'biases'
q.initializer.run()#使用初始化器 initializer op 的 run() 方法初始化 'biases'
print(sess.run(q))#输出变量值
print(sess.run(biases))#输出变量值
# 常量
import tensorflow as tf
# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor `c`.
print(sess.run(c))
import tensorflow as tf
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.Session():
# We can also use 'c.eval()' here.
print(c.eval())
# 查看 CPU/GPU 可用状态
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
# 强制使用CPU版本
# 在声明 Session 的时候
session = tf.Session(config=tf.ConfigProto(device_count={'cpu': 0}))
from keras import backend as K
kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32')
K.eval(kvar)
array([[ 1., 2.],
[ 3., 4.]], dtype=float32)
ref:
https://blog.csdn.net/atfuies/article/details/78252100