由于tensorflow使用c实现,所以鼻炎要与C联系起来
tenorflow的python版本的constant对象就是C的const变量或者数组
且看代码:
import tensorflow as tf
a = tf.constant(12,dtype=tf.int32) #cosnt int a=12;
b = tf.constant(12.0,dtype=tf.float32) #cosnt float b=12.0;
c = tf.constant([12],dtype=tf.int32) #const int c[1]={12};
d = tf.constant([12],dtype=tf.int32) #const int d[1]={12};
e = tf.constant([1,2,3],dtype=tf.int32)#const int e[3]={1,2,3};
f = tf.constant([[1,2],[3,4]],dtype=tf.int32)#const int f[2][2]={{1,2},{3,4}};
g = tf.constant([[[1,2],[3,4]],[[5,6],[7,8]]],dtype=tf.int32)#const int f[2][2][2]={{{1,2},{3,4}},{{5,6},{7,8}}};
with tf.Session() as sess:
print(sess.run(a))
print(sess.run(b))
print(sess.run©)
print(sess.run(d))
print(sess.run(e))
print(sess.run(f))
print(sess.run(g))
输出为:
12
12.0
[12]
[12]
[1 2 3]
[[1 2]
[3 4]]
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
对constant对象重新赋值,是会报错的,例如以下代码:
a = tf.constant(12,dtype=tf.int32) #cosnt int a=12;
tf.assign(a,88)